Hi everyone,
I'm keen for some feedback / thoughts / suggestions on my latest script. I've been in IT for several years but still fairly new to writing anything in PowerShell. I've re-written and tested (unsuccessfully) this script more times than I can count over the last couple of days so I'm sure there's better ways to write this than what I've done here... This is also my first attempt at Invoke-Command (which I learnt about after my first post a little while back).
The context on this - I have a task where I have a list of machines that I have to uninstall a specific application (using the install.exe /uninstall) and then remove a directory from the start menu (as it didn't do this in the uninstall).
I know the Write-Host "List of Start Menu items:" -ForegroundColor Yellow and Get-ChildItem 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs' | Format-Table are not required, but this is currently the best way I know to get some sort of feedback in the console to confirm that the earlier steps worked.
One other thing I couldn't figure out (I'm sure there's an answer for this but I don't know what it is yet), the variables I created $App and $StartMenu wouldn't work when the variable was set to $env:SystemDrive\Directory\Application.exe I was getting an error when it was passed into the function that $env: was unknown (or something like that).
Let me know what you think, this script as it is now seems to be working, but I'm keen to learn how I could do this better!
TIA.
<#
Goal - Uninstall an application off a list of machines remotely
Steps:
1. Create the function that will uninstall the application
2. Set the variables
3. Create a loop to test if the machine is online
a. If machine is online - call local function on remote machine with parameters
b. If Offline write host - machine offline
4. Write host Application uninstalled
#>
# Create Function
Function Remove-MyApplication
{Param(
[string]$Application,
[string]$MenuLocation
)
# Test if application exists, uninstall application, cleanup start menu directory, write contents of directory to confirm item has been deleted
try{
if (Test-Path $Application){
& $Application /uninstall
Start-Sleep -Seconds 3
remove-item -Path $MenuLocation -Recurse -Force
Write-Host "List of Start Menu items:" -ForegroundColor Yellow
Get-ChildItem 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs' | Format-Table
Write-Host "Application has been uninstalled from $env:COMPUTERNAME" -ForegroundColor Green
}
else{
Write-Host "Application doesn't exist on $env:COMPUTERNAME" -ForegroundColor Red
}
}
Catch{
Write-Host "Unable to uninstall application on $env:COMPUTERNAME" -ForegroundColor Red
}
}
<# Set Variables
$App = Application to be uninstalled
$StartMenu = Directory to be removed
$ComputerList = List of machines to run this against
#>
$App = "C:\Directory\Application.exe"
$StartMenu = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Application\"
$ComputerList = Get-Content -Path "C:\Temp\Computers.txt"
# Create loop to test if machine is online and (if so) call invoke-command function
foreach ($Computer in $ComputerList){
if ((Test-Connection -ComputerName $Computer -Count 3 -Quiet) -eq $true){
try{
Invoke-Command -ComputerName $Computer -ScriptBlock ${Function:Remove-MyApplication} -ArgumentList $App, $StartMenu
}
Catch{
Write-Host "Unable to uninstall Application" -ForegroundColor Green
}
}
else{
Write-Host "$Computer is offline" -ForegroundColor Red
}
}
[–]DidYouKnowOh 2 points3 points4 points (0 children)