如果字符串在PowerShell中以开头 [英] If strings starts with in PowerShell
本文介绍了如果字符串在PowerShell中以开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以检查字符串是否以字符串开头?
Is there a way to check if a string starts with a string?
我们正在从 AD 用户中检查组成员身份.我们的广告组如下所示:S_G_share1_W
We are checking the groupmembership from the AD user. Our AD groups look like this: S_G_share1_W
仅当组名以"S_G_"
开头时,才可以运行用于连接网络共享的脚本,因为我们还有其他一些组.
The script for connecting the networkshares should only run if the groupname starts with "S_G_"
, because we have some other groups too.
$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
is an object, but you will actually need to check if $Group.samaccountname.StartsWith("string")
.
将$Group.StartsWith("S_G_")
更改为$Group.samaccountname.StartsWith("S_G_")
.
这篇关于如果字符串在PowerShell中以开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文