gpt4 book ai didi

powershell - 字符串Replace()/Remove()不起作用

转载 作者:行者123 更新时间:2023-12-03 00:46:39 25 4
gpt4 key购买 nike

我想删除一部分用于搜索和分组的字符串,否则它将无法搜索,但是Replace() / Remove()无法正常工作:

    $RootPath = $Selection
$Folders = dir $RootPath | where {$_.PSIsContainer -eq $true}
try {
foreach ($Folder in $Folders) {
$ACLs = Get-Acl $Folder.FullName | ForEach-Object {
$_.Access
}
foreach ($ACL in $ACLs) {
if ($ACL.IdentityReference -notlike "Administrators" -and $ACL.IdentityReference - notlike "Creator Owner" - and $ACL.IdentityReference -notlike "BUILTIN\Administrators" -and $ACL.IdentityReference -notlike "NT AUTHORITY\SYSTEM" -and $ACL.IdentityReference -notlike "System") {
$strAcl = $ACL.IdentityReference
# value is for instance GESCOEUROPE\GR_G-FCASB-INT-ALL
}
}
}

$strNames = $strAcl.Replace("GESCOEUROPE\", "")
# or:
#$strNames = $strAcl.Remove(0, 12)

$strUsers = Get-ADGroupMember -Identity $strNames -Recursive |
Get-ADUser -Property DisplayName |
Select Name |
Sort-Object Name
$OutInfo = $Folder.FullName + "," + $trAcl + $strUsers
$OutInfo | Select-Object -Unique
Add-Content -Value $OutInfo -Path $OutFile | Sort-Object -Unique
} catch [System.IO.IOException] {
}
}

最佳答案

您将$strAcl设置为$ACL.IdentityReference,其类型为[System.Security.Principal.NTAccount](使用 Get-Member 验证);它不是字符串,因此无法在其上调用.Replace()

如果要字符串化$ACL.IdentityReference,请在其上调用.ToString(),或将其强制转换为[string],或将其括在双引号字符串内的表达式中:

# Call .ToString()
$strAcl = $ACL.IdentityReference.ToString()

# Cast to [string]
$strAcl = [string] $ACL.IdentityReference

# Use string interpolation:
$strAcl = "$($ACL.IdentityReference)"

注意:在这种情况下,所有3种方法都是等效的,但在某些情况下 .ToString()对文化敏感[1]
,而其他两种方法始终对文化不敏感。

[1]试试 [cultureinfo]::CurrentCulture = 'de-DE'; (0.5).ToString(); [string] 0.5; "$(0.5)"

关于powershell - 字符串Replace()/Remove()不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42973889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com