如果字符串在 PowerShell 中以

If strings starts with in PowerShell

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

我们正在检查 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
    }
}

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

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