我已编写此脚本来查询多台计算机的“ KService”状态。它可以工作,但是每10分钟大约有100台计算机。有什么方法可以提高这项工作的速度吗?我想同时查询多达1000台计算机。
function Query-Service
{
[CmdletBinding()]
param
(
[Parameter()]
[ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1 })]
[ValidateNotNullOrEmpty()]
[string[]]$ComputerName = $env:COMPUTERNAME
)
foreach ($comp in $ComputerName)
{
$output = @{ 'ComputerName' = $comp }
$output."Service Name" = (Get-Service -Name KService -ComputerName $comp).Name
$output.Status = (Get-Service -Name KService -ComputerName $Comp).Status
[PSCustomObject]$output | Select-Object "ComputerName", "Service Name", "Status"
}
}
Write-Host "Please select the machine list"
Function Get-FileName($InitialDirectory) {
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
#$OpenFileDialog.filter = "CSV (*.csv)| *.csv|TXT (*.txt)| *.txt"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$inputfile = Get-FileName "C:"
$ComputerList = get-content $inputfile
$ErrorActionPreference = "SilentlyContinue"
cls
foreach ($Computer in $ComputerList) {
Query-Service $Computer
}
答案 0 :(得分:0)
如果我们查看命令Get-Service的基于注释的帮助,我们可以看到Computername参数将采用数组。
Get-Help -Full Get-Service
PARAMETERS -ComputerName String []获取在指定计算机上运行的服务。默认值为本地计算机。
键入远程计算机的NetBIOS名称,IP地址或完全限定的域名(FQDN)。要指定本地计算机,请键入计算机名称,点(。)或localhost。
此参数不依赖Windows PowerShell远程处理。即使您的计算机未配置为运行远程命令,也可以使用Get-Service的ComputerName参数。
请尝试以下操作:
function Query-Service
{
[CmdletBinding()]
param
(
[Parameter()]
[ValidateScript( { Test-Connection -ComputerName $_ -Quiet -Count 1 })]
[ValidateNotNullOrEmpty()]
[string[]]$ComputerName = $env:COMPUTERNAME
)
Get-Service -Name KService -ComputerName $ComputerName | Select-Object "MachineName", "Name", "Status"
}