[英]PowerShell Script to pull servers in AD and then search SERVICES running under the Local Administrator account
New to PowerShell, attempting to cobble scripts together to: PowerShell 的新手,试图将脚本拼凑在一起以:
Can anyone guide me...prefer to export out to a CSV file, etc.任何人都可以指导我......更喜欢导出到 CSV 文件等。
THANK YOU!谢谢你!
THIS IS WHAT I HAVE:这就是我所拥有的:
Import-Module ActiveDirectory
$Serverlist = Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' `
-Properties Name
Sort-Object -Property Name |
foreach ($Server in $Serverlist) {
$Server
Get-WmiObject Win32-Service | Select DisplayName, StartName | Where-Object {$_.StartName -eq "administrator"}
GETTING THESE ERRORS:得到这些错误:
At line:5 char:18
+ foreach ($Server in $Serverlist) {
+ ~~
Unexpected token 'in' in expression or statement.
At line:5 char:17
+ foreach ($Server in $Serverlist) {
+ ~
Missing closing ')' in expression.
At line:5 char:32
+ foreach ($Server in $Serverlist) {
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Assuming you have permissions to remotely query those hosts you can try the following:假设您有权远程查询这些主机,您可以尝试以下操作:
$computers = (Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"').DNSHostName
Get-CimInstance Win32_Service -Filter "StartName LIKE '%Administrator%'" -ComputerName $computers
| Select-Object DisplayName, StartName, PSComputerName
Get-CimInstance
and Get-WmiObject
(replaced in this example, since its no longer maintained in new PowerShell versions) can invoke queries in parallel, hence no loop is required. Get-CimInstance
和Get-WmiObject
(在本例中被替换,因为它不再在新的 PowerShell 版本中维护)可以并行调用查询,因此不需要循环。Get-WmiObject
instead of Get-CimInstance
could be if using DCOM as your remoting protocol.Get-WmiObject
而不是Get-CimInstance
的唯一真正原因可能是使用 DCOM 作为您的远程处理协议。 At which point you could use New-CimSessionOption -Protocol DCOM
and connect using a CimSession.New-CimSessionOption -Protocol DCOM
并使用 CimSession 进行连接。 Functionality for both cmdlets is mostly the same.PSComputerName
in your Select-Object
statement to understand from which computer the object is coming from.Select-Object
语句中包含PSComputerName
以了解 object 来自哪台计算机。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.