Issues running Get-Service with Invoke-command session RRS feed

  • Question

  • I am trying get service information via a PS session, with the service name being in a variable that is declared depending on which version of the software is installed.  When I run the command with the Variable name I get an out put of every service name and the status.  However if I hard code the service name the command works.  Is there something I need to do when setting the name of the services in a variable?

    Invoke-Command -Session $PSSession -ScriptBlock {                
                    Try {$UmbrellaStatus = Get-Service -name $UmbrellaService -ErrorAction SilentlyContinue}
                    Catch { Write-Host 'Error Occured' }
                    if (!$error)
                        Write-Host 'The service' $UmbrellaStatus.Name 'is currently' $UmbrellaStatus.Status 'on computer' $ComputerName
                        $ExitScript = Read-Host -Prompt 'Would you like to Exit? 1 - Yes, 2 -No:'
                        Write-Host 'Service does not exist on' $ComputerName'.'
                        Write-Host 'Most likely cause is Umbrella is not installed'
                        $ExitScript = Read-Host -Prompt 'Would you like to Exit? 1 - Yes, 2 -No:'

    I have tried setting the variable the following ways, all with similar results. The last will throw an error but it will continue.

    $UmbrellaService = 'Umbrella_RC'
    $UmbrellaService = "Umbrella_RC"
    $UmbrellaService = Umbrella_RC

    Tuesday, February 12, 2019 4:53 PM

All replies

  • The script block does not know about your variable declared outside of the script block.  ;-)

    Search in your favorite search engine for it:  Powershell Scriptblock arguments.

    Live long and prosper!


    Tuesday, February 12, 2019 5:16 PM
  • Try/Catch soes not work with "SilentlyContinue". You must use "Stop"


    Tuesday, February 12, 2019 6:48 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Thursday, February 28, 2019 2:32 PM