Hemi81
Hemi81

Reputation: 588

PowerShell function

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?

Upvotes: 0

Views: 113

Answers (1)

Eris
Eris

Reputation: 7638

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

Upvotes: 1

Related Questions