PowerShell property Count cannot be found on this object
up vote
2
down vote
favorite
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:ScriptsnewMoveAgents.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?
arrays powershell
|
show 3 more comments
up vote
2
down vote
favorite
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:ScriptsnewMoveAgents.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?
arrays powershell
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 sameThe property 'Count' cannot be found on this object. Verify that the property exists....
– user1534668
Nov 7 at 9:17
The example with only two lines returned works fine on the command line though, odd.
– user1534668
Nov 7 at 9:29
1
Presumably the$ItsAgents
is a single string, try to split it into separate lines:$ItsAgents = (tacmd listSystems -n Primary:SomeHost:NT) -Split '[rn]'
– iRon
Nov 7 at 9:48
Thanks @iRon. I tried your suggestion: I have updated my code to$ItsAgents = ( tacmd listSystems -n $agent ) -Split '[rn]'
But I still get the same error in script (and no error on command line)
– user1534668
Nov 7 at 10:08
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 7 at 10:44
|
show 3 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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:ScriptsnewMoveAgents.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?
arrays powershell
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:ScriptsnewMoveAgents.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?
arrays powershell
arrays powershell
edited Nov 7 at 11:18
marsze
4,03131640
4,03131640
asked Nov 7 at 9:11
user1534668
455
455
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 sameThe property 'Count' cannot be found on this object. Verify that the property exists....
– user1534668
Nov 7 at 9:17
The example with only two lines returned works fine on the command line though, odd.
– user1534668
Nov 7 at 9:29
1
Presumably the$ItsAgents
is a single string, try to split it into separate lines:$ItsAgents = (tacmd listSystems -n Primary:SomeHost:NT) -Split '[rn]'
– iRon
Nov 7 at 9:48
Thanks @iRon. I tried your suggestion: I have updated my code to$ItsAgents = ( tacmd listSystems -n $agent ) -Split '[rn]'
But I still get the same error in script (and no error on command line)
– user1534668
Nov 7 at 10:08
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 7 at 10:44
|
show 3 more comments
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 sameThe property 'Count' cannot be found on this object. Verify that the property exists....
– user1534668
Nov 7 at 9:17
The example with only two lines returned works fine on the command line though, odd.
– user1534668
Nov 7 at 9:29
1
Presumably the$ItsAgents
is a single string, try to split it into separate lines:$ItsAgents = (tacmd listSystems -n Primary:SomeHost:NT) -Split '[rn]'
– iRon
Nov 7 at 9:48
Thanks @iRon. I tried your suggestion: I have updated my code to$ItsAgents = ( tacmd listSystems -n $agent ) -Split '[rn]'
But I still get the same error in script (and no error on command line)
– user1534668
Nov 7 at 10:08
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 7 at 10:44
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....
– user1534668
Nov 7 at 9:17
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....
– user1534668
Nov 7 at 9:17
The example with only two lines returned works fine on the command line though, odd.
– user1534668
Nov 7 at 9:29
The example with only two lines returned works fine on the command line though, odd.
– user1534668
Nov 7 at 9:29
1
1
Presumably the
$ItsAgents
is a single string, try to split it into separate lines: $ItsAgents = (tacmd listSystems -n Primary:SomeHost:NT) -Split '[rn]'
– iRon
Nov 7 at 9:48
Presumably the
$ItsAgents
is a single string, try to split it into separate lines: $ItsAgents = (tacmd listSystems -n Primary:SomeHost:NT) -Split '[rn]'
– iRon
Nov 7 at 9:48
Thanks @iRon. I tried your suggestion: I have updated my code to
$ItsAgents = ( tacmd listSystems -n $agent ) -Split '[rn]'
But I still get the same error in script (and no error on command line)– user1534668
Nov 7 at 10:08
Thanks @iRon. I tried your suggestion: I have updated my code to
$ItsAgents = ( tacmd listSystems -n $agent ) -Split '[rn]'
But I still get the same error in script (and no error on command line)– user1534668
Nov 7 at 10:08
1
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 7 at 10:44
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 7 at 10:44
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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 not Count
property.
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.
– user1534668
Nov 7 at 10:44
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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 not Count
property.
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.
– user1534668
Nov 7 at 10:44
add a comment |
up vote
1
down vote
accepted
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 not Count
property.
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.
– user1534668
Nov 7 at 10:44
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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 not Count
property.
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 not Count
property.
edited Nov 7 at 10:35
answered Nov 7 at 10:26
marsze
4,03131640
4,03131640
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.
– user1534668
Nov 7 at 10:44
add a comment |
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.
– user1534668
Nov 7 at 10:44
1
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.
– user1534668
Nov 7 at 10:44
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.
– user1534668
Nov 7 at 10:44
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186397%2fpowershell-property-count-cannot-be-found-on-this-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 sameThe property 'Count' cannot be found on this object. Verify that the property exists....
– user1534668
Nov 7 at 9:17
The example with only two lines returned works fine on the command line though, odd.
– user1534668
Nov 7 at 9:29
1
Presumably the
$ItsAgents
is a single string, try to split it into separate lines:$ItsAgents = (tacmd listSystems -n Primary:SomeHost:NT) -Split '[rn]'
– iRon
Nov 7 at 9:48
Thanks @iRon. I tried your suggestion: I have updated my code to
$ItsAgents = ( tacmd listSystems -n $agent ) -Split '[rn]'
But I still get the same error in script (and no error on command line)– user1534668
Nov 7 at 10:08
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 7 at 10:44