我正在尝试编写一个脚本来查看用户以及他们所属的 AD 组,寻找具有多个“子组”的特定组。 例如 VPN-GRP-ONE、VPN-GRP-2、VPN-GRP-3...
尝试使用我在一些演示中找到的一些东西,但它无法正常工作,因为它希望导入 ActiveDirectory 模块以使用 get-aduser 并且我们不允许安装我们尚未拥有的新模块。 (我的可用模块中没有 ActiveDirectory)
我正在尝试使用:
$list1 = C:\Users\MrAoxx\Documents\List1.txt
foreach ($_ in $list1) {
net user $_ /domain}
我希望得到的输出可以采取下一步,将其传输到一个新的文本文件,并开始从中剥离我需要的内容,以获得我正在寻找的 AD 组名称,即:一、二、三。但它所做的只是打开 txt 文件,没有其他任何事情。
我看到您已经接受了答案,但是,这里还有其他方法可以减轻您的这种努力。那么,至于这个……
--- '(我的可用模块中没有 ActiveDirectory)' ---
---我们不可以安装东西---
...您是否需要在系统上实际安装/启用它们才能使用它们。这就是隐式 PSRemoting 的用途,或者使用内置的 .Net 命名空间或 adsisearcher。
如何与他们中的任何一个联系:
使用 PowerShell Active Directory Cmdlet 无需安装任何软件
Enter-PSSession -ComputerName dc1 –credential nwtraders\administrator
Set-Location c:\
Import-Module activedirectory
Powershell 远程使用模块命令行开关(远程导入模块)
# 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
使用 PowerShell ADSI 适配器使用 Active Directory
# 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()
这并不有趣,但你可以 - 请注意,足够长的组名称绝对有可能会被截断:
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.
}
我将引导您了解其工作原理:
net user
输出*
字符分割文本的其余部分。这种花哨的解析是您应该选择安装 RSAT 工具的原因。以下是使用
Get-ADUser
的方法:
Get-Content C:\Users\MrAoxx\Documents\List1.txt | Foreach-Object {
$groups = ( ( Get-ADUser $_ -Property MemberOf ).MemberOf | Get-AdGroup ).Name
# Process here
}
$list1 = get-content 'C:\Users\MrAoxx\Documents\List1.txt'
foreach ($_ in $list1) {
net user $_ /domain >> C:\Users\MrAoxx\Documents\FullList.txt}
这按照我需要的方式工作,感谢@LotPings 的回答。