找不到与参数名称 -ComputerName 匹配的参数

找不到与参数名称 -ComputerName 匹配的参数

我编写了一个脚本来验证服务是否已成功停止。如果没有,它将终止正在运行的进程。以下是我的脚本。

让我知道需要添加什么。我对 powershell 还很陌生。

$configData = (Get-Content (Join-Path $PSScriptRoot "config\gp.processes.Json") -Raw | ConvertFrom-Json)
$svcProcessList = $configData.UI

foreach ($svcProcess in $svcProcessList) {
    Write-Output "::INFO::Looking for $svcProcess on $computerName to kill"
    $sumOfReturnValues += stop-Process -processName $svcProcess -computerName $computerName
}
if($sumOfReturnValues -ne 0)
{
    Write-Error "Unable to stop Services as I am unable to terminate all the related processes"
    Exit 1
}

错误信息 ”

找不到与参数名称“computerName”匹配的参数

答案1

正如@LotPings 指出的那样,问题并不出在错误所暗示的计算机名称上。尝试停止已停止的服务时,命令会出错。Get
-process 返回相同的错误。

通过忽略这些错误并在它们实际工作时返回失败,仍然可以使用这些 cmdlet。

$configData = (Get-Content (Join-Path $PSScriptRoot "config\gp.processes.Json") -Raw | ConvertFrom-Json)
$svcProcessList = $configData.UI

$ReturnValues = @()
foreach ($svcProcess in $svcProcessList) {
    Write-Output "::INFO::Looking for $svcProcess on $computerName to kill"
    stop-process -Name $svcProcess -computerName $computerName -ErrorAction SilentlyContinue
    try {
        if (get-process -Name $svcProcess -computerName $computerName -ErrorAction 0 |
                select -expandproperty Responding) {
            throw "error to catch block"
        }
    }
    catch {
        $ReturnValues += $svcProcess
    }
    [int]$sumOfReturnValues = $ReturnValues | Measure-Object |
    Select-Object -ExpandProperty count
}
if ($sumOfReturnValues -ne 0) {
    Write-Error "Unable to stop Services as I am unable to terminate all the related processes"
    Exit 1
}

相关内容