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?










share|improve this question
























  • 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






  • 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

















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?










share|improve this question
























  • 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






  • 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















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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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






  • 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












  • 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














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.






share|improve this answer



















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer



















  • 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















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.






share|improve this answer



















  • 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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














  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini