4

I am trying to count some lines output by a command. Basically all the lines which end in " Y " in this example.

Fist Capture the command results:

PS> $ItsAgents = tacmd listSystems -n Primary:SomeHost:NT
PS> $ItsAgents
Managed System Name      Product Code Version     Status
Primary:SomeHost:NT NT           06.30.07.00 Y
SomeHost:Q7         Q7           06.30.01.00 N

Now count the online ones:

PS> $AgentCount = ($ItsAgents | Select-String ' Y ').Count
PS> $AgentCount 
1

Now that all works as I expect. So I put it in my script like this:

$ItsAgents = tacmd listSystems -n $agent
Write-Host $ItsAgents
$BeforeCount = ($ItsAgents | Select-String ' Y ').Count

And when the script runs (under Set-StrictMode) I get:

The property 'Count' cannot be found on this object. Verify that the
property exists.
At Y:\Scripts\newMoveAgents.ps1:303 char:7
+             $BeforeCount = ($ItsAgents | Select-String ' Y ').Count
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict

The Write-Host does output sane results so $agent is set correctly and the tacmd command is running OK So why does it fail in the script, but work on command line?

That Robin
  • 75
  • 1
  • 9
  • OK I have found it does work in some cases, but not in others, the output of `$ItsAgents` shows it is working when more than two lines of results come back it looks to work fine, but this one errors: `Managed System Name Product Code Version Status Primary:S1TWANAT01STD:NT NT 06.30.07.00 Y` With the same `The property 'Count' cannot be found on this object. Verify that the property exists....` – That Robin Nov 07 '18 at 09:17
  • The example with only two lines returned works fine on the command line though, odd. – That Robin Nov 07 '18 at 09:29
  • 2
    Presumably the `$ItsAgents` is a single string, try to split it into separate lines: `$ItsAgents = (tacmd listSystems -n Primary:SomeHost:NT) -Split '[\r\n]'` – iRon Nov 07 '18 at 09:48
  • Thanks @iRon. I tried your suggestion: I have updated my code to `$ItsAgents = ( tacmd listSystems -n $agent ) -Split '[\r\n]'` But I still get the same error in script (and no error on command line) – That Robin Nov 07 '18 at 10:08
  • Rectification: To avoid empty lines, the split command should be: `-Split '[\r\n]+'` (but I do not expect this to resolve the error) – iRon Nov 07 '18 at 10:44
  • 1
    Anyways, I am not able to reproduce the error, even with a null (`$Null`), array including null (`@(Null)`), or any strange object like: `($Null | Select-String ' Y ').Count`. In other words, try to find out the object type that causes the error. (is `$ItsAgents` still a string array as presumed? check e.g.: `$ItsAgents.PSTypeNames`). A quick solution might be to force the result to an array by adding an `@` sign: `@($ItsAgents | Select-String ' Y ').Count`. – iRon Nov 07 '18 at 10:44
  • Which version are you using? What's the output from `$PSVersionTable` – Dan Stef Nov 07 '18 at 10:44
  • `$PSVersionTable Name Value ---- ----- PSVersion 5.1.14409.1012 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.14409.1012 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 ` – That Robin Nov 07 '18 at 10:46

1 Answers1

5

Use the @() operator to force the output to always be an array:

$BeforeCount = @($ItsAgents | Select-String ' Y ').Count

The array sub-expression operator creates an array, even if it contains zero or one object. (Microsoft Docs)

Note: Afaik it should work the same way both as a script and inside the console. Maybe your commands produce different output, where the console version returns 2+ results but for some reason the script version only 1 or 0 results, which would be the reason why there is no Count property.

marsze
  • 15,079
  • 5
  • 45
  • 61
  • 1
    Thanks @marsze that has solved my problem. It is indeed possible that some instances would return a count of zero when running in the script. – That Robin Nov 07 '18 at 10:44