简体   繁体   English

PowerShell 将服务器拉入 AD 然后搜索在本地管理员帐户下运行的服务的脚本

[英]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 的新手,试图将脚本拼凑在一起以:

  1. Pull a list of Servers in Active Directory (done).拉取 Active Directory 中的服务器列表(完成)。
  2. Query each server for a list of SERVICE accounts running under ADMINISTATOR credentials.查询每台服务器以获取在管理员凭据下运行的服务帐户列表。

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
  • Notes笔记
    1. 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-CimInstanceGet-WmiObject (在本例中被替换,因为它不再在新的 PowerShell 版本中维护)可以并行调用查询,因此不需要循环。
    2. Both cmdlets allow queries with WQL .这两个 cmdlet 都允许使用WQL进行查询。 Faster filtering this way than with .以这种方式过滤比使用
    3. Only real reason to use 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.这两个 cmdlet 的功能基本相同。
    4. You should include 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.

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