PowerShell Script String Issue

Welcome Forums General PowerShell Q&A PowerShell Script String Issue

Viewing 1 reply thread
  • Author
    Posts
    • #241232
      Participant
      Topics: 1
      Replies: 0
      Points: 13
      Rank: Member

      Most of the script is working as expected, however I hit a snag at the end. The part where it says Where DisplayName -eq $String refuses to work even with a simple hard-coded variable like $String = "Pulse Secure". If I instead type in Where DisplayName -eq "Pulse Secure" it magically works. I’ve commented out a bit of the code at the end and have just been using Write-Host $UninstallString to test if the string is processing properly (it should print the msiexec uninstall line if it works correctly). The end goal is to make $String = the program from the listbox that the user has selected, which should work once I figure this string issue out.

      Add-Type -AssemblyName System.Windows.Forms
      Add-Type -AssemblyName System.Drawing
      
      $main_form = New-Object System.Windows.Forms.Form
      $main_form.Text ='Remote Program Uninstaller by Maconi'
      $main_form.Width = 700
      $main_form.Height = 460
      
      $ComputerLabel = New-Object System.Windows.Forms.Label
      $ComputerLabel.Text = "Computer:"
      $ComputerLabel.Location = New-Object System.Drawing.Point(5,10)
      $ComputerLabel.AutoSize = $true
      $main_form.Controls.Add($ComputerLabel)
      
      $ComputerTextBox = New-Object System.Windows.Forms.TextBox
      $ComputerTextBox.Width = 100
      $ComputerTextBox.Location = New-Object System.Drawing.Point(80,10)
      $main_form.Controls.Add($ComputerTextBox)
      
      $ProgramComboBox = New-Object System.Windows.Forms.ListBox
      $ProgramComboBox.Location = New-Object System.Drawing.Point(40,90)
      $ProgramComboBox.Height = 300
      $ProgramComboBox.Width = 600
      $main_form.Controls.Add($ProgramComboBox)
      
      $StatusLabel = New-Object System.Windows.Forms.Label
      $StatusLabel.Text = "Status:"
      $StatusLabel.Location = New-Object System.Drawing.Point(5,60)
      $StatusLabel.AutoSize = $true
      $main_form.Controls.Add($StatusLabel)
      
      $ErrorLabel = New-Object System.Windows.Forms.Label
      $ErrorLabel.Text = ""
      $ErrorLabel.Location = New-Object System.Drawing.Point(125,60)
      $ErrorLabel.AutoSize = $true
      $main_form.Controls.Add($ErrorLabel)
      
      $ComputerButton = New-Object System.Windows.Forms.Button
      $ComputerButton.Location = New-Object System.Drawing.Size(200,7)
      $ComputerButton.Size = New-Object System.Drawing.Size(120,25)
      $ComputerButton.Text = "Connect"
      $main_form.Controls.Add($ComputerButton)
      
      $UninstallButton = New-Object System.Windows.Forms.Button
      $UninstallButton.Location = New-Object System.Drawing.Size(500,50)
      $UninstallButton.Size = New-Object System.Drawing.Size(120,25)
      $UninstallButton.Text = "Uninstall"
      $main_form.Controls.Add($UninstallButton)
      
      $ComputerButton.Add_Click(
      {
      $ErrorLabel.Text = "Processing..."
      If (Test-Connection -ComputerName $ComputerTextBox.Text -Quiet)
      {
      $ProgramComboBox.Items.Clear()
      
      $ProgramList = Invoke-Command -ComputerName $ComputerTextBox.Text { Get-ChildItem -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall' | Get-ItemProperty | Select-Object -ExpandProperty DisplayName }
      
      foreach ($Program in $ProgramList) { If (![string]::IsNullOrEmpty($Program)) {$ProgramComboBox.Items.Add($Program)} }
      
      $ProgramComboBox.Sorted = $true
      
      $ErrorLabel.Text = "Programs Loaded!"
      }
      
      Else
      {
      $ErrorLabel.Text = "Computer Offline!"
      }
      }
      )
      
      $UninstallButton.Add_Click(
      {
      $ErrorLabel.Text = "Processing..."
      
      If (Test-Connection -ComputerName $ComputerTextBox.Text -Quiet)
      {
      $String = "Pulse Secure"
      
      #$ProgramComboBox.SelectedItem
      
      $UninstallString = Invoke-Command -ComputerName $ComputerTextBox.Text { Get-ChildItem -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall' | Get-ItemProperty | Where DisplayName -eq $String | Select-Object -ExpandProperty UninstallString }
      
      Write-Host $UninstallString
      
      #$Uninstall = "$UninstallString REBOOT=ReallySuppress /q"
      #([WMICLASS]"\\$Computer\ROOT\CIMV2:Win32_Process").Create($Uninstall)
      #$ErrorLabel.Text = "$String Uninstalled!"
      }
      
      Else
      {
      $ErrorLabel.Text = "Computer Offline!"
      }
      }
      )
      
      $main_form.ShowDialog()
      
    • #241346
      Participant
      Topics: 13
      Replies: 1690
      Points: 2,876
      Helping Hand
      Rank: Community Hero

      Use the label, not Write-Host to debug:

      Add-Type -AssemblyName System.Windows.Forms
      Add-Type -AssemblyName System.Drawing
      
      $main_form = New-Object System.Windows.Forms.Form
      $main_form.Text ='Remote Program Uninstaller by Maconi'
      $main_form.Width = 700
      $main_form.Height = 460
      
      $ComputerLabel = New-Object System.Windows.Forms.Label
      $ComputerLabel.Text = "Computer:"
      $ComputerLabel.Location = New-Object System.Drawing.Point(5,10)
      $ComputerLabel.AutoSize = $true
      $main_form.Controls.Add($ComputerLabel)
      
      $ComputerTextBox = New-Object System.Windows.Forms.TextBox
      $ComputerTextBox.Width = 100
      $ComputerTextBox.Location = New-Object System.Drawing.Point(80,10)
      $main_form.Controls.Add($ComputerTextBox)
      
      $ProgramComboBox = New-Object System.Windows.Forms.ListBox
      $ProgramComboBox.Location = New-Object System.Drawing.Point(40,90)
      $ProgramComboBox.Height = 300
      $ProgramComboBox.Width = 600
      $main_form.Controls.Add($ProgramComboBox)
      
      $StatusLabel = New-Object System.Windows.Forms.Label
      $StatusLabel.Text = "Status:"
      $StatusLabel.Location = New-Object System.Drawing.Point(5,60)
      $StatusLabel.AutoSize = $true
      $main_form.Controls.Add($StatusLabel)
      
      $ErrorLabel = New-Object System.Windows.Forms.Label
      $ErrorLabel.Text = ""
      $ErrorLabel.Location = New-Object System.Drawing.Point(125,60)
      $ErrorLabel.AutoSize = $true
      $main_form.Controls.Add($ErrorLabel)
      
      $ComputerButton = New-Object System.Windows.Forms.Button
      $ComputerButton.Location = New-Object System.Drawing.Size(200,7)
      $ComputerButton.Size = New-Object System.Drawing.Size(120,25)
      $ComputerButton.Text = "Connect"
      $main_form.Controls.Add($ComputerButton)
      
      $UninstallButton = New-Object System.Windows.Forms.Button
      $UninstallButton.Location = New-Object System.Drawing.Size(500,50)
      $UninstallButton.Size = New-Object System.Drawing.Size(120,25)
      $UninstallButton.Text = "Uninstall"
      $main_form.Controls.Add($UninstallButton)
      
      $ComputerButton.Add_Click({
          $ErrorLabel.Text = "Processing..."
          #If (Test-Connection -ComputerName $ComputerTextBox.Text -Quiet){
              $ProgramComboBox.Items.Clear()
      
              $remoteCmd = {
                  $path = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 
                          'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
      
                  Get-ChildItem -Path $path | 
                  Get-ItemProperty | 
                  Select-Object DisplayName, Version, UninstallString
              }
      
              $Global:ProgramList = Invoke-Command -ScriptBlock $remoteCmd
      
              foreach ($Program in $ProgramList | Where{![string]::IsNullOrEmpty($_.DisplayName)} ) { 
                      $ProgramComboBox.Items.Add($Program.DisplayName)
              }
      
              $ProgramComboBox.Sorted = $true
      
              $ErrorLabel.Text = "Programs Loaded!"
          #}
          #Else {
          #    $ErrorLabel.Text = "Computer Offline!"
          #}
      })
      
      $UninstallButton.Add_Click({
          $ErrorLabel.Text = $programList | Where{$_.DisplayName -eq $ProgramComboBox.SelectedItem} | Select -ExpandProperty UninstallString
      })
      
      $main_form.ShowDialog()
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.