【发布时间】:2022-01-26 06:54:04
【问题描述】:
我想比较用户的组和 DFS 的组,看看用户是否有访问 DFS 的权限。但我陷入了拆分字符串。
这是我的代码的一部分:
$folder = Read-Host 'Enter the folder path'
$User = Read-Host 'Enter the sAMAccountName of user'
$getSGs = (get-acl $folder).Access | Select-Object -uniq -ExpandProperty IdentityReference
$getSGsSplit = $getSGs -split('.*\\')
$arr = $getSGsSplit -split ' '
Write-Host $arr
$getSGs的值:
domain-ORG\xxx-Read domain-ORG\xxx-Modify domain-ORG\xxx-Admin domain-ORG\xxx-Center domain-ORG\xxxx-Admin BUILTIN\Administrators
$getSGsSplit 的值:
xxx-Read xxx-Modify xxx-Admin xxx-Center xxxx-Admin Administrators
我想要的是用空格分割 $getSGsSplit:
xxx-Read
xxx-Modify
xxx-Admin
xxx-Center
xxxx-Admin
BUILTIN\Administrators
但是我已经尝试了很多模式,它们都不起作用。
$getSGsSplit -split ' .-`t`n`r'
$getSGsSplit -replace ' ','\n'
$getSGsSplit.split(' ').trim()
$getSGsSplit.split('\s').trim()
$getSGsSplit -split ' '
无论我使用哪种模式,write-host $arr[2] 的控制台仍然是空格。 write-host $arr[2].getType() 的控制台总是 System.String
PS C:\Users\zzz\Desktop> C:\Users\zzz\Desktop\12.ps1
Enter the folder path: \\domain.org\xxx\xx\xxx
Enter the sAMAccountName of user: zzz
PS C:\Users\zzz\Desktop>
谁能建议如何解决这个问题?
【问题讨论】:
-
($getSGsSplit -split '\s').Where({ $_ -ne '' })。您的.split与\S结合使用有一个好主意,但.net 方法的.split参数不采用正则表达式值。 Powershell-Split运算符虽然可以。
标签: powershell replace split