Powershell 选择字符串的输出在输出中有@{

Powershell 选择字符串的输出在输出中有@{,powershell,Powershell,我正在尝试获取处于自动模式但未运行的服务并启动它们,除了我想从脚本中忽略的几个服务之外,是否有办法获得所需的输出,格式正确,如图2所示,因为如果无法将service.name作为变量,请选择字符串“输出我的脚本”。 我正在使用下面的命令 Get-CimInstance win32_service -Filter "startmode = 'auto' AND state != 'running' " | select name, startname, exitcode | Select-Str

我正在尝试获取处于自动模式但未运行的服务并启动它们,除了我想从脚本中忽略的几个服务之外,是否有办法获得所需的输出,格式正确,如图2所示,因为如果无法将service.name作为变量,请选择字符串“输出我的脚本”。 我正在使用下面的命令

Get-CimInstance win32_service -Filter "startmode = 'auto' AND state != 'running' "  | select name, startname, exitcode | Select-String   "gupdate|RemoteRegistry"  -NotMatch

下面是我的剧本

    $Services = Get-CimInstance win32_service -Filter "startmode = 'auto' AND state != 'running' "  | select name, startname, exitcode | Select-String  -Pattern "gupdate|RemoteRegistry" -NotMatch
$ServicesRunning = Get-CimInstance win32_service -Filter "state = 'running'"
if ([string]::IsNullOrEmpty($Services)){
    Write-Output "OK: All services running | ServicesRunning=$($ServicesRunning.Count);0;0;0;0"
    $host.SetShouldExit(0)
}
else{
    $ServicesStopped=""
    ForEach ($Service in $Services){
        Start-Service @($Service.Name) -ErrorAction SilentlyContinue | Out-Null  
        if ($(Get-Service -Name ($Service.Name)).Status -eq "running"){
            $ServicesStopped += "($Service.Name)(Started manually),"
            If ($ExitCode -eq 0){
                $ExitCode = 1
            }
        }
        Else{
            $ServicesStopped += "$($Service.Name)(Stopped),"
            $ExitCode = 2
        }
    }
    If ($ExitCode -eq 2){
        Write-Output "CRITICAL: Service(s) stopped: $($ServicesStopped.TrimEnd(",")) | ServicesRunning=$($ServicesRunning.Count);0;0;0;0"
        $host.SetShouldExit(2)
    }
    Else{
        Write-Output "WARNING: Service(s) stopped: $($ServicesStopped.TrimEnd(",")) | ServicesRunning=$($ServicesRunning.Count);0;0;0;0"
        $host.SetShouldExit(1)
    }
}

Select String
需要一个字符串或字符串列表作为输入。
Select Object
生成一个自定义对象列表。将后者插入前者会导致后者的输出转换为字符串。这显然不是您想要的。而且它也不需要,因为您可以直接执行所有筛选操作
获取CimInstance
参数
-Filter

$fltr = "name!='gupdate' AND name!='RemoteRegistry'" + 
        " AND startmode='auto' AND state!='running'"

Get-CimInstance Win32_Service -Filter $fltr |
    Select-Object Name, StartName, ExitCode

Select String
需要一个字符串或字符串列表作为输入。
Select Object
生成一个自定义对象列表。将后者插入前者会导致后者的输出转换为字符串。这显然不是您想要的。而且它也不需要,因为您可以直接执行所有筛选操作
获取CimInstance
参数
-Filter

$fltr = "name!='gupdate' AND name!='RemoteRegistry'" + 
        " AND startmode='auto' AND state!='running'"

Get-CimInstance Win32_Service -Filter $fltr |
    Select-Object Name, StartName, ExitCode

Get-CimInstance win32_服务-Filter“name!='GuUpdate'和name!='RemoteRegistry'和startmode='auto'和state!='running'”|选择name、startname、exitcode
感谢Ansgar Wiechers解决了这个问题…你太棒了!!!AnsgarWiechers发布答案:)
Get-CimInstance win32_服务-Filter“名称!='gupdate'和名称!='RemoteRegistry'和startmode='auto'和state!='running'”|选择名称、startname和exitcode
谢谢Ansgar Wiechers这个问题已经解决了…你太棒了!!!@AnsgarWiechers发布答案:)