我是PowerShell的新手,我正在尝试使用WMI对象检索SQL Services详细信息。我的代码如下:
$InputFile = Import-Csv "C:\input\Servers.csv" | %{ $_.Instance = $_.Instance -replace "\\.*?$" ; $_}
Write-Output "SQL Services details for Server:" | Out-File C:\output\SQLCHECK.STOPPED.LOG
Write-Output "********************************" | Out-File C:\output\SQLCHECK.STOPPED.LOG -Append
foreach($Servers in $Inputfile)
{
Write-Output $Servers|ft -AutoSize | Out-File C:\output\SQLCHECK.STOPPED.LOG
$Servicesstate=Get-WmiObject win32_service -ComputerName $Servers.instance | Select Name, Startmode, State | Where-Object `
{$_.name -like "*SQL*" -and $_.Startmode -match "Auto" -and $_.state -match "Stopped"} | ft -auto
if (!$Servicesstate )
{
Write-Host "No Services in STOP state"
Write-Output "No Services in STOPPED state" | Out-File C:\output\SQLCHECK.STOPPED.LOG -Append
}
Else
{
echo ($Servicesstate )
Write-Output ($Servicesstate) | Out-File C:\output\SQLCHECK.STOPPED.LOG -Append
}
}
我的输出会是这样的:
Instance Name:
ABCDEFGH
Name Startmode State
SQLdmCollectionService$Default Auto Stopped
SQLdmManagementService$Default Auto Stopped
SQLdmPredictiveAnalyticsService$Default Auto Stopped
我的问题是,如何在输出中添加其他列并将自定义文本添加为值。
如果有任何服务停止,我想添加备注列并显示失败。
Name Startmode State Remarks
SQLdmCollectionService$Default Auto Stopped Failed
SQLdmManagementService$Default Auto Stopped Failed
SQLdmPredictiveAnalyticsService$Default Auto Stopped Failed
答案 0 :(得分:1)
您的列是由您在Select-Object中放置的内容定义的,因为您只有Name,Startmode和State这些将是列。
更改
Select Name,Startmode,State
要
Select-Object Name,Startmode,State,Remark
通过添加另一个名为Remarks的属性,您将有效地向输出添加另一列,您可以通过调用属性来更改Remark的值
$Servicestate.Remark = 'Failed'
所以你的最终代码可能看起来像这样
$InputFile = Import-Csv "C:\input\Servers.csv" | %{ $_.Instance = $_.Instance -replace "\\.*?$" ; $_}
Write-Output "SQL Services details for Server:" | Out-File C:\output\SQLCHECK.STOPPED.LOG
Write-Output "********************************" | Out-File C:\output\SQLCHECK.STOPPED.LOG -Append
foreach($Servers in $Inputfile)
{
Write-Output $Servers|ft -AutoSize | Out-File C:\output\SQLCHECK.STOPPED.LOG
$Servicesstate=Get-WmiObject win32_service -ComputerName $Servers.instance | Select Name, Startmode, State | Where-Object `
{$_.name -like "*SQL*" -and $_.Startmode -match "Auto" -and $_.state -match "Stopped"} | ft -auto
if (!$Servicesstate )
{
# Edit the new column before you output
$Servicestate.Remark = 'Failed'
Write-Host "No Services in STOP state"
Write-Output "No Services in STOPPED state" | Out-File C:\output\SQLCHECK.STOPPED.LOG -Append
}
Else
{
# Edit the new column before you output
$Servicestate.Remark = 'Success'
echo ($Servicesstate )
Write-Output ($Servicesstate) | Out-File C:\output\SQLCHECK.STOPPED.LOG -Append
}
}
答案 1 :(得分:0)
将值分配给$servicestate
时,您可以使用Select
命令并使用哈希表创建额外值。看看这个略微修改过的脚本版本,密切注意第9行:
$InputFile = Import-Csv "C:\input\Servers.csv" | %{ $_.Instance = $_.Instance -replace "\\.*?$" ; $_}
Write-Output "SQL Services details for Server:" | Out-File C:\output\SQLCHECK.STOPPED.LOG
Write-Output "********************************" | Out-File C:\output\SQLCHECK.STOPPED.LOG -Append
foreach($Servers in $Inputfile)
{
Write-Output $Servers|ft -AutoSize | Out-File C:\output\SQLCHECK.STOPPED.LOG
$Servicesstate=Get-WmiObject win32_service -ComputerName $Servers.instance | Select Name, Startmode, State |
Where{$_.name -like "*SQL*" -and $_.Startmode -match "Auto" -and $_.state -match "Stopped"}|Select Name,Startmode,State,@{l='Remarks';e={if($_.State -eq "Stopped"){"Failed"}}}
if (!$Servicesstate )
{
Write-Host "No Services in STOP state"
Write-Output "No Services in STOPPED state" | Out-File C:\output\SQLCHECK.STOPPED.LOG -Append
}
Else
{
echo ($Servicesstate|ft -AutoSize )
Write-Output ($Servicesstate) |FT -auto| Out-File C:\output\SQLCHECK.STOPPED.LOG -Append
}
}