【问题标题】:Powershell command using list as variable使用列表作为变量的 Powershell 命令
【发布时间】:2021-09-20 19:42:49
【问题描述】:

我正在尝试编写一个脚本来查看用户及其所属的 AD 组,寻找具有多个“子组”的特定组。 例如 VPN-GRP-ONE、VPN-GRP-TWO、VPN-GRP-THREE....

尝试使用我在一些演示中找到的一些东西,但它无法正常工作,因为它希望导入 ActiveDirectory 模块以使用 get-aduser,并且我们不允许安装我们还没有的新模块。 (我的可用模块中没有 ActiveDirectory)

我正在尝试使用:

$list1 = C:\Users\MrAoxx\Documents\List1.txt
foreach ($_ in $list1) {
net user $_ /domain}

我希望输出结果是我可以采取下一步将其通过管道传输到一个新的文本文件并开始从中剥离我需要的内容以获取我正在寻找的 AD 组名称,即:ONE、TWO、三。但它所做的只是打开 txt 文件而已。

【问题讨论】:

  • 我强烈建议您为您的操作系统安装 RSAT 并利用随它安装的 ActiveDirectory 模块。对于任何 Windows 管理员来说,这是一个非常标准的工具集,尤其是对于 AD 角色。
  • 我们不允许安装未经公司 INFOSEC 小组审查的东西,这使我们非常有限。
  • RSAT 与用于系统管理工作的工具一样标准。这不是一些第三方工具,这是微软提供的标准系统管理工具包。如果您要求以 Windows syadmin 身份安装它并因为“Infosec”没有审查该工具而被拒绝,我会感到非常惊讶。
  • $list1 = Get-Content 'C:\Users\MrAoxx\Documents\List1.txt'
  • 使用net user $_ /domain该命令的问题是它会截断组成员部分中的组名称。不幸的是,现在是 2019 年,与用于自省 AD 的 net user 命令相比,我更擅长 cmdlet,并且不知道是否有一些命令不会截断组成员资格。

标签: list powershell active-directory


【解决方案1】:

我看到你已经接受了一个答案,但是,这里有其他方法可以为你减轻这种努力。那么,至于这个……

--- '(我的可用模块中没有 ActiveDirectory)' ---

---我们不允许安装东西---

...您甚至需要在系统上实际安装/启用它们才能使用它们。这就是 Implicit PSRemoting 的用途,或者使用内置的 .Net 命名空间或 adsisearcher。

我们如何处理他们中的任何一个:

Use PowerShell Active Directory Cmdlets Without Installing Any Software

Enter-PSSession -ComputerName dc1 –credential nwtraders\administrator
Set-Location c:\
Import-Module activedirectory

Powershell Remote Use of Module Commandlets (Remoting Import-Module)

# Create a Powershell remote session to a server with the #commandlets installed.
$Session = New-PSsession -Computername Server1

# Use the newly created remote Powershell session to send a #command to that session
Invoke-Command -Command {Import-Module ActiveDirectory} -Session $Session

# Use that session with the modules to add the available 
# commandlets to your existing Powershell command shell with a 
# new command name prefix.
Import-PSSession -Session $Session -Module ActiveDirectory -Prefix RM

Working with Active Directory using PowerShell ADSI adapter

# Searching for an object
$Searcher = New-Object DirectoryServices.DirectorySearcher 
$Searcher.Filter = '(&(objectCategory=person)(anr=gusev))'
$Searcher.SearchRoot = 'LDAP://OU=Laptops,OU=Computers,DC=contoso,DC=com'
$Searcher.FindAll()

【讨论】:

  • 导入功能需要我们没有的权限,而获取我的凭据的例外情况需要提出请求并解释为什么这是绝对需要而不是可以提供帮助的东西。
【解决方案2】:

这并不好玩,但请注意 - 请注意,足够长的组名绝对有可能被截断:

Get-Content C:\Users\MrAoxx\Documents\List1.txt | Foreach-Object {
  $partOfGroups = ( ( net user $_ /domain | select-string '\*' | out-string ).Trim() -split "`r`n" ) |
    Foreach-Object { $_.Substring(29).Trim() -split '\*' } |
    Where-Object { -Not [String]::IsNullOrWhiteSpace($_) }

  # You can look for specific groups in $partOfGroups if that user is part
  # of any particular group, and process for that user here.
}

我将引导您了解其工作原理:

  1. 获取当前用户从文件中读取的net user 输出
  2. 选择所有包含 * 的行并将输出转换为字符串,修剪前导和尾随空格。
  3. 为了便于处理,将输出重新拆分为每行的数组。
  4. 对于每行输出,删除前 29 个字符,并用 * 字符分割其余文本。
  5. 从最终数组输出中删除所有空字符串

这种花式解析是您应该选择安装 RSAT 工具的原因。以下是使用 Get-ADUser 的方法:

Get-Content C:\Users\MrAoxx\Documents\List1.txt | Foreach-Object {
  $groups = ( ( Get-ADUser $_ -Property MemberOf ).MemberOf | Get-AdGroup ).Name

  # Process here
}

【讨论】:

  • 这也为我工作并删除了非 AD 组信息,谢谢。
【解决方案3】:
$list1 = get-content 'C:\Users\MrAoxx\Documents\List1.txt'
foreach ($_ in $list1) {
net user $_ /domain >> C:\Users\MrAoxx\Documents\FullList.txt} 

感谢@LotPings 的回答,这符合我的需要。

【讨论】:

    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    相关资源
    最近更新 更多