Powershell - 管道输出 - 主机何时?

时间:2018-04-18 13:58:09

标签: powershell

我的下面的脚本提出了两个我似乎无法解决的问题。

1)服务输出应出现在第一个读取主机之后,但它出现在行尾(下面的每个输出)

2)启动服务脚本失败后,脚本运行If语句,无论

我可以请一点帮忙吗?我已经在一个月的午餐中从powershell那里得到了这么远。

function startservice {
    $userinput = ""
    $status = get-service
    Write-Host "This script will list any Stopped services and ask you for the service to start"
    sleep 2
    Read-Host "Press enter to continue"

    if ($status.status -eq "Stopped") {
        write-host "Stopped status are"
        $status | Where-Object {$_.status -eq "Stopped"} | 
        select name, status #Why are these services not displayed now but display after $userinput?

        sleep 2

        $userinput = Read-Host "Which service would you like to start?
        Press Q to quit: "
        if ($userinput -match "Q"){
        break } else {
        if ($userinput -match $_.name) 
        {start-service -inputobject $userinput
        write-host "You have successfully started service $userinput" #Upon failure, script starts this line regardless
        }
        else {write-host "No such service exists, try again."}
        startservice


        }
    } 

    else {
        write-host "All status are running"
    }
}
startservice

输出

PS C:\Windows\system32> C:\Users\Sumeet\Desktop\Script.ps1
This script will list any Stopped services and ask you for the service to start
Press enter to continue: 

Stopped status are

Which service would you like to start?
        Press Q to quit: : aa
