问题描述
我已经通过GPO在所有台式机上配置了winrm,因此现在可以使用invoke-command
cmdlet在远程计算机上本地运行命令.当我在本地计算机上运行net localgroup administrators
时,它可以工作并提供我想要的东西.问题是我无法使用此数据做任何事情.我无法将结果传递给变量,因此可以说删除特定帐户.
I've configured winrm on all my desktops via GPO, so I can now use the invoke-command
cmdlet to run commands locally on remote machines. When I run net localgroup administrators
on my local machine this works and gives me what I want. The problem is I cannot do anything with this data. I cannot pipe out the results to a variable so I can lets say remove specific accounts.
是否有内置cmdlet,可以让我做与net localgroup administrators
相同的操作?
Is there a built in cmdlet that will let me do the same as net localgroup administrators
?
推荐答案
虽然可以运行net localgroup groupname
并解析其输出,但这并不是非常PoSh的方法.我建议使用 WinNT提供程序代替:
While it's possible to run net localgroup groupname
and parse its output, it isn't a very PoSh way of doing this. I'd recommend using the WinNT provider instead:
$computers = 'HostA', 'HostB', 'HostC', ...
$groupname = 'Administrators'
$computers | % {
$group = [ADSI]("WinNT://$_/$groupname,group")
$group.PSBase.Invoke('Members') | % {
$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
}
}
如果要使用Invoke-Command
,则可以执行以下操作:
If you want to use Invoke-Command
you could do something like this:
$computers = 'HostA', 'HostB', 'HostC', ...
$groupname = 'Administrators'
Invoke-Command -Computer $computers -ScriptBlock {
$group = [ADSI]("WinNT://$env:COMPUTERNAME/$($args[0]),group")
$group.PSBase.Invoke('Members') | % {
$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
}
} -ArgumentList $groupname
这篇关于在Powershell中等效的net localgroup管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!