gpt4 book ai didi

powershell - 针对 Active Directory 中的所有计算机运行 powershell 脚本

转载 作者:行者123 更新时间:2023-12-02 23:12:28 24 4
gpt4 key购买 nike

我已经编写了一个生成一些信息的脚本,但我需要在事件目录中的 160 多台计算机上运行它。我不想在每台计算机上单独运行此脚本,有什么方法可以从一个集中位置的所有计算机上执行此脚本吗?

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

我不想单独登录每台计算机。有没有更快的方法?一定有。

任何帮助将不胜感激。

索海尔。

更新版本:

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 "*\* *\*"}}

错误消息:

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

最佳答案

只是为了让它更短:

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

正如@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.

关于powershell - 针对 Active Directory 中的所有计算机运行 powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33783835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com