0
votes

i'm new at powershell scripts

i want to uninstall softwares located in remote computer from my local computer so i'm using

$computer =Read-Host -Prompt 'Input your server name or IP adress'
$user =Read-Host -Prompt 'Input your server username'
Invoke-Command -ComputerName $computer -ScriptBlock {  

Write-host -ForegroundColor Magenta "Please select the software you wish to uninstall..."


    $javaVer = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" | select DisplayName, UninstallString, InstallLocation, InstallDate | out-gridview -PassThru



    write-host -ForegroundColor Yellow "The following software will be uninstalled:"




ForEach ($ver in $javaVer) {

    If ($ver.UninstallString) {

        $uninst = $ver.UninstallString
        & cmd /c $uninst /quiet /norestart
    }

}
} -credential $user

testing it in my local machine i got this error

Out-GridView ne fonctionne pas à partir d’une session à distance. + CategoryInfo : InvalidOperation : (Microsoft.Power...GridViewCommand:OutGridViewCommand) [Out-GridView] , NotSupportedException + FullyQualifiedErrorId : RemotingNotSupported,Microsoft.PowerShell.Commands.OutGridViewCommand + PSComputerName : 192.168.1.200

google translated:

Out-GridView does not work from a remote session. + CategoryInfo: InvalidOperation: (Microsoft.Power ... GridViewCommand: OutGridViewCommand) [Out-GridView], NotSupportedException + FullyQualifiedErrorId: RemotingNotSupported, Microsoft.PowerShell.Commands.OutGridViewCommand + PSComputerName: 192.168.1.200

but if i send a simple command like ipconfig ,it work

What i'm doing wrong ?

1
the Out-GridView cmdlet requires that one be in an interactive session. the session that Invoke-Command uses is both on the other system AND non-interactive. if you need user interaction, split your remote calls into two parts & use the returned info from the 1st in your local Out-GridView display. - Lee_Dailey
@Lee_Dailey sorry i'm still learning powershell scripts, can you modify the code and post it as an answer please? - user11845248
are you wanting to just uninstall a specific app - java, perhaps? that is implied by your $JavaVer variable name ... - Lee_Dailey
@Lee_Dailey no, i want to choose from the opened list the software that i want to uninstall or input the name of the software but choosing from the list is easier for me - user11845248
OK ... i think the code in the ANSWER i just posted will work, but i cannot fully test is since i have only one system - and no apps that i want to uninstall. [grin] - Lee_Dailey

1 Answers

0
votes

since you CANNOT run Out-GridView in a remote, non-interactive session [what you get from Invoke-Command], this breaks the process into steps ...

  • asks for the target system name
  • sets up two I-C scriptblocks
  • gets the remote installed app list via I-C & the 1st scriptblock
  • shows that via O-GV for the user to choose apps
  • shows the to-be-uninstalled apps
  • does the actual remote uninstall via a 2nd call to I-C using the 2nd scriptblock

note that the actual command to uninstall the apps has been converted to an expanded string. remove the double quotes around "& cmd /c $uninst /quiet /norestart" when ready to really do it.

also, there is NO error handling at all. [grin]

# the following likely should include a test to see if the system actually exists
$ComputerName = Read-Host -Prompt 'Input your server name or IP adress '
#$user = Read-Host -Prompt 'Input your server username'
Write-Host ''

$ICScriptblock_One = {
    $HKLM_TargetList = @(
        "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"
        "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
        )
    # send the results to the calling system
    Get-ItemProperty -Path $HKLM_TargetList |
        Where-Object {
            # filter out items with blanks in the follwing properties
            $_.DisplayName -and
            $_.UninstallString
            } |
        Sort-Object -Property DisplayName |
        Select-Object -Property DisplayName, UninstallString, InstallLocation, InstallDate
    } # end >>> $ICScriptblock_One = {

$ICScriptblock_Two = {
    ForEach ($ATR_Item in $Using:AppsToRemove)
        {
        $uninst = $ATR_Item.UninstallString
        # remove the double quotes when ready to do this for real
        "& cmd /c $uninst /quiet /norestart"
        }
    } # end >>> $ICScriptblock_Two = {

$InstalledAppList = Invoke-Command -ComputerName $ComputerName -ScriptBlock $ICScriptblock_One

$OGVMessage = 'Please select the software you wish to uninstall...'
$AppsToRemove = $InstalledAppList |
    Out-GridView -Title $OGVMessage -OutputMode Multiple

write-host -ForegroundColor Yellow "The following software will be uninstalled :"
$AppsToRemove.DisplayName |
    Out-Host
Write-Host ''

Invoke-Command -ComputerName $ComputerName -ScriptBlock $ICScriptblock_Two

output on screen ...

Input your server name or IP adress : localhost

The following software will be uninstalled :
Apple Software Update
Google Update Helper
Python 3.7.4 Utility Scripts (64-bit)
Speccy

& cmd /c MsiExec.exe /I{A30EA700-5515-48F0-88B0-9E99DC356B88} /quiet /norestart
& cmd /c MsiExec.exe /I{60EC980A-BDA2-4CB6-A427-B07A5498B4CA} /quiet /norestart
& cmd /c MsiExec.exe /I{16F74529-EDE0-4BBD-B2AF-89AF9C696EA8} /quiet /norestart
& cmd /c "C:\Program Files\Speccy\uninst.exe" /quiet /norestart