Powershell:如果字符串以

Joc*_*Sch 27 powershell if-statement startswith

有没有办法检查,如果一个字符串以字符串开头?

我们正在检查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
    }
}
Run Code Online (Sandbox Code Playgroud)

M.G*_*M.G 39

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

改变$Group.StartsWith("S_G_")$Group.samaccountname.StartsWith("S_G_")