您好,我目前正在研究脚本以监视回RMM工具,似乎在转换对象以匹配脚本中的“已知字符串”时遇到问题。
理想情况下,我想轮询本地计算机的本地管理员组,然后将其与我预定义的字符串进行内联比较,我希望获得该值,然后只写一个多行字符串进行匹配,然后执行一些操作比较2的语句。
$test3 = Get-LocalGroupMember -SID "S-1-5-32-544" | select -ExpandProperty Name | out-string
$test =@"
PC\Administrator
PC\test
"@
这是一个小片段,因此第一个提取本地广告组,然后将其保存到变量中,而$ test是我定义的变量。
两者输出到控制台时看起来都相同。
非常感谢。
这是Windows中的一个错误,其中孤立的SID被保留在组中。请尝试以下方法:
$adminGroup = [ADSI]::new("WinNT://$env:COMPUTERNAME/$((Get-LocalGroup -SID S-1-5-32-544).Name)")
$adminGroupMembers = $adminGroup.Invoke('Members') |% {([ADSI]$_).Path.Replace('WinNT://', '')}
$adminGroupMembers | Out-String
您需要根据需要操纵输出。
而不是使用预定义的多行字符串,请使用字符串数组或哈希表进行比较。您尝试执行此操作的方式可能会导致比较失败,这仅是因为返回的项可能与预定义字符串中的顺序不同。
选项1:使用数组
$testUsers = 'PC\Administrator', 'PC\test'
# this gets the users that are mentioned in the $testUsers array.
# if you want the opposite (users in the group, but NOT in the $testUsers array),
# change '-contains' into '-notcontains'
(Get-LocalGroupMember -SID "S-1-5-32-544").Name | Where-Object { $testUsers -contains $_ }
选项2:使用哈希表(设置工作较多,但速度非常快)
$testusers = @{
'PC\Administrator' = $true # the Values will not be used, so anything can go in here
'PC\test' = $true
}
# this gets the users that are mentioned in the $testUsers Hashtable.
# if you want the opposite (users in the group, but NOT in the $testUsers Hashtable),
# change '$testUsers.ContainsKey($_)' into '!$testUsers.ContainsKey($_)'
(Get-LocalGroupMember -SID "S-1-5-32-544").Name | Where-Object { $testUsers.ContainsKey($_)