I'm in need of being able to stop some processes and stop and start some Scheduled tasks, so far here is my script. Does this look acceptable?
- $computer = 'PC123'
- try{
- Enter-PSSession -Credential (Get-Credential -Credential $env:username) -ComputerName $computer -ErrorAction Stop
- }
- catch{
- $_.Exception.Message
- }
- finally{
- Get-Process | Where-Object {$_.Name -eq 'Process1'} | Stop-Process -Force
- Get-Process | Where-Object {$_.Name -eq 'Process2'} | Stop-Process -Force
- Get-Process | Where-Object {$_.Name -eq 'Process3'} | Stop-Process -Force
- schtasks /end /tn Task 1
- schtasks /end /tn Task 2
- schtasks /run /tn Task 1
- schtasks /run /tn Task 2
- Exit-PSSession
- cls
- Get-ChildItem \\$computer\c$\Windows\Logs\ | where {$_.Name -like "TestCapture*"} | Select-Object -Last 1 | Invoke-Item
- }
Sometimes when I run the script the first time, stopping process1 fails > access denied
If I simply re run the script, it passes
it does not CLS, and it does not open the log file