Name                                      Status
----                                      ------
AJRouter                                 Stopped
ALG                                      Stopped
AppIDSvc                                 Stopped
AppReadiness                             Stopped
AppVClient                               Stopped
AssignedAccessManagerSvc                 Stopped
AxInstSV                                 Stopped
Browser                                  Stopped
camsvc                                   Stopped
ClipSVC                                  Stopped
COMSysApp                                Stopped
cphs                                     Stopped
CscService                               Stopped
defragsvc                                Stopped
DeviceInstall                            Stopped
DevicesFlowUserSvc_2c06d82               Stopped
DevQueryBroker                           Stopped
diagnosticshub.standardcollector.service Stopped
diagsvc                                  Stopped
DmEnrollmentSvc                          Stopped
dmwappushservice                         Stopped
dot3svc                                  Stopped
DsmSvc                                   Stopped
Eaphost                                  Stopped
embeddedmode                             Stopped
EntAppSvc                                Stopped
Fax                                      Stopped
fhsvc                                    Stopped
FrameServer                              Stopped
gpsvc                                    Stopped
GraphicsPerfSvc                          Stopped
gupdate                                  Stopped
gupdatem                                 Stopped
HomeGroupListener                        Stopped
HvHost                                   Stopped
icssvc                                   Stopped
igfxCUIService2.0.0.0                    Stopped
IKEEXT                                   Stopped
InstallService                           Stopped
IpxlatCfgSvc                             Stopped
irmon                                    Stopped
KtmRm                                    Stopped
lltdsvc                                  Stopped
MapsBroker                               Stopped
MessagingService_2c06d82                 Stopped
MSDTC                                    Stopped
MSiSCSI                                  Stopped
msiserver                                Stopped
NaturalAuthentication                    Stopped
NcaSvc                                   Stopped
Netlogon                                 Stopped
Netman                                   Stopped
NetSetupSvc                              Stopped
NetTcpPortSharing                        Stopped
NgcCtnrSvc                               Stopped
NgcSvc                                   Stopped
ose                                      Stopped
p2pimsvc                                 Stopped
p2psvc                                   Stopped
PeerDistSvc                              Stopped
PerfHost                                 Stopped
PhoneSvc                                 Stopped
PimIndexMaintenanceSvc_2c06d82           Stopped
pla                                      Stopped
PNRPAutoReg                              Stopped
PNRPsvc                                  Stopped
PolicyAgent                              Stopped
PrintNotify                              Stopped
PrintWorkflowUserSvc_2c06d82             Stopped
PushToInstall                            Stopped
QWAVE                                    Stopped
RasAuto                                  Stopped
RemoteAccess                             Stopped
RemoteRegistry                           Stopped
RetailDemo                               Stopped
RpcLocator                               Stopped
SCardSvr                                 Stopped
ScDeviceEnum                             Stopped
SCPolicySvc                              Stopped
SDRSVC                                   Stopped
seclogon                                 Stopped
Sense                                    Stopped
SensorDataService                        Stopped
SharedAccess                             Stopped
SharedRealitySvc                         Stopped
shpamsvc                                 Stopped
smphost                                  Stopped
SmsRouter                                Stopped
SNMPTRAP                                 Stopped
spectrum                                 Stopped
sppsvc                                   Stopped
svsvc                                    Stopped
swprv                                    Stopped
TapiSrv                                  Stopped
TieringEngineService                     Stopped
tiledatamodelsvc                         Stopped
TrustedInstaller                         Stopped
tzautoupdate                             Stopped
UevAgentService                          Stopped
UI0Detect                                Stopped
UnistoreSvc_2c06d82                      Stopped
upnphost                                 Stopped
UserDataSvc_2c06d82                      Stopped
UsoSvc                                   Stopped
vmicguestinterface                       Stopped
vmicheartbeat                            Stopped
vmickvpexchange                          Stopped
vmicrdv                                  Stopped
vmicshutdown                             Stopped
vmictimesync                             Stopped
vmicvmsession                            Stopped
vmicvss                                  Stopped
VSS                                      Stopped
W32Time                                  Stopped
WalletService                            Stopped
WarpJITSvc                               Stopped
wbengine                                 Stopped
WbioSrvc                                 Stopped
wcncsvc                                  Stopped
WebClient                                Stopped
Wecsvc                                   Stopped
WEPHOSTSVC                               Stopped
wercplsupport                            Stopped
WerSvc                                   Stopped
WFDSConMgrSvc                            Stopped
WiaRpc                                   Stopped
wisvc                                    Stopped
wlidsvc                                  Stopped
wlpasvc                                  Stopped
wmiApSrv                                 Stopped
WMPNetworkSvc                            Stopped
workfolderssvc                           Stopped
WPDBusEnum                               Stopped
wuauserv                                 Stopped
WwanSvc                                  Stopped
xbgm                                     Stopped
XblGameSave                              Stopped
XboxGipSvc                               Stopped
XboxNetApiSvc                            Stopped
start-service : Service aa was not found on computer '.'.
At C:\Users\Sumeet\Desktop\Script.ps1:23 char:10
+         {start-service -inputobject $userinput
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-Service], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.StartServiceCommand

You have successfully started service aa
This script will list any Stopped services and ask you for the service to start
Press enter to continue: 

1 个答案:

答案 0 :(得分:-1)

  1. 我不确定为什么会存在这种竞争条件,但你可以通过在调用读取主机行之前明确告知数据写入主机来解决这个问题
  2. 您实际上并未检查是否存在错误,并且您的方案中可能不存在$ _,因为您不在管道中。 $?可以检查是否成功完成。这样的事情应该会有所帮助:
  3. $ userinput =“” $ status = get-service | Where-Object {$ _。status -eq“Stopped”} |选择名称,状态 Write-Host“此脚本将列出所有已停止的服务并要求您启动服务” 读取主机“按enter继续”

    if ($status) 
    {
        write-host "Stopped status are:"
        $status | out-host
        $userinput = Read-Host "Which service would you like to start?
        Press Q to quit: "
        if ($userinput -match "Q")
        {
            break 
        } 
        elseif ($userinput -in $status.name) 
        {
            start-service -Name $userinput -ErrorAction SilentlyContinue
            if($?)
            {
                write-host "You have successfully started service $userinput"
            } 
            else
            {
                write-host "$userinput has failed to start"
            }
        }
        else 
        {
            write-host "No such service exists, try again."
        }
            #startservice
    } 
    else {
        write-host "All status are running"
    }
    

    我也改变了一下顶级查询。