使用PowerShell卸载程序的脚本

时间:2014-10-23 12:19:20

标签: powershell

我是Powershell的新手,我想尝试用它卸载软件。我搜索了几个网站,但我找不到一个简单的脚本,允许我在我选择的计算机上卸载软件。有没有人有我可以使用的脚本?

3 个答案:

答案 0 :(得分:0)

看看Get-WMIObject -class Win32_Product

有一种名为uninstall

的方法

我创建了一个搜索和删除应用程序的函数

<#
    .SYNOPSIS
        Query WMI for MSI installed applications.

    .DESCRIPTION
        Queries WMI for applications installed by MSI by calling the Win32_Product class,
        all parameters are optional and the -Computer Parameter can be piped to by name only and have multiple entries.

    .PARAMETER  Computername
        Specifies the computer to query, can be an array of computers and can be name piped to.
        Default is localhost.

    .PARAMETER  Software
        Used to search for specific software, e.g *java* or "Java Auto Updater". Wrap strings with Double quotations (").
        Default is "*" wildcard meaning all items.

    .PARAMETER  Uninstall
        Uninstalls all found software using the Uninstall() Method.

    .PARAMETER  Credentials
        This Switch specifies credentials for remote machines. 
        Just using -Credentials will prompt you to enter your credentials when executing the command.

    .PARAMETER Showprogress

        Shows the progress on a per machine basis(works better if you have a few machines to check on).
        Default is off.

    .EXAMPLE
        This will query the local machine for anything installed like "Java" and return it to screen.
        PS C:\> Get-SoftwareNT -Software "*java*"

        This will export all Software installed in the local machine to a file called File.csv With only the Name and installed date selected.
        PS C:\> Get-SoftwareNT | Select-Object -Property name,InstallDate | Export-Csv -Path C:\MyPath\File.csv -NoTypeInformation

        This Searches for software on the computers in the file C:\ListofComputers.txt that match "*Java*" and return it to screen.
        PS C:\> Get-SoftwareNT -Software "*java*" -Computername (Get-Content -Path C:\ListofComputers.txt)

    .EXAMPLE
        This will ask you for your credentials and query the machine BAD-USER and BAD-USER2 for any software that matches the term "*itunes*" and returns it to screen with a progress bar.
        PS C:\> Get-SoftwareNT -Credentials -Computername BAD-USER,BAD-USER2 -Software "*iTunes*" -showprogress

        This will to the same as the command above but then uninstall the software and display a return code
        (0 usually means it was successful, 1603 usually means no permissions)
        PS C:\> Get-SoftwareNT -Credentials -Computername BAD-USER,BAD-USER2 -Software "*iTunes*" -Uninstall

    .NOTES
        Please note that the Win32_Product is quite inefficient as it uses windows installer to query every application installed to gather
        the information and dose a "reconfiguration" in the process, this is bad if you have disabled a service for a msi installed app because in the process it
        dose a repair on the app and if there are any issues it will fix them first and then move on and this means reactivating disabled services etc.


#>

function Get-SoftwareNT {

    [CmdletBinding()] 

        param(

            [Parameter(ValueFromPipelineByPropertyName=$True,ValueFromPipeline=$True)]
            [System.String[]]$Computername = "localhost",

            $Software = "*",

            [Switch]$Uninstall = $false,

            [Switch]$Credentials = $false,

            [Switch]$Showprogress = $false
        )

    BEGIN { 

        $NTErrorLog = "C:\NTErrorLogs\Get-SoftwareNT.txt"
        $Path_Test = Test-Path -Path $NTErrorLog
        $Encoding = "ASCII"

        if (!($Path_Test)) {New-Item -Path (Split-Path $NTErrorLog -Parent) -ItemType Container -ErrorAction SilentlyContinue ; New-Item -Path (Split-Path $NTErrorLog -Parent) -Name (Split-Path $NTErrorLog -Leaf) -ItemType file -Value "Unreachable machines: `n" | Out-Null}
        else {Get-Content -Path $NTErrorLog -Force | Out-File -FilePath ("$NTErrorLog" + '_') -Append -Force -Encoding $Encoding ; Clear-Content $NTErrorLog -Force | Out-Null}

        if ($Credentials) { $credstore = Get-Credential }

        $Count = (100 / ($Computername).Count -as [Decimal])
        [Decimal]$Complete = "0"


        }

    PROCESS{

        Foreach ($Computer in $Computername) {

        try {

            $Computer_Active = $true
            $TestConnection = Test-Connection -Count 1 -ComputerName $Computer -ea Stop

        } catch {

            Write-Warning -Message "$computer was not reachable Logging to $NTErrorLog"
            $Computer | Out-File -FilePath $NTErrorLog -Append -Encoding $Encoding -Force
            $Computer_Active = $false

        }
            if ($Computer_Active) {
                if ($Showprogress) {Write-Progress -Activity "Looking for $Software on $Computer" -CurrentOperation "Connecting to WMI on $Computer" -PercentComplete $Complete}

                if ($Credentials) { $Var1 = Get-WmiObject -Credential $credstore -Class Win32_Product -ComputerName $Computer | Where-Object {$_.name -Like "$Software"} }
                else { $Var1 = Get-WmiObject -Class Win32_Product -ComputerName $Computer | Where-Object {$_.name -Like "$Software"} }

                Write-Output -InputObject $Var1

                if ($Uninstall) { $Var1.Uninstall()  }

                $Complete +=$Count

                }

            if ($Showprogress) {Write-Progress -Activity "Finished with $Computer" -CurrentOperation "Done" -PercentComplete $Complete} 
        }


        if ($Showprogress) {Write-Progress -Activity "Done" -Completed}

    }
    END{}
}

答案 1 :(得分:0)

我创建了(在许多其他软件管理功能中)Remove-Software script

这就是你所要求的。但是,由于软件供应商的固有工作在90%的时间内没有坚持标准,因此可能需要针对您的情况进行调整。如果没有,这将使你非常接近。

答案 2 :(得分:0)

此Scrpts使用Win32_product属性获取安装在ws上的软件列表。然后使用Class Key卸载软件。

$ classkey =“IdentifyingNumber = "$($classKey1.IdentifyingNumber)"",Name=""$($classKey1.Name)"",Version=""$($classKey1.Version)”“

修改上述$ class键以适合“System.Management.ManagementObject”

形式
                # Load Windows Forms assembly

            [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
            [void][System.Windows.Forms.Application]::EnableVisualStyles()

            # Create a GUI

            $form = New-Object System.Windows.Forms.Form
            $form.text = "Software Uninstall"
            $form.Size = New-Object System.Drawing.Size(920,550)
            $form.BackColor='SkyBlue'
            $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
            $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
            $dataGridView = New-Object System.Windows.Forms.DataGridView
            $dataGridView.Size = New-Object System.Drawing.Size(900,400)
            $dataGridView.DefaultCellStyle.BackColor = 'LightBlue'

            #Button 

            $button = New-Object System.Windows.Forms.Button
            $button.Location = New-Object System.Drawing.Size(400,420)
            $button.Size = New-Object System.Drawing.Size(75,25)
            $button.BackColor = "LightGreen"
            $button.text = "Refresh"
            $form.Controls.Add($button)
            $form.Controls.Add($dataGridView)

            # Note Label
            $Label0 = New-Object System.Windows.Forms.Label
            $Label0.Location = New-Object System.Drawing.Size(15,450)
            $Label0.Size = New-Object System.Drawing.Size(880,50)
            $Label0.Backcolor = [System.Drawing.Color]::Yellow
            $Label0.Font = New-Object System.Drawing.Font("Calibri",14,[System.Drawing.FontStyle]::Bold) 
            $Label0.Text = "Note: This Script Cannot Uninstall Policy Restricted Apps such as McAfee etc. Repair option is not Available in this Version. For Reinstall of Software Kindly use SCCM."
            $form.Controls.Add($Label0) 


            # Select appropriate columns

            $dataGridView.Columns.Insert(0, (New-Object System.Windows.Forms.DataGridViewButtonColumn))
            $dataGridView.ColumnCount = 8
            $dataGridView.ColumnHeadersVisible = $true
            #$dataGridView.HeaderText = "Uninstall"
            $dataGridView.Columns[0].Name = "Uninstall"
            $dataGridView.Columns[1].Name = "Description"
            $dataGridView.Columns[2].Name = "IdentifyingNumber"
            $dataGridView.Columns[3].Name = "Name"
            $dataGridView.Columns[4].Name = "Vendor"
            $dataGridView.Columns[5].Name = "Version"
            $dataGridView.Columns[6].Name = "Caption"
            $dataGridView.Columns[7].Name = "InstallLocation"

            $dataGridView.Columns[0].width = 50
            $dataGridView.Columns[1].width = 200

            # Get a list of items

            Get-WmiObject -Class Win32_Product | foreach {
                $dataGridView.Rows.Add("Uninstall",$_.Description,$_.IdentifyingNumber,$_.Name,$_.Vendor,$_.Version,$_.Caption,$_.InstallLocation) | out-null
            }


            # Refresh

            function gridClick(){
                                    $rowIndex = $dataGridView.CurrentRow.Index
                                    $columnIndex = $dataGridView.CurrentCell.ColumnIndex

                                    If ($columnIndex -eq '0')
                                        {
                                            $columnIndex0 = $dataGridView.ColumnIndex+1
                                            $columnIndex1 = $dataGridView.ColumnIndex+2
                                            $columnIndex2 = $dataGridView.ColumnIndex+3
                                            $columnIndex3 = $dataGridView.ColumnIndex+4
                                            $columnIndex5 = $dataGridView.ColumnIndex+5

                                            $IdentifyingNumber = $dataGridView.Rows[$rowIndex].Cells[$columnIndex1].value
                                            $Name = $dataGridView.Rows[$rowIndex].Cells[$columnIndex0].value
                                            $Version = $dataGridView.Rows[$rowIndex].Cells[$columnIndex5].value

                                            Write-Host $dataGridView.Rows[$rowIndex].Cells[$columnIndex0].value
                                            Write-Host $dataGridView.Rows[$rowIndex].Cells[$columnIndex1].value
                                            Write-Host $dataGridView.Rows[$rowIndex].Cells[$columnIndex5].value


                                            $classKey1 = New-Object PSObject -Property @{
                                                                                            IdentifyingNumber=$IdentifyingNumber
                                                                                            Name=$Name
                                                                                            Version=$Version
                                                                                        }| select IdentifyingNumber, Name, Version

                                            # Write-Host $classKey


                                            $classkey = "IdentifyingNumber=`"$($classKey1.IdentifyingNumber)"",Name=""$($classKey1.Name)"",Version=""$($classKey1.Version)`""


                                            $wshell = New-Object -ComObject Wscript.Shell

                                            $OUTPUT = [System.Windows.Forms.MessageBox]::Show("You Have Selected $Name. Do You Want to Proceed With The Uninstall", "Warning!!!", 4)

                                            if ($OUTPUT -eq "YES" ){       

                                                    Try{

                                                         ([wmi]”\\localhost\root\cimv2:Win32_Product.$classKey”).uninstall()


                                                       }
                                                    Catch{
                                                            Write-Warning $_
                                                         }
                                                }
                                        <#    Else
                                                {
                                                    $wshell = New-Object -ComObject Wscript.Shell

                                                    $wshell.Popup("You Have Selected No, Click Ok to Continue" ,0,"Warning!!!",0x0)
                                                }  
                                        }#>
                                   # Vaid Cell 
                                  <# Else
                                        {   

                                            $wshell = New-Object -ComObject Wscript.Shell

                                            $OUTPUT = $wshell.Popup("You Have Selected Invalid Column. Please click on Uninstall Button" ,0,"Warning!!!",0x0)   
                                        } #>
                                }


            $button.Add_Click({
                $selectedRow = $dataGridView.CurrentRow.Index

                $dataGridView.Rows.Clear()

                start-sleep -s 7

            Get-WmiObject -Class Win32_Product | foreach {
                $dataGridView.Rows.Add("Uninstall",$_.Description,$_.IdentifyingNumber,$_.Name,$_.Vendor,$_.Version,$_.Caption,$_.InstallLocation) | out-null
            }

            })

            $Uninstall = $dataGridView.add_DoubleClick({gridClick})


            # Show the form

            [void]$form.ShowDialog()