Get-Service | Where-Object CanStop | Select-Object-Property DisplayName, DependentServices | Out-GridView-Title'Service to stop?'-OutputMode Single | Stop-Service-WhatIf
运行上面这行代码时,网格视图窗口现在看起来很棒,但 Stop-Service 将不再停止选择您选择的服务,因为 Select-Object 将对象类型从 Service 更改为自定义对象:
Stop-Service : The specified wildcard character pattern is not valid: @{DisplayName=Windows Audio Endpoint Builder;
DependentServices=System.ServiceProcess.ServiceController[]}
# create object that tells PowerShell which column(s) should be visible: # show "DisplayName", and "DependentServices" [string[]]$visible = 'DisplayName', 'DependentServices' $type = 'DefaultDisplayPropertySet' [System.Management.Automation.PSMemberInfo[]]$info = [System.Management.Automation.PSPropertySet]::new($type,$visible)
Get-Service | Where-Object CanStop | # add the secret object to each object that you pipe into Out-GridView: Add-Member-MemberType MemberSet -Name PSStandardMembers -Value$info-PassThru | Out-GridView-Title'Service to stop?'-OutputMode Single | Stop-Service-WhatIf
# create object that tells PowerShell which column(s) should be visible: # show "DisplayName", and "DependentServices" [string[]]$visible = 'DisplayName', 'DependentServices' $type = 'DefaultDisplayPropertySet' [System.Management.Automation.PSMemberInfo[]]$info = [System.Management.Automation.PSPropertySet]::new($type,$visible)
Get-Service | Where-Object CanStop | # clone the objects so they now belong to you: Select-Object-Property * | # add the secret object to each object that you pipe into Out-GridView: Add-Member-MemberType MemberSet -Name PSStandardMembers -Value$info-PassThru | Out-GridView-Title'Service to stop?'-OutputMode Single | Stop-Service-WhatIf
# create object that tells PowerShell which column(s) should be visible: # show "Name", "Description" and "MainWindowTitle" [string[]]$visible = 'Name', 'Description', 'MainWindowTitle' $type = 'DefaultDisplayPropertySet' [System.Management.Automation.PSMemberInfo[]]$info = [System.Management.Automation.PSPropertySet]::new($type,$visible)
Get-Process | Where-Object MainWindowTitle | Sort-Object-Property Name | # clone the objects so they now belong to you: Select-Object-Property * | # add the secret object to each object that you pipe into Out-GridView: Add-Member-MemberType MemberSet -Name PSStandardMembers -Value$info-PassThru | Out-GridView-Title'Select a process'-OutputMode Single | # still all properties available: Select-Object-Property *