Link to home
Start Free TrialLog in
Avatar of Dayna Aronson
Dayna AronsonFlag for United States of America

asked on

Cannot exit IF statement in PowerShell

I must have an error in my syntax? In line 10, even when the $LocalProcessStatus is equal to running, the script continues on to the other statements instead of exiting and I get this error?
Exit-PSSession : Object reference not set to an instance of an object.
At C:\AdminTasks\Check_PIAnalysisManager.ps1:22 char:41
+ if ($LocalProcessStatus -eq "Running") {Exit-PSSession}
+                                         ~~~~~~~~~~~~~~
 
When I look at examples, it seems like this should be ok. What am I doing wrong?  

1)      $LocalProcess = get-service PIAnalysisManager -ComputerName localhost
2)      $RemoteProcess = get-service PIAnalysisManager -ComputerName VED-AF01-AN.dems.int

3)      #Write-host $LocalProcess.Status
4)      $LocalProcessStatus = $LocalProcess.Status
5)      Write-host $LocalProcessStatus
6)      #
7)      $RemoteProcessStatus = $RemoteProcess.Status
8)      Write-host $RemoteProcessStatus

9)      # check to see if service is running locally. if it is just exit
10)      if ($LocalProcessStatus -eq "Running") {Exit-PSSession}
11)      elseif ($RemoteProcessStatus -eq "Running") {Exit-PSSession}
12)      else {Start-Service PIAnalysisManager}

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dayna Aronson

ASKER

Would have bet my life I started using "exit" and had the same problem which is why I tried Exit-PSSession... But yes, using "exit" gives the behavior I want. Thanks.
Thanks.