简体   繁体   English

对Active Directory中的所有计算机运行Powershell脚本

[英]Running a powershell script against all the computers in Active Directory

I have written a script that generates some information but I need to run it on over 160 computers in an active directory. 我已经编写了一个脚本,该脚本可以生成一些信息,但是我需要在活动目录中的160多台计算机上运行它。 I don't want to run this script on each machine individually, is there any way of executing this script all all the machines from one centralized location? 我不想在每台计算机上单独运行此脚本,是否可以从一个集中位置在所有计算机上执行此脚本?

invoke-command -ComputerName test-pc -ScriptBlock {gwmi win32_service | Select-object Name, PathName | where-object {$_.PathName -notlike '"*' -and $_.PathName -like "*\* *\*"}}

I don't want to be logging on to each machine individually. 我不想单独登录每台计算机。 Is there quicker way? 有更快的方法吗? There must be. 必须有。

Any help will be much appreciated. 任何帮助都感激不尽。

Sohail. 苏海尔。

Updated version: 更新后的版本:

invoke-command -ComputerName @(Get-ADComputer -Filter {Name -like "GBST*"} | Select-Object Name)  -ScriptBlock {gwmi win32_service | Select-object Name, PathName | where-object {$_.PathName -notlike '"*' -and $_.PathName -like "*\* *\*"}}

Error message: 错误信息:

invoke-command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects 
instead of strings.
At line:1 char:2
+     invoke-command -ComputerName @(Get-ADComputer -filter {Name -like "GBSU1*"} | S ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
    + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand

Just to make it shorter : 只是为了使其更简短:

(Get-ADComputer -Filter 'Name -like "GBSU*"').Name | % {
    Get-WMIObject Win32_Service |
    Select-Object Name, PathName |
    Where-Object { $_.PathName -notlike '"*' -and $_.PathName -like "** **" }
}

As pointed out by @bluuf : 正如@bluuf指出的那样:

This is actually not a 'good' solution since Get-WmiObject supports the ComputerName parameter : Invoke-Command shouldn't be used at all in this case. 实际上,这不是一个好的解决方案,因为Get-WmiObject支持ComputerName参数:在这种情况下,根本不应该使用Invoke-Command

There is a syntax error in your revised example where you kept the "*" in the Filter. 在修改后的示例中存在语法错误,您在过滤器中保留了“ *”。 You can't filter for all then narrow your results. 您无法过滤所有内容,然后缩小结果范围。 Asterisk is implied when filtering based on properties. 基于属性过滤时暗含星号。

I also added the ".Name" property to the computer search to only pass that to the invoke-command section. 我还向计算机搜索添加了“ .Name”属性,以仅将其传递给invoke-command部分。 I tried the following code and it seemed to work. 我尝试了以下代码,它似乎可以工作。

invoke-command -ComputerName @((Get-ADComputer -Filter 'Name -like "GBST*"').Name) -ScriptBlock {gwmi win32_service | Select-object Name, PathName | where-object {$_.PathName -notlike 'C:\Windows\system32\*' -and $_.PathName -like 'C:\Program Files\*'}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM