【问题标题】:How to add hardcoded value to WMI output in Powershell如何在 Powershell 中将硬编码值添加到 WMI 输出
【发布时间】:2014-09-16 15:29:13
【问题描述】:

我是 powershell 新手,我正在尝试使用 WMI 对象检索 SQL 服务详细信息。我的代码如下:


$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

【问题讨论】:

  • 感谢您格式化文本

标签: sql sql-server powershell csv wmi


【解决方案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
}
}

【讨论】:

  • 当我使用 $Servicesstate.Remark = 'Failed'
      无法在此对象上找到属性“Remark”时出现错误;确保它存在并且是可设置的。在 line:17 char:1 + $Servicestate.Remark = 'Failed' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
【解决方案2】:

将您的值分配给$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
    }
}

【讨论】:

  • 谢谢,这很有魅力。它首先返回错误,但随后我在备注列中添加引号修复了错误 {l='Remarks';e={if($_.State -eq "Stopped")
  • 当您手动输入内容时,我只想保持简洁,但为了脚本的可读性,让我们使用{label='Remarks';expression={if($_.State -eq "Stopped"),嗯?
  • @BenThul 我几乎总是提供我自己编写的代码,以帮助那些不知道一些快捷方式或别名的人,因为这对我来说似乎更自然。如果人们对我提供的代码有疑问或需要帮助来理解我提供的代码,我总是很乐意分解并解释它(包括使用的任何速记)。此外,我更新了我的答案以修复“备注”(添加引号)。
猜你喜欢
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 2022-12-20
  • 2020-07-22
相关资源
最近更新 更多