# Get the directory where the script is located $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition # Path to the server list file $serverListPath = Join-Path $scriptDir "servers.txt" # Prompt for username and credentials $username = Read-Host "Enter the username (e.g., DOMAIN\user or user@domain.com)" $cred = Get-Credential -UserName $username -Message "Enter the password for $username" # Services to check and stop $services = @("wuauserv", "bits", "cryptsvc", "trustedinstaller") # Read server names from file $servers = Get-Content -Path $serverListPath foreach ($server in $servers) { Write-Host "`n--- Processing $server ---" -ForegroundColor Cyan foreach ($service in $services) { try { # Get service status remotely $svcStatus = Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock { param($svcName) $svc = Get-Service -Name $svcName -ErrorAction Stop return @{ Name = $svc.DisplayName; Status = $svc.Status } } -ArgumentList $service Write-Host "Service: $($svcStatus.Name)" Write-Host " Status: $($svcStatus.Status)" # Stop the service if it's running if ($svcStatus.Status -eq 'Running') { Write-Host " Stopping $service on $server..." Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock { param($svcName) Stop-Service -Name $svcName -Force } -ArgumentList $service Write-Host " $service stopped successfully." -ForegroundColor Green } else { Write-Host " $service is not running." } } catch { Write-Host " Error with $service on $server: $_" -ForegroundColor Red } } }