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:'
}
Else
{
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