我正在尝试设置一个powershell脚本来自动运行一个命令,以获取所有拥有"Remotepowershellenabled"$为True,然后将该列表与2个管理组进行比较。之后,我希望它设置所有不属于2个管理员帐户的用户帐户,然后将该选项设置为$false。当我到达脚本的这一部分时,新变量没有填充任何内容。我知道变量$UserswithRemotePS中列出的一些用户不在$DomainAdmin变量中。
$UsersNotDA = $UserswithRemotePS | where {$_.samaccountname -inotin $DomainAdmin}
我把这个剧本放在一起,看了其他几个类似的剧本,所以我显然错过了一些东西。如有任何帮助,我们将不胜感激。
$DomainAdmins = (Get-ADGroupMember -Identity "Domain Admins").samaccountname|out-string
$Exchangeadmins = (Get-ADGroupMember -Identity "ExchangeAdmins").samaccountname|out-string
Get-PSSession|Remove-PSSession
$ExchangePSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeServer/PowerShell/ -Authentication Kerberos
$UserswithRemotePS = (Invoke-Command -Session (Get-PSSession) {Get-User -ResultSize Unlimited -Filter 'RemotePowerShellEnabled -eq $true'}).samaccountname|out-string
$UsersNotDA = $UserswithRemotePS | where {$_.samaccountname -notin $DomainAdmin}
您的代码中有一些拼写错误,如$DomainAdmins
,稍后您将使用-notin $DomainAdmin
(请注意其中缺少s
(,但最值得注意的是您使用Out-String
破坏数组时犯的错误。
这将使数组成为单个字符串,其中运算符-notin
用于搜索数组中不包含的项。
此外,通过在此处使用单引号:'RemotePowerShellEnabled -eq $true'
,$true将不会按照您的意愿进行计算,因为现在的值正是字符串'$true'
。为此,您需要双引号。
尝试
# get arrays of SamAccountNames (so do not pipe to Out-String!!)
$DomainAdmins = (Get-ADGroupMember -Identity "Domain Admins").SamAccountName
$Exchangeadmins = (Get-ADGroupMember -Identity "ExchangeAdmins").SamAccountName
Get-PSSession|Remove-PSSession
$ExchangePSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeServer/PowerShell/ -Authentication Kerberos
$UserswithRemotePS = Invoke-Command -Session $ExchangePSSession -ScriptBlock {
(Get-User -ResultSize Unlimited -Filter "RemotePowerShellEnabled -eq $true").SamAccountName
}
$UsersNotDA = $UserswithRemotePS | Where-Object {$_.SamAccountName -notin $DomainAdmins}