105 lines
3.8 KiB
PowerShell
105 lines
3.8 KiB
PowerShell
# Deploy bloatware removal to remote computers
|
|
# Requires Administrator privileges and PSRemoting enabled on target machines
|
|
# Run as Administrator
|
|
|
|
#Requires -RunAsAdministrator
|
|
|
|
param(
|
|
[Parameter(Mandatory=$true, HelpMessage="Computer name or IP address")]
|
|
[string[]]$ComputerName,
|
|
|
|
[Parameter(Mandatory=$false)]
|
|
[string]$LocalScriptPath = (Split-Path -Parent $MyInvocation.MyCommand.Path) + '\Remove-AllBloat.ps1',
|
|
|
|
[Parameter(Mandatory=$false)]
|
|
[switch]$RunImmediately = $false,
|
|
|
|
[Parameter(Mandatory=$false)]
|
|
[PSCredential]$Credential
|
|
)
|
|
|
|
$ErrorActionPreference = 'Continue'
|
|
|
|
if (-not (Test-Path $LocalScriptPath)) {
|
|
Write-Host "[-] Script not found: $LocalScriptPath" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "[*] Starting deployment to $($ComputerName.Count) computer(s)..."
|
|
Write-Host ""
|
|
|
|
$RemoteScriptPath = "C:\Windows\Temp\Remove-AllBloat.ps1"
|
|
$Results = @()
|
|
|
|
foreach ($Computer in $ComputerName) {
|
|
Write-Host "[*] Processing: $Computer"
|
|
|
|
try {
|
|
# Test connection
|
|
$Connection = Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue
|
|
if (-not $Connection) {
|
|
Write-Host " [-] Computer unreachable or PSRemoting not enabled" -ForegroundColor Yellow
|
|
$Results += @{Computer = $Computer; Status = "Unreachable"; Message = "PSRemoting not responding" }
|
|
continue
|
|
}
|
|
|
|
# Copy script to remote computer
|
|
Write-Host " [*] Copying script..."
|
|
$Session = New-PSSession -ComputerName $Computer -Credential $Credential -ErrorAction Stop
|
|
|
|
Copy-Item -Path $LocalScriptPath -Destination $RemoteScriptPath -ToSession $Session -Force
|
|
|
|
# Execute script
|
|
Write-Host " [*] Executing script..."
|
|
$Output = Invoke-Command -Session $Session -ScriptBlock {
|
|
param($ScriptPath)
|
|
& powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File $ScriptPath -ErrorAction SilentlyContinue
|
|
return "Script executed"
|
|
} -ArgumentList $RemoteScriptPath -ErrorAction Stop
|
|
|
|
# Clean up remote copy
|
|
Invoke-Command -Session $Session -ScriptBlock {
|
|
Remove-Item -Path $args[0] -Force -ErrorAction SilentlyContinue
|
|
} -ArgumentList $RemoteScriptPath
|
|
|
|
Remove-PSSession -Session $Session
|
|
|
|
Write-Host " [+] Success" -ForegroundColor Green
|
|
$Results += @{Computer = $Computer; Status = "Success"; Message = "Bloatware removal executed" }
|
|
|
|
} catch {
|
|
Write-Host " [-] Error: $_" -ForegroundColor Red
|
|
$Results += @{Computer = $Computer; Status = "Failed"; Message = $_.Exception.Message }
|
|
}
|
|
|
|
Write-Host ""
|
|
}
|
|
|
|
# Summary
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host "Deployment Summary" -ForegroundColor Cyan
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
|
|
$Success = $Results | Where-Object { $_.Status -eq "Success" } | Measure-Object | Select-Object -ExpandProperty Count
|
|
$Failed = $Results | Where-Object { $_.Status -eq "Failed" } | Measure-Object | Select-Object -ExpandProperty Count
|
|
$Unreachable = $Results | Where-Object { $_.Status -eq "Unreachable" } | Measure-Object | Select-Object -ExpandProperty Count
|
|
|
|
Write-Host "Total Computers: $($Results.Count)"
|
|
Write-Host "Successful: $Success" -ForegroundColor Green
|
|
Write-Host "Failed: $Failed" -ForegroundColor Red
|
|
Write-Host "Unreachable: $Unreachable" -ForegroundColor Yellow
|
|
|
|
Write-Host ""
|
|
Write-Host "Detailed Results:" -ForegroundColor Cyan
|
|
foreach ($Result in $Results) {
|
|
$Color = switch ($Result.Status) {
|
|
"Success" { "Green" }
|
|
"Failed" { "Red" }
|
|
"Unreachable" { "Yellow" }
|
|
}
|
|
Write-Host " $($Result.Computer): $($Result.Status) - $($Result.Message)" -ForegroundColor $Color
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "[+] Deployment complete!"
|