PowerShell 函数

PowerShell function

我有这个 PowerShell 版本 2 函数...

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

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

substring 方法正在删除字符串的前 20 个字符,就像我想要的那样。问题是它只对数组的第一个元素执行此操作。

示例输入

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

输出

stONE 000000000000000000testTwo 000000000000000000testThree

我不需要移动到数组中的下一个元素,对吧?我错过了什么?

您明确将参数称为单个 String。您需要将其设置为这样的数组:

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 
    $all_sids | foreach-object { $_.Substring(20) }
}