-1

I written a script to verify the services were succefully stopped If not it will kill the running processes. Below is my script.

Let me know what needs be added. I am very new to 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
}

Error message "

A parameter cannot be found that matches parameter name 'computerName'

Nithin
  • 1
  • 1
  • 3
  • 1
    It helps reading the help. cite from `Get-Help Stop-Process` => `Stop-Process works only on processes running on the local computer.` – LotPings May 23 '19 at 16:28
  • [Query and kill a process on a remote computer using PowerShell and WMI](https://4sysops.com/archives/query-and-kill-a-process-on-a-remote-computer-using-powershell-and-wmi/) – JosefZ May 24 '19 at 10:19
  • Hi Josefz thnks for the resposne. Script mentioned in the page working for me... But we hav to stop multiple processes at a time.. What is the way i can call them using this script – Nithin May 29 '19 at 09:01

1 Answers1

1

As @LotPings pointed out, the trouble is not with the computername as the error suggests. The command errors when attempting to stop a service that is already stopped.
Get-process returns the same error.

Its still possible to use these cmdlets by muting these errors and returning failures when they actually work.

$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
}

  • Hi there @I-Script-Alot! Welcome to the community! Explaining your answer can make it easier to understand and even help others that are looking for similar solutions. Take some time to explain why are you proposing this solution and how you are achieving what the OP aims to. Best regards! – fboaventura Jun 25 '19 at 15:08
  • 1
    @fboaventura Thank you. I have updated my comment – I-Script-Alot Jun 29 '19 at 06:40