PowerShell从服务器获取用户,组

时间:2019-05-22 09:27:35

标签: powershell

我有一个PowerShell脚本,该脚本从Windows Server获取所有用户和组数据。但是该脚本必须在每台服务器上运行,因为我找不到在不同服务器上运行的-ComputerName选项。因此必须修改该脚本,并且当我们基于服务器列表输入从一台服务器运行该脚本时,它应该从Windows服务器获取所有数据。

1 个答案:

答案 0 :(得分:0)

如果您有一个脚本甚至可以在一台计算机上执行此操作,则可以使用ForEach在任意数量的计算机上执行该脚本。

'computer1',computer2','computer3' | 
ForEach {
    # put your code here 
    # and for the computername property, use $PSItem to get the name being passed in
}

您只需使用环境变量

即可获取运行代码的当前计算机名称。
$env:COMPUTERNAME

或仅使用

hostname

您没有在做的就是向我们显示您的代码,因此我们不知道您在做什么。 许多cmdlet / WMI和CIM类都有计算机名称参数,因此,如果看不到该参数,则说明您没有使用具有此功能的cmdlet /功能。

这还表明您对PowerShell还是非常陌生的,我建议您花些时间来加速。使用YouTube,MSDN Channe9或MS了解资源并搜索...

  • “入门PowerShell”
  • “中级PowerShell”
  • “高级Powershell”
  • “ PowerShell运行命令远程计算机”

始终首先使用帮助文件。

Get-Command -Name '*Computer*' | Format-Table -AutoSize

# Results

CommandType Name                                        Version      Source                                              
----------- ----                                        -------      ------                                                                                
Cmdlet      Add-ADComputerServiceAccount                1.0.1.0      ActiveDirectory                                     
Cmdlet      Add-Computer                                3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Add-WsusComputer                            2.0.0.0      UpdateServices                                      
Cmdlet      Checkpoint-Computer                         3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Disable-ComputerRestore                     3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Enable-ComputerRestore                      3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Get-ADComputer                              1.0.1.0      ActiveDirectory                                     
Cmdlet      Get-ADComputerServiceAccount                1.0.1.0      ActiveDirectory                                     
Cmdlet      Get-ComputerInfo                            3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Get-ComputerRestorePoint                    3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Get-WsusComputer                            2.0.0.0      UpdateServices                                      
Cmdlet      New-ADComputer                              1.0.1.0      ActiveDirectory                                     
..                       
Cmdlet      Remove-ADComputer                           1.0.1.0      ActiveDirectory                                     
Cmdlet      Remove-ADComputerServiceAccount             1.0.1.0      ActiveDirectory                                     
Cmdlet      Remove-Computer                             3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Rename-Computer                             3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Reset-ComputerMachinePassword               3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Restart-Computer                            3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Restore-Computer                            3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Set-ADComputer                              1.0.1.0      ActiveDirectory                                     
Cmdlet      Stop-Computer                               3.1.0.0      Microsoft.PowerShell.Management                     
Cmdlet      Test-ComputerSecureChannel                  3.1.0.0      Microsoft.PowerShell.Management                     


# Find all cmdlets / functions with a target parameter
# Ignore any error show, as that just means the object does not have it
Get-Command -CommandType Function | 
Where-Object { $_.parameters.keys -match 'computer'} | 
Out-GridView -PassThru -Title 'Available functions which has a specific parameter'

Get-Command -CommandType Cmdlet | 
Where-Object { $_.parameters.keys -match 'computer'}| 
Out-GridView -PassThru -Title 'Results for cmdlets which has a specific parameter'

# Results (truncated for brevity)

CommandType     Name                 Version    Source
-----------     ----                 -------    ------
Cmdlet          Add-Computer         3.1.0.0    Microsoft.PowerShell.Management     
Cmdlet          Connect-PSSession    3.0.0.0    Microsoft.PowerShell.Core           
Cmdlet          Connect-WSMan        3.0.0.0    Microsoft.WSMan.Management          
Cmdlet          Disconnect-WSMan     3.0.0.0    Microsoft.WSMan.Management          
Cmdlet          Enter-PSSession      3.0.0.0    Microsoft.PowerShell.Core           
Cmdlet          Get-CimInstance      1.0.0.0    CimCmdlets                          
Cmdlet          Get-Process          3.1.0.0    Microsoft.PowerShell.Management     
Cmdlet          Get-EventLog         3.1.0.0    Microsoft.PowerShell.Management     
Cmdlet          Get-Service          3.1.0.0    Microsoft.PowerShell.Management     
Cmdlet          Get-WmiObject        3.1.0.0    Microsoft.PowerShell.Management     
Cmdlet          Invoke-CimMethod     1.0.0.0    CimCmdlets                          
Cmdlet          Invoke-Command       3.0.0.0    Microsoft.PowerShell.Core           
Cmdlet          Invoke-WmiMethod     3.1.0.0    Microsoft.PowerShell.Management     
Cmdlet          Stop-Computer        3.1.0.0    Microsoft.PowerShell.Management     
Cmdlet          Test-Connection      3.1.0.0    Microsoft.PowerShell.Management

...



# get function / cmdlet details
Get-Command -Name Get-CimInstance -Syntax

(Get-Command -Name Get-WMIObject).Parameters.Keys

 # Results
...
 Credential
 ThrottleLimit
 ComputerName
 Namespace
...

(Get-Command -Name Get-CimInstance).Parameters.Keys

# Results

CimSession
ClassName
...
ComputerName
...
Property
...

Get-help -Name Get-CimInstance -Full
Get-help -Name Get-CimInstance -Online
Get-help -Name Get-CimInstance -Examples

Examples:

...
Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName Server01,Server02
...
$s = New-CimSession –ComputerName Server01,Server02
...
相关问题