【问题标题】:Trying to script RemotePowershellEnabled $false to all users except for a couple in specific admin groups尝试为所有用户编写 RemotePowershellEnabled $false 脚本,特定管理员组中的几个用户除外
【发布时间】:2022-10-14 22:14:00
【问题描述】:

我正在尝试设置一个 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}

【问题讨论】:

  • 目前你有一个错字:-inotin 应该是:-notin。不知道这些变量中有什么很难帮助...
  • 我从其他地方拉了它,并假设它应该代表“不在”,但我也尝试将它作为 -notin 以及仍然没有运气。一旦我摆脱了公司的具体细节,我会添加代码

标签: powershell automation


【解决方案1】:

您的代码中有一些拼写错误,例如$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}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 2016-02-21
    • 1970-01-01
    相关资源
    最近更新 更多