Trailing spaces and hard returns

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Stuart Fleck Stuart Fleck 1 year, 1 month ago.

  • Author
    Posts
  • #33224
    Profile photo of Stuart Fleck
    Stuart Fleck
    Participant

    When running the following

    $RAW = Get-ciminstance -classname win32_share | select path  
    

    and then pass the results into a foreach such as

    foreach ($Share in $Raw)
    {
     Get-Acl $share   
    }
    

    I get an error such as Cannot find drive.

    After a bunch of banging my head and testing the results individually, I discovered that there were trailing spaces and hard returns in the output that were making the variable invalid.

    I fixed the issue by the following

     $RAW = Get-ciminstance -classname win32_share | select path | ForEach-Object {$_.path}
    foreach ($Share in $Raw)
    {
     Get-Acl $share   
    }
    

    Is their any way to know that the output contains trailing spaces? I did not see the issue until I sent the results to a text file and saw the carriage returns and the then while clicking around noticed that there were trailing spaces as well.

    I suppose that I can add the Foreach-Object {$_. } but i was wondering if there were any other ways to address this issue.

  • #33226
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    In your select cmdlet, use -expandproperty.

  • #33231
    Profile photo of Stuart Fleck
    Stuart Fleck
    Participant

    @ Bob Thank you. I like the easy answers

    Get-ciminstance -classname win32_share | select path -ExpandProperty path
    

    It worked like a charm

    Desired result

    $RAW = Get-ciminstance -classname win32_share | select path -ExpandProperty path
    foreach ($Share in $Raw)
    {
     $Permissions = Get-Acl $share
     $Permissions.AccessToString   
    }
    

You must be logged in to reply to this topic.