<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dienste beenden &#8211; abow &#8211; how do you do IT</title>
	<atom:link href="https://abow.info/tag/dienste-beenden/feed/" rel="self" type="application/rss+xml" />
	<link>https://abow.info</link>
	<description>Alle möglichen Handkniffe, die ich mir so zusammentrage im Berufsalltag</description>
	<lastBuildDate>Tue, 02 Jun 2026 21:36:31 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://abow.info/wp-content/uploads/2026/06/cropped-favicon-512-32x32.png</url>
	<title>Dienste beenden &#8211; abow &#8211; how do you do IT</title>
	<link>https://abow.info</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Powershell // Einen Prozess auf Remotecomputern beenden</title>
		<link>https://abow.info/microsoft/powershell-einen-prozess-auf-remotecomputern-beenden/</link>
					<comments>https://abow.info/microsoft/powershell-einen-prozess-auf-remotecomputern-beenden/#respond</comments>
		
		<dc:creator><![CDATA[Andi Bow]]></dc:creator>
		<pubDate>Wed, 21 Dec 2022 12:00:00 +0000</pubDate>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Microsoft Powershell]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[AD]]></category>
		<category><![CDATA[Dienste beenden]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Remote-Powershell]]></category>
		<category><![CDATA[Skript]]></category>
		<guid isPermaLink="false">https://abow.info/?p=193</guid>

					<description><![CDATA[Du sitzt an deinem Admin-PC und ein Prozess hängt – aber nicht auf deiner Maschine, sondern auf einem halben Dutzend Servern gleichzeitig. Jeden einzeln per RDP aufmachen? Spar…]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Du sitzt an deinem Admin-PC und ein Prozess hängt – aber nicht auf deiner Maschine, sondern auf einem halben Dutzend Servern gleichzeitig. Jeden einzeln per RDP aufmachen? Spar dir das. In diesem Artikel zeige ich dir ein PowerShell-Skript, mit dem du <strong>einen Prozess auf einem oder mehreren Remote-Computern in einem Rutsch beendest</strong> – wahlweise per Hostliste, per Textdatei oder direkt aus einer Active-Directory-OU heraus.</p>



<p class="wp-block-paragraph">Gegenüber meiner ersten Version (2022) ist das Skript komplett überarbeitet: zuverlässiges Remoting über <code>Invoke-Command</code>, sauberer Parameter-Block, Sicherheitsabfrage über das native <code>-Confirm</code> von PowerShell statt einer Windows-Forms-Box und eine echte Erfolgskontrolle pro Computer.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><strong>Getestet auf:</strong> Windows 10 / 11 und Windows Server 2016–2022.</p>
</blockquote>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Voraussetzungen</h2>



<p class="wp-block-paragraph">Damit das Skript funktioniert, müssen ein paar Dinge stimmen:</p>



<ul class="wp-block-list">
<li><strong>PowerShell-Remoting (WinRM)</strong> ist auf den Zielcomputern aktiv (<code>Enable-PSRemoting -Force</code>).</li>



<li>Die Remotecomputer sind aus deinem Netzwerk <strong>erreichbar</strong>.</li>



<li>Du hast <strong>Admin-Rechte</strong> auf den Zielen, z. B. als Domain-Administrator.</li>



<li>Die <strong>ExecutionPolicy</strong> lässt die Ausführung zu. Für einen einmaligen Lauf reicht:</li>
</ul>



<pre class="wp-block-code"><code>  powershell.exe -ExecutionPolicy Bypass -File .\Stop-RemoteProcess.ps1</code></pre>



<p class="wp-block-paragraph">(Wie man ExecutionPolicys sauber per GPO für Benutzer oder Computer setzt, schreibe ich gerne in einem eigenen Artikel.)</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Drei Wege, die Zielcomputer anzugeben</h2>



<p class="wp-block-paragraph">Das Skript arbeitet mit Parameter-Sets – du wählst genau <strong>einen</strong> der drei Wege:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Weg</th><th>Parameter</th><th>Wann sinnvoll</th></tr></thead><tbody><tr><td>Direkte Liste</td><td><code>-ComputerName SRV01,SRV02</code></td><td>Du kennst die Hosts</td></tr><tr><td>Textdatei</td><td><code>-Path C:\temp\hosts.txt</code></td><td>Feste Maschinenlisten, ein Host pro Zeile</td></tr><tr><td>AD-Suche</td><td><code>-SearchBase ... -NameFilter ...</code></td><td>Dynamisch alle Server einer OU</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">Den zu beendenden Prozess gibst du immer über <code>-ProcessName</code> an (ohne <code>.exe</code>).</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Anwendungsbeispiele</h2>



<pre class="wp-block-code"><code># Direkt zwei Hosts
.\Stop-RemoteProcess.ps1 -ComputerName SRV01,SRV02 -ProcessName notepad

# Hostliste aus Textdatei
.\Stop-RemoteProcess.ps1 -Path C:\temp\hosts.txt -ProcessName Teams

# Alle Server einer OU, deren Name "SQL" enthält
.\Stop-RemoteProcess.ps1 -SearchBase 'OU=Server,DC=abow,DC=local' -NameFilter 'SQL' -ProcessName sqlservr</code></pre>



<p class="wp-block-paragraph">Vor dem Beenden zeigt dir das Skript alle gefundenen Computer an und fragt pro Maschine nach (<code>-Confirm</code>). Erst nach deinem <strong>OK</strong> wird der Prozess wirklich gekillt – und anschließend geprüft, ob er auch tatsächlich weg ist.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Das Skript</h2>



<pre class="wp-block-code"><code>#Requires -Version 5.1
&lt;#
.SYNOPSIS
    Beendet einen Prozess auf einem oder mehreren Remote-Computern via Invoke-Command.

.NOTES
    Name:    Stop-RemoteProcess
    Author:  Andreas Bowitz (Andi Bow)
    Version: 1.0
#&gt;

&#91;CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', DefaultParameterSetName = 'ComputerName')]
param(
    &#91;Parameter(Mandatory, ParameterSetName = 'ComputerName')]
    &#91;string&#91;]]$ComputerName,

    &#91;Parameter(Mandatory, ParameterSetName = 'File')]
    &#91;ValidateScript({ Test-Path $_ })]
    &#91;string]$Path,

    &#91;Parameter(Mandatory, ParameterSetName = 'AD')]
    &#91;string]$SearchBase,

    &#91;Parameter(Mandatory, ParameterSetName = 'AD')]
    &#91;string]$NameFilter,

    &#91;Parameter(Mandatory)]
    &#91;string]$ProcessName,

    &#91;System.Management.Automation.PSCredential]$Credential
)

# --- Admin-Rechte pruefen ---
$identity  = &#91;Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
if (-not $principal.IsInRole(&#91;Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Warning "Keine Administrator-Rechte. Bitte PowerShell als Administrator starten."
    return
}

# --- Zielcomputer ermitteln ---
$targets = switch ($PSCmdlet.ParameterSetName) {
    'ComputerName' { $ComputerName }
    'File'         { Get-Content -Path $Path | Where-Object { $_.Trim() } }
    'AD' {
        Import-Module ActiveDirectory -ErrorAction Stop
        Get-ADComputer -Filter "Name -like '*$NameFilter*'" -SearchBase $SearchBase |
            Select-Object -ExpandProperty Name
    }
}

if (-not $targets) {
    Write-Warning "Keine Computer gefunden. Abbruch."
    return
}

Write-Host "Folgende Computer wurden gefunden:" -ForegroundColor Cyan
$targets | ForEach-Object { Write-Host "  - $_" }

# --- Credentials holen ---
if (-not $Credential) {
    $Credential = Get-Credential -Message "Bitte mit einem Administrator-Konto anmelden."
}

# --- Prozess remote beenden ---
foreach ($computer in $targets) {

    if (-not (Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
        Write-Warning "$computer ist nicht erreichbar - uebersprungen."
        continue
    }

    if (-not $PSCmdlet.ShouldProcess($computer, "Prozess '$ProcessName' beenden")) {
        continue
    }

    try {
        $result = Invoke-Command -ComputerName $computer -Credential $Credential -ScriptBlock {
            param($name)
            $procs = Get-Process -Name $name -ErrorAction SilentlyContinue
            if (-not $procs) { return 'not-running' }
            $procs | Stop-Process -Force -ErrorAction Stop
            Start-Sleep -Seconds 1
            if (Get-Process -Name $name -ErrorAction SilentlyContinue) { 'still-running' } else { 'stopped' }
        } -ArgumentList $ProcessName

        switch ($result) {
            'stopped'       { Write-Host "&#91;OK]   $computer : '$ProcessName' wurde beendet." -ForegroundColor Green }
            'not-running'   { Write-Host "&#91;INFO] $computer : '$ProcessName' war nicht aktiv." -ForegroundColor Yellow }
            'still-running' { Write-Warning "$computer : '$ProcessName' konnte NICHT beendet werden." }
        }
    }
    catch {
        Write-Warning "$computer : Fehler - $($_.Exception.Message)"
    }
}</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Was sich gegenüber der alten Version verbessert hat</h2>



<p class="wp-block-paragraph">Für alle, die die 2022er-Fassung kennen – die wichtigsten Änderungen und <em>warum</em>:</p>



<ul class="wp-block-list">
<li><strong><code>Invoke-Command</code> statt <code>Enter-PSSession</code> in der Schleife.</strong> <code>Enter-PSSession</code> ist für interaktive Sitzungen gedacht und lässt sich nicht sinnvoll automatisiert in einer Schleife abarbeiten. <code>Invoke-Command</code> führt den Block remote aus und gibt ein Ergebnis zurück.</li>



<li><strong><code>-using</code>/Argument-Übergabe.</strong> Die lokale Variable <code>$ProcessName</code> landet jetzt über <code>-ArgumentList</code> korrekt im Remote-Scriptblock – vorher war sie dort schlicht leer.</li>



<li><strong><code>Get-Content</code> statt <code>[IO.File]::ReadAllText</code>.</strong> Letzteres liefert <em>einen</em> String, sodass die Schleife nur einmal über den Gesamttext lief. <code>Get-Content</code> gibt ein Array – eine Zeile pro Host.</li>



<li><strong><code>Select-Object -ExpandProperty Name</code> statt <code>Format-Table</code>.</strong> <code>Format-Table</code> erzeugt Anzeigeobjekte, keine verwertbaren Strings – die Schleife lief damit ins Leere.</li>



<li><strong>Native Sicherheitsabfrage über <code>ShouldProcess</code>/<code>-Confirm</code></strong> statt einer <code>System.Windows.Forms.MessageBox</code>. Funktioniert auch ohne GUI / in Remote-Sessions.</li>



<li><strong>Erreichbarkeitsprüfung</strong> pro Host (<code>Test-Connection</code>), saubere <code>try/catch</code>-Fehlerbehandlung und ein klares Status-Ergebnis je Computer.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p class="wp-block-paragraph">Hier könnt ihr das Skript herunterladen:</p>



<div class="wp-block-file"><a id="wp-block-file--media-520099cf-ddb6-48ed-ae4a-d33426306b91" href="https://abow.info/wp-content/uploads/2022/12/Stop-RemoteProcess.ps1">Stop-RemoteProcess</a><a href="https://abow.info/wp-content/uploads/2022/12/Stop-RemoteProcess.ps1" class="wp-block-file__button wp-element-button" download aria-describedby="wp-block-file--media-520099cf-ddb6-48ed-ae4a-d33426306b91"><strong>Herunterladen</strong></a></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">Wie immer berichte ich hier nur von meinen persönlichen Erfahrungen und Ergebnissen. Dies ist keine offizielle Anleitung von Microsoft. Das Nutzen von Skripten geschieht auf eigene Gefahr. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p class="wp-block-paragraph"></p>
</blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://abow.info/microsoft/powershell-einen-prozess-auf-remotecomputern-beenden/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
