请您告诉我如何让我的脚本在单个表格中为每个“服务器”的所有“服务”格式化其输出?
请在下面找到我当前的PowerShell脚本:
$serverList = gc computer.txt
$serviceList = gc service.txt
if ((Test-Path OUTPUT.txt) -eq $true) {
Write-Host "Deleting existing OUTPUT file"
Remove-Item OUTPUT.txt
}
ForEach ($server in $serverList)
{
$style = @{Expression={$server};Label="Server Name";width=30}, `
@{Expression={$_.Name};Label="Service Name";width=20}, `
@{Expression={$_.StartMode};Label="StartMode";width=10}, `
@{Expression={$_.State};Label="State";width=10}, `
@{Expression={$_.ProcessId};Label="ProcessId";width=10}
Write-Host "Starting Service Check on $server"
ForEach ($service in $serviceList)
{
Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name='$service'" | Select-Object -Property Name, StartMode, State, ProcessId | Format-Table $style | Out-File OUTPUT.txt -Append
}
Write-Host "Service Check Completed on $server"
}
答案 0 :(得分:0)
WMI附加属性PSComputername。您可以将它附加到select-object-command中,如下所示:
Get-WmiObject -Class Win32_Service -ComputerName $server -Filter "Name='$service'" | Select-Object -Property PScomputername, Name, StartMode, State, ProcessId | Format-Table $style | Out-File OUTPUT.txt -Append