我有这个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 http://stackoverflow.com/questions/10286164/powershell-function-return-value
$all_sids | foreach-object { $_.Substring(20) }
}