Hello,
I am fairly new to PowerShell and I started working on a script to uninstall application from machines. I was watching the Getting Started with PowerShell course on MVA and I saw the cmdletbinding which can be used to prompt users for parameters and I thought I could use this fairly easy. So I started messing with a small script and now it has become larger than anything I ever created since I decided to add conditional statements.
Basically what I would like to do is there are multiple computer and applications, if Y the parameter become strings. Afterwards list all the application installed on the machine/s (and add machine name to the list). I am guessing that I would need to add-member the computer name. The reason behind this list is to have the exact name for the application that would be uninstalled if there are better ways to go about this I am all for it.
So the problem that I having with my script is that after the first user the next parameter is not prompted and then script errors. Any suggestions? and if you have time explanations so I can better understand why my script is not working.
Thanks in advance.
I have made some changes to the script. I am not sure which would be the best way to proceed.
This is the old script.
$multicomp= Read-Host "Do you want to target multiple computers? Y/N" #For multiple computers if ($multicomp -match 'Y' -or 'y') { {[CmdletBinding()] Param( #Enter the computer name. [Parameter(Mandatory=$True)] [string[]]$computer ) } } #For single computer elseif ($multicomp -match 'N' -or 'n') { {[CmdletBinding()] Param( #Enter the computer name. [Parameter(Mandatory=$True)] $computer ) } } else { Write-Host "Invalid Input. Please Try Again" } Get-WmiObject Win32_Product -ComputerName $computer | Add-Member -NotePropertyName ComputerName -NotePropertyValue $computer | Select Name, Version, Caption | Sort-Object Name | Out-GridView Write-Host " Do you want to uninstall multiple applications? Y/N" $multiapp= Read-Host #for multiple applications if ($multiapp -match 'Y' -or 'y') { {[CmdletBinding()] Param( #Enter the application name. Name must be exact. [Parameter(Mandatory=$True)] [string[]]$appname ) } } #for single application elseif ($multiapp -match 'N' -or 'n') { {[CmdletBinding()] Param( #Enter the application name. Name must be exact. [Parameter(Mandatory=$True)] $appname ) } } else { Write-Host "Invalid Input. Please Try Again" } #Uninstall applicaiton based on the name value $app= Get-WmiObject Win32_Product -ComputerName $computer | where { $_.name -eq $appname } $app.Uninstall() #Display success message. $message= "$appname has been succesfully uninstalled from $computer. Press any key to continue..." Write-Host $message #Wait for Key input to close. $host.UI.RawUI.ReadKey()
New script
$multicomp= Read-Host "Do you want to target multiple computers? Y/N" #For multiple computers If ($multicomp -eq "Y" -or "y") { $computer = @() do { $input = (Read-Host "Enter Computer Names") if ($input -ne '') { $computer += $input } } until ($input -eq'') } #For single computer ElseIf ($multicomp -eq "N" -or "n") { $computer = Read-Host "Enter Computer Name" } Else { Write-Host "Invalid Input. Please Try Again" } Get-WmiObject Win32_Product -ComputerName $computer | Select Name, Version, Caption | Sort-Object Name | Out-GridView $multiapp= Read-Host " Do you want to uninstall multiple applications? Y/N" #for multiple applications If ($multiapp -eq 'Y' -or 'y') { $appname = @() do { $input = (Read-Host "**Application names must be exact** Enter Application Names") if ($input -ne '') { $appname += $input } } until ($input -eq'') } #for single application ElseIf ($multiapp -eq 'N' -or 'n') { $appname = Read-Host "**Application name must be exact** Enter Application Name" } Else { Write-Host "Invalid Input. Please Try Again" } #Uninstall applicaiton based on the name value $app= Get-WmiObject Win32_Product -ComputerName $computer | where { $_.name -eq $appname } $app.Uninstall() #Display success message. $message= "$appname has been succesfully uninstalled from $computer. Press any key to continue..." Write-Host $message #Wait for Key input to close. $host.UI.RawUI.ReadKey()