Kaseya Community

Powershell command isn't parsing properly when using a variable

  • Hi,

    I'm trying to execute a Powershell command in a script as follows:

    Get-Service | Where-Object {$_.Name -eq "#ln#"}

    The variable #ln# is the short service name being passed by the calling monitor set.

    This is the command that ends up being run:

    + Get-Service | Where-Object {$_.Name -eq <<<<  StorageCraft ImageReady}

    Does anyone know the proper syntax I need to use to have the shortname show up properly between the quotes at the end of the line?

    Thanks so much for any advice!

  • Hi

    Why are you doing

    [CODE]

    Get-Service | Where-Object {$_.Name -eq "#In#"}

    [/CODE]

    instead of

    [CODE]

    Get-Service -Name "#In#" | Select Name

    [/CODE]

    If you pipe the Get-Service directly to the Where Statement you use more ressources than using the Get-Service -Name "#In#".

    I understand that you wanted to look to the short name by giving the long name of the service with powershell, so If I've correct understand you should proceed like that :

    [CODE]

    PS C:\Users\admin> Get-Service -DisplayName "Vmware Tools"

    Status   Name               DisplayName

    ------   ----               -----------

    Running  VMTools            VMware Tools

    PS C:\Users\admin> Get-Service -DisplayName "Vmware Tools" | Select Name

    Name

    ----

    VMTools

    [/CODE]

    or the other attributes

    [CODE]

    PS C:\Users\admin> Get-Service -Name "VMTools" | Select DisplayName

    DisplayName

    -----------

    VMware Tools

    [/CODE]

    With the Measure-Command cmdlets you should compare the both way to get the same results :

    [CODE]

    PS C:\Users\admin> Measure-Command {Get-Service | Where-Object {$_.Name -eq "VMTools"}  }

    Days              : 0

    Hours             : 0

    Minutes           : 0

    Seconds           : 0

    Milliseconds      : 56

    Ticks             : 565016

    TotalDays         : 6,53953703703704E-07

    TotalHours        : 1,56948888888889E-05

    TotalMinutes      : 0,000941693333333333

    TotalSeconds      : 0,0565016

    TotalMilliseconds : 56,5016

    PS C:\Users\admin> Measure-Command {Get-Service -Name "VMTools" | Select DisplayName }

    Days              : 0

    Hours             : 0

    Minutes           : 0

    Seconds           : 0

    Milliseconds      : 45

    Ticks             : 452067

    TotalDays         : 5,23225694444444E-07

    TotalHours        : 1,25574166666667E-05

    TotalMinutes      : 0,000753445

    TotalSeconds      : 0,0452067

    TotalMilliseconds : 45,2067

    [/CODE]

    Ok, That's only 11 ms of difference but if you want to be sure to execute the correct way... Measure-Command if your best friends.

    Hope I've helped you ...

  • Ah,

    If you just wanted to have the Display Name property of the services you could use this short way :

    [CODE]

    PS C:\Users\admin> Get-Service -Name "VMTools" |%{$_.DisplayName}

    VMware Tools

    PS C:\Users\admin> Measure-Command {Get-Service -Name "VMTools" |%{$_.DisplayName}}

    Days              : 0

    Hours             : 0

    Minutes           : 0

    Seconds           : 0

    Milliseconds      : 0

    Ticks             : 8922

    TotalDays         : 1,03263888888889E-08

    TotalHours        : 2,47833333333333E-07

    TotalMinutes      : 1,487E-05

    TotalSeconds      : 0,0008922

    TotalMilliseconds : 0,8922

    [/CODE]

    PS :  Could you add a Syntax Highlighter to this forums ? (Support of Lua, PS, XML, Bash and cie...)