locked
Count mapped drives RRS feed

  • Question

  • Hello I'm using this code to get unavailable mapped drives:

    $MappedDrivesUnavailable = Get-SmbMapping |where -property Status -Value Unavailable -EQ | select LocalPath,RemotePath

    But when I try to get the amount of unavailable mapped drives , it does not work:

    Write-Host "MappedDrivesUnavailable.count : $MappedDrivesUnavailable.count"

    Any clue?

    Thanks

    G

    Friday, May 22, 2020 4:34 PM

All replies

  • (Get-SmbMapping |where -property Status -Value Unavailable -EQ).Count


    \_(ツ)_/

    • Proposed as answer by Vector BCO Tuesday, May 26, 2020 7:53 PM
    Friday, May 22, 2020 4:46 PM
  • Thanks this works in most of the cases, however sometimes I get a void value.
    Tuesday, May 26, 2020 3:44 PM
  • gren, you can use Measure-Object cmdlet for getting 0 when you do not have any unavailable drives or you can update your powershell 2 to the latest official version (5.1) where you will get 0 even without Measire-Object

    (Get-SmbMapping |where -property Status -Value Unavailable -EQ | measure-object).Count


    The opinion expressed by me is not an official position of Microsoft


    • Edited by Vector BCO Tuesday, May 26, 2020 4:14 PM
    • Proposed as answer by Vector BCO Tuesday, May 26, 2020 7:53 PM
    Tuesday, May 26, 2020 4:10 PM
  • gren, you can use Measure-Object cmdlet for getting 0 when you do not have any unavailable drives or you can update your powershell 2 to the latest official version (5.1) where you will get 0 even without Measire-Object

    (Get-SmbMapping |where -property Status -Value Unavailable -EQ | measure-object).Count


    The opinion expressed by me is not an official position of Microsoft


    Measure -Object does not solve this issue.


    \_(ツ)_/

    Tuesday, May 26, 2020 5:36 PM
  • Thanks this works in most of the cases, however sometimes I get a void value.

    If there is an exception then the results will be null.

    The following may help with some versions of PS.

    (@(Get-SmbMapping |Where-Object{$_.Status -eq 'Unavailable')).Count

    Measure-Object is intended for other things.  


    \_(ツ)_/

    Tuesday, May 26, 2020 5:41 PM
  • gren, you can use Measure-Object cmdlet for getting 0 when you do not have any unavailable drives or you can update your powershell 2 to the latest official version (5.1) where you will get 0 even without Measire-Object

    (Get-SmbMapping |where -property Status -Value Unavailable -EQ | measure-object).Count


    The opinion expressed by me is not an official position of Microsoft


    Measure -Object does not solve this issue.


    \_(ツ)_/

    r, u sure? i'm checking my words before posting anything

    The opinion expressed by me is not an official position of Microsoft

    Tuesday, May 26, 2020 5:42 PM

  • r, u sure? i'm checking my words before posting anything

    The opinion expressed by me is not an official position of Microsoft

    I am just challenging you to think on more than one level.   Think of it as a game.


    \_(ツ)_/

    Tuesday, May 26, 2020 5:44 PM

  • r, u sure? i'm checking my words before posting anything

    The opinion expressed by me is not an official position of Microsoft

    I am just challenging you to think on more than one level.   Think of it as a game.


    \_(ツ)_/

    oK, so lets imagine that this is a game :) 

    lets imagine that you will post something just after checking my words but not just writing "this does not solve the issue" Ok?

    If Ok, so lets continue. Lets imagine that you are not rude and you tries help topic starter solve his problem instead of posting rubbish messages to me, Ok?

    If Ok, so lets continue. So if you have "steps to reproduce" for this "issue" (which not an issue at all) please try to post them (STR) with your recommendation, because i have "steps to reproduce" and solution, Ok?


    The opinion expressed by me is not an official position of Microsoft

    Tuesday, May 26, 2020 6:30 PM
  • Thanks this works in most of the cases, however sometimes I get a void value.

    If there is an exception then the results will be null.

    The following may help with some versions of PS.

    (@(Get-SmbMapping |Where-Object{$_.Status -eq 'Unavailable')).Count

    Measure-Object is intended for other things.  


    \_(ツ)_/

    if u will check what you are writing on powershell 2 your code will return 1 even without any drives ;) 

    So i believe you need start here 

    Cannot say how much i love "other things" when someone not posting any of them ;) 


    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Tuesday, May 26, 2020 6:43 PM
    Tuesday, May 26, 2020 6:37 PM

  • r, u sure? i'm checking my words before posting anything

    The opinion expressed by me is not an official position of Microsoft

    I am just challenging you to think on more than one level.   Think of it as a game.


    \_(ツ)_/

    oK, so lets imagine that this is a game :) 

    lets imagine that you will post something just after checking my words but not just writing "this does not solve the issue" Ok?

    If Ok, so lets continue. Lets imagine that you are not rude and you tries help topic starter solve his problem instead of posting rubbish messages to me, Ok?

    If Ok, so lets continue. So if you have "steps to reproduce" for this "issue" (which not an issue at all) please try to post them (STR) with your recommendation, because i have "steps to reproduce" and solution, Ok?


    The opinion expressed by me is not an official position of Microsoft

    Instead of taking this as a contest you need to understand that some criticism is intended to be helpful.  The issue you have is that "Measure-Object" cannot count nulls in a pipeline in some versions of PowerShell.  This is a known issue and has been for a long time.  The behavior noted by the OP is that of either an earlier version of PS or of a missing Net update.  I am not sure of all of the pieces.  

    By nor reacting to a criticism as if it is negative you can learn more about PS and the underlying technology.  You offered an alternate to my solution which is actually the best way to do what was asked.  Why use a pipeline when you want a single value?

    The second script I offered will overcome all issues with versions using the older method for getting results from a null return.

    The changes to PS were to eliminate singletons from a result when the result was defined as a collection.  Strings were also modified to behave this way.  Most singletons will now return 1 for an object and 0 for an empty collection.  A $null will always return 0.  This was not the case for earlier versions of PowerShell.

    My reply as intended to be informative but also challenged you to think deeper.  By thinking deeper and follwoing the trail you learn more than can be posted in a forum.


    \_(ツ)_/

    Tuesday, May 26, 2020 7:07 PM
  • Thanks this works in most of the cases, however sometimes I get a void value.

    If there is an exception then the results will be null.

    The following may help with some versions of PS.

    (@(Get-SmbMapping |Where-Object{$_.Status -eq 'Unavailable')).Count

    Measure-Object is intended for other things.  


    \_(ツ)_/

    if u will check what you are writing on powershell 2 your code will return 1 even without any drives ;) 

    So i believe you need start here 

    Cannot say how much i love "other things" when someone not posting any of them ;) 


    The opinion expressed by me is not an official position of Microsoft

    PowerShell 2 is not supported and hasn't been for years. No one should be using PS 2 as it has many security issues and all modern behaviors are significantly different.  PS 2 code often fails in PS 3 and later.  Mostly it works.

    In current PowerShell 5 and later the array decoration is not required. In PS2 it won't work with all CmdLets.

    Stay tuned.


    \_(ツ)_/

    Tuesday, May 26, 2020 7:10 PM
  • Thanks this works in most of the cases, however sometimes I get a void value.

    The easier and more correct way to handle all conditions is to test for a result instead of testing for a count.

    if($MappedDrivesUnavailable = Get-SmbMapping | Where-Object{$_.Status -eq 'Unavailable'}){
        Write-Host "MappedDrivesUnavailable.count : $($MappedDrivesUnavailable.Count)"
    }else{
        Write-Host 'No results returned'
    }
    This method will directly handle all null conditions in all versions.


    \_(ツ)_/

    Tuesday, May 26, 2020 7:15 PM
  • If you put $MappedDrivesUnavailable inside a quoted string then Powershell will interpolate the variable before accessing the "count" property. To fix that, place it inside "$()" to signal that the expression is to be evaluated first.

    $MappedDrivesUnavailable = Get-SmbMapping |
        Where-Object -property Status -Value Unavailable -EQ |
            Select-Object LocalPath,RemotePath
    Write-Host "MappedDrivesUnavailable.count : $($MappedDrivesUnavailable.count)"


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, May 26, 2020 7:19 PM
  • If you put $MappedDrivesUnavailable inside a quoted string then Powershell will interpolate the variable before accessing the "count" property. To fix that, place it inside "$()" to signal that the expression is to be evaluated first.

    $MappedDrivesUnavailable = Get-SmbMapping |
        Where-Object -property Status -Value Unavailable -EQ |
            Select-Object LocalPath,RemotePath
    Write-Host "MappedDrivesUnavailable.count : $($MappedDrivesUnavailable.count)"


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Correct - if you look you will see that I already posted that fix which is not what the OP was asking about.  The formatted output would have never worked so we have to assume he was telling the truth about "Count" being null.


    \_(ツ)_/

    Tuesday, May 26, 2020 7:22 PM

  • PowerShell 2 is not supported and hasn't been for years. No one should be using PS 2 as it has many security issues and all modern behaviors are significantly different.  PS 2 code often fails in PS 3 and later.  Mostly it works.

    In current PowerShell 5 and later the array decoration is not required. In PS2 it won't work with all CmdLets.

    Stay tuned.


    \_(ツ)_/

    great, so you wrote one of 2 of my suggesstions ;)

    btw my comment was added 3 hours before yours :-D


    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Tuesday, May 26, 2020 7:49 PM
    Tuesday, May 26, 2020 7:29 PM
  • Thanks this works in most of the cases, however sometimes I get a void value.

    The easier and more correct way to handle all conditions is to test for a result instead of testing for a count.

    if($MappedDrivesUnavailable = Get-SmbMapping | Where-Object{$_.Status -eq 'Unavailable'}){
        Write-Host "MappedDrivesUnavailable.count : $($MappedDrivesUnavailable.Count)"
    }else{
        Write-Host 'No results returned'
    }
    This method will directly handle all null conditions in all versions.


    \_(ツ)_/

    haha, as usual code without testing will do rubish. Please try again later once again :)

    your code will always go to else block ;) and you r not right about Rich code which would work (even if it will do something other that author originally requested)


    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Tuesday, May 26, 2020 7:48 PM
    Tuesday, May 26, 2020 7:31 PM

  • Instead of taking this as a contest you need to understand that some criticism is intended to be helpful.  The issue you have is that "Measure-Object" cannot count nulls in a pipeline in some versions of PowerShell.  This is a known issue and has been for a long time.  The behavior noted by the OP is that of either an earlier version of PS or of a missing Net update.  I am not sure of all of the pieces.  

    By nor reacting to a criticism as if it is negative you can learn more about PS and the underlying technology.  You offered an alternate to my solution which is actually the best way to do what was asked.  Why use a pipeline when you want a single value?

    The second script I offered will overcome all issues with versions using the older method for getting results from a null return.

    The changes to PS were to eliminate singletons from a result when the result was defined as a collection.  Strings were also modified to behave this way.  Most singletons will now return 1 for an object and 0 for an empty collection.  A $null will always return 0.  This was not the case for earlier versions of PowerShell.

    My reply as intended to be informative but also challenged you to think deeper.  By thinking deeper and follwoing the trail you learn more than can be posted in a forum.


    \_(ツ)_/

    aha, so if topic starter have posh 2, your recommendation will not solve his problem, correct? (i know answer) so in this case you post something that should work on posh 5 without any modification but you cannot fidn possible rootcause and post some random solution which will not needed on posh 5 and will not help on posh 2, am i correct? (i know answer)

    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Tuesday, May 26, 2020 8:32 PM added 2, at the first sentense
    Tuesday, May 26, 2020 8:01 PM

  • Instead of taking this as a contest you need to understand that some criticism is intended to be helpful.  The issue you have is that "Measure-Object" cannot count nulls in a pipeline in some versions of PowerShell.  This is a known issue and has been for a long time.  The behavior noted by the OP is that of either an earlier version of PS or of a missing Net update.  I am not sure of all of the pieces.  

    By nor reacting to a criticism as if it is negative you can learn more about PS and the underlying technology.  You offered an alternate to my solution which is actually the best way to do what was asked.  Why use a pipeline when you want a single value?

    The second script I offered will overcome all issues with versions using the older method for getting results from a null return.

    The changes to PS were to eliminate singletons from a result when the result was defined as a collection.  Strings were also modified to behave this way.  Most singletons will now return 1 for an object and 0 for an empty collection.  A $null will always return 0.  This was not the case for earlier versions of PowerShell.

    My reply as intended to be informative but also challenged you to think deeper.  By thinking deeper and follwoing the trail you learn more than can be posted in a forum.


    \_(ツ)_/

    aha, so if topic starter have posh your recommendation will not solve his problem, correct? (i know answer) so in this case you post something that should work on posh 5 without any modification but you cannot fidn possible rootcause and post some random solution which will not needed on posh 5 and will not help on posh 2, am i correct? (i know answer)

    The opinion expressed by me is not an official position of Microsoft

    Sorry but now you are not making sense.  I suspect your English is, in part, the issue but it may also be why you misu8nderstand my posts.  I see no point in trying to continue.

    The OP can chose the solution.  Debate is pointless.


    \_(ツ)_/

    Tuesday, May 26, 2020 8:06 PM
  •   I see no point in trying to continue.

    The OP can chose the solution.  Debate is pointless.


    \_(ツ)_/

    i do not want debates with you (when i do not ask this directly), but please fix your code to be more helpful.

    your if statement will be always eq null on every posh version (ps2, 3, 5 or even 7)

    sorry for my english, i do not have enough practice last time :(


    The opinion expressed by me is not an official position of Microsoft

    • Edited by Vector BCO Tuesday, May 26, 2020 8:33 PM
    Tuesday, May 26, 2020 8:30 PM
  • Well, sure . . . by three minutes? I didn't refresh my view after reading the question but before submitting my post. My bad, I guess. :-(


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, May 26, 2020 9:35 PM
  • You guys have fun . . . I'm out!

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, May 26, 2020 9:37 PM