2014-10-23 2 views
-3

Im new to Powershell 그리고 나는 그것으로 소프트웨어를 제거하려고합니다. 여러 웹 사이트를 검색했지만 내 컴퓨터에서 소프트웨어를 제거 할 수있는 간단한 스크립트를 찾을 수 없습니다. 누구든지 사용할 수있는 스크립트가 있습니까?powershell을 사용하여 프로그램을 제거하는 스크립트

+0

. 당신은 사용할 수 있습니다 = PowerShell을 사용하여 WMI를 호출하거나 MSIExec을 사용하거나 문제의 소프트웨어에서 제공하는 제거 방법을 사용하십시오. 소프트웨어를 제거하는 한 가지 방법은 없습니다. _properly_. http://stackoverflow.com/questions/ 113542/how-can-i-uninstall-an-application-using-powershell – Matt

답변

0

가져-WMIObject에서 -class Win32_Product와 한 번 봐는

많은 사이 (I 응용 프로그램을 내가 만든

<# 
    .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{} 
} 
+0

나는 당신을 인용하지 말라고 꾸짖 으려했습니다. 소스. http://community.spiceworks.com/scripts/show/2354-get-softwarent-cmdlet-updated – Matt

+0

Win32_Product를 절대로 사용하지 마십시오. 쿼리 할 때마다 강제로 MSI 검사를 수행합니다. http://myitforum.com/cs2/blogs/gramsey/archive/2011/01/25/win32-product-is-evil.aspx –

+0

.NOTES에서 나는 그것을 복용한다고 언급했다. 가장 효율적인 방법은 아니지만 애드혹 솔루션으로 작업을 수행합니다. 더 좋은 방법이 있습니다. 이것이 할 수있는 방법 중 하나 일뿐입니다. –

0

를 검색하고 제거하는 함수를 만들어

제거라는 방법이있다 기타 소프트웨어 관리 기능) Remove-Software script

당신이 묻는 것만합니다. 그러나 소프트웨어 공급 업체의 고유 한 작업으로 인해 표준 90 %를 고수하지 않아서 상황에 맞게 조정해야 할 수도 있습니다. 이 방법을 사용하면 극단적으로 닫을 수 있습니다.

0

이 Scrpts는 Win32_product 속성을 사용하여 ws에 설치된 소프트웨어 목록을 가져옵니다. 그런 다음 클래스 키를 사용하여 소프트웨어를 제거합니다.

$의 classkey은 "따라 IdentifyingNumber = "$($classKey1.IdentifyingNumber)"",Name=""$($classKey1.Name)"",Version=""$($classKey1.Version)" "

위의 $ 클래스 키의 형식은"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() 
+0

답변은 항상 높이 평가되지만 실제로 코드가 어떻게 문제를 해결하는지에 대한 정보를 제공하는 것이 좋습니다. 답변을 둘러싼 배경을 제공하고 훌륭한 답변을 작성하는 방법에 대한 정보는 [도움말] (http://stackoverflow.com/help/how-to-answer)을 참조하십시오. –

+1

의견을 보내 주셔서 감사합니다 ... 필수 정보를 추가했습니다. – Deep

관련 문제