Try Catch Finally

Ask your Windows 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.
User avatar
MarvelManiac
Posts: 63
Joined: Mon May 01, 2017 8:28 pm

Try Catch Finally

Post by MarvelManiac » Wed Aug 01, 2018 8:25 am

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?

Code: Select all

$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
}
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.

User avatar
jvierra
Posts: 13301
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Try Catch Finally

Post by jvierra » Wed Aug 01, 2018 8:27 am

You cannot use "Enter-PsSession" in a script. It is for interactive use only.
'

User avatar
MarvelManiac
Posts: 63
Joined: Mon May 01, 2017 8:28 pm

Re: Try Catch Finally

Post by MarvelManiac » Wed Aug 01, 2018 8:32 am

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.

User avatar
jvierra
Posts: 13301
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Try Catch Finally

Post by jvierra » Wed Aug 01, 2018 8:38 am

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
Joined: Mon May 01, 2017 8:28 pm

Re: Try Catch Finally

Post by MarvelManiac » Wed Aug 01, 2018 8:49 am

yeah, our systems that are connected on wireless and invoke-command don't play nice together

User avatar
jvierra
Posts: 13301
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Try Catch Finally

Post by jvierra » Wed Aug 01, 2018 8:55 am

Invoke-Command works perfectly over wireless. Thousands of people use this. Perhaps you have a badly configured network.

User avatar
MarvelManiac
Posts: 63
Joined: Mon May 01, 2017 8:28 pm

Re: Try Catch Finally

Post by MarvelManiac » Wed Aug 01, 2018 10:27 am

Perhaps you have a badly configured network.

Yeah.... I'd be willing to bet that :/

User avatar
MarvelManiac
Posts: 63
Joined: Mon May 01, 2017 8:28 pm

Re: Try Catch Finally

Post by MarvelManiac » 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?

User avatar
jvierra
Posts: 13301
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Try Catch Finally

Post by jvierra » Fri Aug 03, 2018 6:37 am

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
Joined: Mon May 01, 2017 8:28 pm

Re: Try Catch Finally

Post by MarvelManiac » Fri Aug 03, 2018 7:43 am

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.

Code: Select all

Invoke-Command -ComputerName WIRELESSPC -ScriptBlock { Get-Process | Where-Object { $_.Name -eq 'Process1'} | Stop-Process -Force }
[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
on the network and that the name provided is spelled correctly. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (WIRELESSPC.wireless.net:String) [], PSRemotingTransportException
+ 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?

Locked