首页 > 解决方案 > 在脚本中获取随机的“InputObject”验证错误

问题描述

执行我的脚本时,我收到一个我不太理解的错误。它告诉我存在验证错误。

我已经测试了一些不同的变化,但它一直归结为相同的错误。

ForEach ($Index in $SQLServer)
{
    $ServiceStatus = Invoke-Command {Get-Service 'MSSQLSERVER'} -comp $Index
    while ($FilteredService.Status -ne 'Running')
    {
        Invoke-Command {Start-Service $ServiceStatus} -comp $Index
        Write-Host $ServiceStatus.Status
        Write-Host "SQL Service starting on " $Index
        function Check-Status 
        {
            Start-Sleep -seconds 30
            $ServiceStatus.Refresh()
            if ($ServiceStatus.Status -eq 'Running')
            {
                Write-Host "SQL Service running"
            }
            else
            {
                Check-Status
            }
        }
    }
}

错误是这样的:

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

通常,一旦某些参数尚未初始化,就会发生此错误,但我无法弄清楚是哪个参数。任何帮助,将不胜感激。

编辑:

ForEach ($Index in $SQLServer)
{
    $ArrayService = Invoke-Command {Get-Service -Name 'MSSQLSERVER'} -comp $Index

    if ($ArrayService.Status -ne 'Running')
    {
        Invoke-Command {Start-Service $script:ArrayService} -comp $Index
        Write-Host $ArrayService.Status
        Write-Host "SQL Service starting on " $Index
        function Check-Status 
        {
            Start-Sleep -seconds 30
            $ArrayService.Refresh()
            if ($ArrayService.Status -eq 'Running')
            {
                Write-Host "SQL Service running"
            }
            else
            {
                Check-Status
            }
            Check-Status
        }
    }
}

标签: powershellscriptingremote-server

解决方案


推荐阅读