0

I have this PowerShell version 2 function ...

function Get-Sids{
    #[CmdletBinding()]
    param ([string]$all_sids)

    $all_sids | foreach-object { $_.Substring(20) }
    return $all_sids
}

The substring method is removing the first 20 characterss of the string like I want it to. The problem is it is only doing it on the first element of the array.

Example input

$all_sids = "000000000000000000testONE", "000000000000000000testTwo", "000000000000000000testThree"

output

stONE 000000000000000000testTwo 000000000000000000testThree

I shouldn't need to move to the next element in the array, right? What am I missing?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hemi81
  • 578
  • 2
  • 15
  • 34

1 Answers1

1

You explicitly called the parameter a single String. You need to set it as an array like so:

function Get-Sids{
    #[CmdletBinding()]
    param (
        # Note the extra set of braces to denote array
        [string[]]$all_sids
    )

    # Powershell implicitly "returns" anything left on the stack
    # See http://stackoverflow.com/questions/10286164/powershell-function-return-value
    $all_sids | foreach-object { $_.Substring(20) }
}
Eris
  • 7,378
  • 1
  • 30
  • 45
  • That was it! I had to do the same to my variable, as well, because I was set as a single string value. Thanks @Eris – Hemi81 Aug 17 '15 at 16:25
  • 1
    I realized the `return` was incorrect, so I updated the answer. – Eris Aug 17 '15 at 16:27