简体   繁体   English

通过PowerShell在远程服务器上获取服务

[英]Getting services on remote servers through powershell

I've been trying to develop a powershell script to query a particular OU in AD and then get the services on each of those services including name, status, startmode, startname, description. 我一直在尝试开发一个powershell脚本来查询AD中的特定OU,然后在每个服务上获取服务,包括名称,状态,startmode,startname,description。 I've gotten really close but not getting the results I expect. 我已经非常接近但没有得到我期望的结果。 Can someone help? 有人可以帮忙吗?

The problem is if I run the command for one server specifically, I get the results I want. 问题是如果我专门为一个服务器运行命令,我得到我想要的结果。 If I run for all servers in an OU it does not find any unique startnames (only the local accounts). 如果我在OU中运行所有服务器,则它找不到任何唯一的起始名(只有本地帐户)。 I want a single CSV with the server name first 我想要一个首先使用服务器名称的CSV

Script for single computer (getting results we want): 单个计算机的脚本(获得我们想要的结果):

$ServerName = “server01”

Get-WmiObject win32_service -ComputerName $servername | Select @{N="ServerName";E={$ServerName}}, Name, Status, StartMode, StartName, description | Export-Csv "server_Services.csv" -NoTypeInformation

Script for querying OU (only returning services with local accounts): 查询OU的脚本(仅返回本地帐户的服务):

$ServerOU = 'OU=Servers,DC=some,DC=domain'

$ServerList = Get-QADComputer -SearchRoot $ServerOU -SearchScope Subtree

$ServerList | ForEach {   
      $ServerNameTemp = $_.Name
      Get-WMIObject Win32_Service | 
      Select @{N="ServerName";E={$ServerNameTemp}}, StartName, Name, StartMode, Status, description
      } | Export-Csv -NoTypeInformation $ExportFile

This should do the trick i think but th following would wanna make a file for each server in the OU, it would keep overwriting the same file. 这应该是我认为的技巧,但下面想要为OU中的每个服务器创建一个文件,它将继续覆盖相同的文件。 I also Changed Get-QADComputer into Get-ADComputer no specific need for quest powershell for this one :) 我还将Get-QADComputer改为Get-ADComputer,没有特别需要这个版本的任务powershell :)

$ServerOU = 'OU=Servers,DC=some,DC=domain'

$ServerList = Get-ADComputer -SearchBase $ServerOU  -Filter *

ForEach ($Server in $Serverlist)
{
          Invoke-Command -ComputerName $Server.Name -ScriptBlock {
              Get-WMIObject Win32_Service | 
              Select @{N="ServerName";E={[System.Net.Dns]::GetHostName()}}, StartName, Name, StartMode, Status, description}
} | Export-Csv -NoTypeInformation $ExportFile

This way it would send ti all to a variable first and then to a file. 这样它首先将ti全部发送到变量然后发送到文件。

$ServerOU = 'OU=Servers,DC=some,DC=domain'
$Services = @()
$ServerList = Get-ADComputer -SearchBase $ServerOU -Filter * 

ForEach ($Server in $Serverlist)
{
          $Services += Invoke-Command -ComputerName $Server.Name -ScriptBlock {
               Get-WMIObject Win32_Service | 
              Select-Object @{N="ServerName";E={[System.Net.Dns]::GetHostName()}}, StartName, Name, StartMode, Status, description }
} 

$Services | Export-Csv -NoTypeInformation $Exportfile

PS Remoting is automatically enabled from win 2012. If you are using any Win servers prior to that, you need to enable to WinRM services, to enable powershell remoting. 从2012年开始自动启用PS Remoting。如果您之前使用任何Win服务器,则需要启用WinRM服务,以启用PowerShell远程处理。 This is very important, if you are trying to use "Invoke-Command" or any of those "*-Session" cmdlets against those servers. 如果您尝试对这些服务器使用“Invoke-Command”或任何“* -Session”cmdlet,这一点非常重要。

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

相关问题 Powershell 脚本,用于查找在远程服务器上运行的服务 - Powershell script to find services running on remote servers 发出运行powershell命令以检查远程服务器服务状态的问题 - Issues running powershell command to check status of remote servers services 远程桌面服务:通过 PowerShell 的 CAP 或 RAP - Remote Desktop Services: CAP or RAP through PowerShell 是否可以通过 Powershell 在多个远程服务器上安装 Windows 更新? - Is it possible to install Windows Updates on multiple, remote servers through Powershell? 使用PowerShell在远程服务器上安装可执行文件(.msi / .exe)时出错 - Getting error in installing executables (.msi/.exe) on remote servers using PowerShell 使用PowerShell在远程服务器上添加文件夹 - Add Folders On Remote Servers With PowerShell Windows服务在远程服务器(不同域)上的部署 - Deployment of Windows Services on Remote Servers (Different Domain) 通过Powershell在多个远程服务器上解压缩文件 - Unzip a file on multiple remote servers via Powershell 在多个驱动器上的服务器上进行Powershell远程文件搜索 - Powershell remote file search on servers on multiple drives 如何使用Powershell在远程服务器上搜索文件夹 - How to use powershell to search remote servers for folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM