代码之家  ›  专栏  ›  技术社区  ›  Brandon

PowerShell启动服务-无法验证“InputObject”问题

  •  0
  • Brandon  · 技术社区  · 7 年前

    问题

    这与: PowerShell Start-Service Timeout

    我试图运行一个循环来强制执行操作超时,但继续遇到随机验证错误。代码设置如下:

    $timeout = New-TimeSpan -Seconds $SecondsToWait
    $timer = [System.Diagnostics.Stopwatch]::StartNew()
    
    $j = Start-Job -ScriptBlock { Start-Service $ServiceName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue }
    $count = 0
    
    Do {
        If ($j.State -eq 'Running') {
            If ($timer.Elapsed.seconds -ne $count) {
                $count ++
                Write-Host "Elapsed Time: $($timer.Elapsed.seconds)"
            }
    
            $j | Receive-Job
        }
    } While ( $timer.Elapsed -lt $timeout )
    

    我尝试了一个视图变体来解决这个问题,但我一直在返回到程序退出时的同一个根本问题

    Cannot validate argument on parameter 'InputObject'. The Argument is null or empty. 
    Supply an argument that is not null or empty and then try the command again.
    
    At C:file\path.ps1:543 char:13
             $j | Receive-Job
    CategoryInfo         : InvalidData: (:) [Start-Service], ParameterBindingValidationException
    FullyQualifiedErrorId: ParameterArgumentValidatinErrorNullNotAllowed,Microsoft.PowerShell.Commands.StartServiceCommand 
    

    我已经在StackOverflow和其他一些网站上查看了许多解决方案,但大多数都不太相似,无法将答案转换为我的问题。我看到的大多数问题都与 Invoke-Command

    powershell Start-Service fails

    Powershell function throwing null exception

    "Cannot validate argument on parameter 'Identity'" while changing the description for multiple users

    Start-Service “,” Do While 在遇到此错误之前,循环随机迭代15到30次。

    1 回复  |  直到 7 年前
        1
  •  1
  •   ArcSet    7 年前

    因此,接收作业获取线程的结果。然后删除了这些结果。您得到的错误是因为线程已完成,结果是

    Start-Service $ServiceName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
    

    这就是

    无法验证参数“InputObject”上的参数。论点是 null或空。提供一个不为null或空的参数,然后 请重试该命令。

    因为您没有指定$ServiceName。。。 您可以通过使用-keep标记执行以下操作并删除-keep标记来验证这一点。keep标记将告诉作业在下次调用之前不要删除结果。

    $j = Start-Job -ScriptBlock { Start-Service $ServiceName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue }
    $count = 0
    
    Do {
    
        $j.State
        $j | Receive-Job -keep
    
    
    } While ( $timer.Elapsed -lt $timeout )
    

    下面将显示运行多次,然后一旦完成,将显示以下内容

    Running
    Running
    Running
    Completed
    Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
        + CategoryInfo          : InvalidData: (:) [Start-Service], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartServiceCommand
        + PSComputerName        : localhost 
    

    接收作业 -保留

    Running
    {Results here}
    Running
    {Results here}
    Complete
    {Results here}
    

    如果你不想看到这个错误,那么就删除它 接收作业 尝试捕捉 对于脚本块中的错误