If I do a
$services = Get-Service
$services.length
I get 126 services. But if I specify one service
$services = Get-Service -Name vds
$services.length
I get nothing. This messes up loops I do in my script. How do I return the correct number from .length even though the length is 1 ?
With normal strings this is easy, I just specify as array:
$myarray = ,"one","two","three"
$myarray.length
would return 3, and
$myarray = ,"one"
$myarray.length
would return 1..
How do I get the correct length returned?
The reason you get nothing is because $services is no longer an array when you request 1 service by name. You’ll likely have to create the array and assign the service to element 0.
This works for me…