使用列表作为变量的PowerShell命令

xzv2uavs  于 9个月前  发布在  Shell
关注(0)|答案(3)|浏览(132)

我试图写一个脚本来看看用户和他们是什么广告组的一部分,寻找一个特定的组,有多个“子组”.例如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}

字符串
我希望输出,然后我可以采取下一步管道,以一个新的文本文件,并开始剥离我需要它得到的只是广告组的名称,我正在寻找即:一,二,三。但它所做的一切就是打开txt文件,没有别的。

qhhrdooz

qhhrdooz1#

我看到你已经接受了一个答案,然而,这里有其他的方法来减轻你的这种努力。所以,至于这个......
--- '(我的可用模块中没有ActiveDirectory)' ---

  • 我们不允许安装东西-
    .你甚至需要在你的系统上实际安装/启用它们才能使用它们。这就是隐式PSRemoting的用途,和/或使用内置的.Net命名空间或adsiphon。
  • 如何我们任何一个:*

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()

g52tjvyc

g52tjvyc2#

这并不有趣,但在这里你去-注意,这是绝对可能的,足够长的组名称将被截断:

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输出
1.选择所有包含 * 的行,并将输出转换为字符串,删除前导和尾随空格。
1.为了便于处理,将输出重新拆分为每行一个数组。
1.对于输出的每一行,删除前29个字符,并通过*字符分割文本的其余部分。
1.从最终数组输出中删除所有空字符串
这就是为什么你应该选择installing RSAT tools的原因。下面是你如何使用Get-ADUser

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

  # Process here
}

kwvwclae

kwvwclae3#

$list1 = get-content 'C:\Users\MrAoxx\Documents\List1.txt'
foreach ($_ in $list1) {
net user $_ /domain >> C:\Users\MrAoxx\Documents\FullList.txt}

字符串
这是我需要的方式,感谢@LotPings的答案。

相关问题