Try Catch Finally

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Try Catch Finally

Post by MarvelManiac »

Hello,
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?
  1. $computer = 'PC123'
  2.  
  3. try{
  4. Enter-PSSession -Credential (Get-Credential -Credential $env:username) -ComputerName $computer -ErrorAction Stop
  5. }
  6. catch{
  7.  
  8. $_.Exception.Message
  9.  
  10. }
  11. finally{
  12. Get-Process | Where-Object {$_.Name -eq 'Process1'} | Stop-Process -Force
  13. Get-Process | Where-Object {$_.Name -eq 'Process2'} | Stop-Process -Force
  14. Get-Process | Where-Object {$_.Name -eq 'Process3'} | Stop-Process -Force
  15. schtasks /end /tn Task 1
  16. schtasks /end /tn Task 2
  17. schtasks /run /tn Task 1
  18. schtasks /run /tn Task 2
  19. Exit-PSSession
  20. cls
  21. Get-ChildItem \\$computer\c$\Windows\Logs\ | where {$_.Name -like "TestCapture*"} | Select-Object -Last 1 | Invoke-Item
  22. }
here are things I'm noticing
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
Last edited by MarvelManiac on Wed Aug 01, 2018 8:29 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Try Catch Finally

Post by jvierra »

You cannot use "Enter-PsSession" in a script. It is for interactive use only.
'
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Try Catch Finally

Post by MarvelManiac »

Soooo if that's the case, why does it run?

as I mentioned, if I run it again 98% of the script goes through
the two items that should work, don't lol

the reason why I'm trying to use enter-pssession is because our wireless clients can't take some commands without timing out.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Try Catch Finally

Post by jvierra »

If you run this at a prompt it may appear to work but it won't work correctly. If you run it independently as a script it will not work.

Use Invoke-Command in a script.

See help for Enter-PsSession and Invoke-Command.
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Try Catch Finally

Post by MarvelManiac »

yeah, our systems that are connected on wireless and invoke-command don't play nice together
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Try Catch Finally

Post by jvierra »

Invoke-Command works perfectly over wireless. Thousands of people use this. Perhaps you have a badly configured network.
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Try Catch Finally

Post by MarvelManiac »

Perhaps you have a badly configured network.

Yeah.... I'd be willing to bet that :/
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Try Catch Finally

Post by MarvelManiac »

So while my network may not be configured and Enter-PSSession is supposed to be used in an interactive role
could I, create this in a script, send it over to the remote PC have it triggered?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Try Catch Finally

Post by jvierra »

MarvelManiac wrote: Fri Aug 03, 2018 6:21 am So while my network may not be configured and Enter-PSSession is supposed to be used in an interactive role
could I, create this in a script, send it over to the remote PC have it triggered?
If the network is not configured how can you send anything?

YOu need to be clear and accurate in your requests or they will not make much sense to my small tired brain.
User avatar
MarvelManiac
Posts: 63
Last visit: Thu Sep 13, 2018 3:40 pm

Re: Try Catch Finally

Post by MarvelManiac »

Sorry, didn't mean not configured, I meant to say configured properly.....

In my environment, if a computer is only on wireless, we have to use the FQDN to access it

In powershell, there are some commands I cannot execute Invoke-Command being one. Here is an example.
  1. Invoke-Command -ComputerName WIRELESSPC -ScriptBlock { Get-Process | Where-Object { $_.Name -eq 'Process1'} | Stop-Process -Force }
  2. [WIRELESSPC.wireless.net] Connecting to remote server WIRELESSPC.wireless.net failed with the following error message : WinRM cannot process the request. The following error occurred while using Kerberos authentication: Cannot find the computer WIRELESSPC.wireless.net. Verify that the computer exists
  3. on the network and that the name provided is spelled correctly. For more information, see the about_Remote_Troubleshooting Help topic.
  4.     + CategoryInfo          : OpenError: (WIRELESSPC.wireless.net:String) [], PSRemotingTransportException
  5.     + FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStateBroken
So I was wondering if I could get around this by coping a file running the script, export the results to a txt file and return it to my application. You've stated
'You cannot use "Enter-PsSession" in a script. It is for interactive use only.'

In theroy, can I get around this?
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked