Global Variable in Invoke Command

15 Views Asked by At

I want to use a global Variable in an Invoke-Command. In the example below I want to replace the Get-Service with the global variable which will be filled in an earlier Function, $global:ServAction = Get-Service

Invoke-Command -ComputerName $Dispatcher {$global:ServAction 'Teamcenter Dispatcher Scheduler V14000.2.0.11_20230301.00'}

I get the error:

At C:\Temp\PS_RestartServices.ps1:165 char:75
+ ... $ServAction 'Teamcenter Dispatcher Scheduler V14000.2.0.11_20230301.0 ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token ''Teamcenter Dispatcher Scheduler V14000.2.0.11_20230301.00'' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Now when I replace the Global variable (in the Invoke-Command) to value Get-Service, it works just fine. Looks like the : is causing the issue here. How to solve this?

Many thanks in advance. Ronald

0

There are 0 best solutions below