如果字符串以PowerShell开头

时间:2016-02-26 14:40:39

标签: powershell if-statement startswith

有没有办法检查字符串是否以字符串开头?

我们正在检查AD用户的群组成员身份。我们的AD群组如下所示:S_G_share1_W

连接网络共享的脚本只应在组名以"S_G_"开头时运行,因为我们也有其他组。

$GroupArray = Get-ADPrincipalGroupMembership $env:USERNAME | select samaccountname

foreach ($Group in $GroupArray) {

    if ($Group.StartsWith("S_G_")) {

        $Group = $Group -replace "S_G_", $FileServerRV
        Write-Host $Group

        $Group = $Group.Substring(0, $Group.Length-2)
        Write-Host $Group

        #erstellen des Anzeigennames
        $Groupname = $Group.Replace($FileServerRV, "")
        Write-Host "Call Function with parameter "$Group $Groupname
    }
}

1 个答案:

答案 0 :(得分:29)

$Group是一个对象,但实际上您需要检查是否$Group.samaccountname.StartsWith("string")

$Group.StartsWith("S_G_")更改为$Group.samaccountname.StartsWith("S_G_")