30 Nov 2011 @ 9:32 PM 

A few weeks ago I found myself in a community where someone told me that he did an uninstall of an application on 35 workstations.
Now how did he do that? Through SCCM? RES Automation Manager? Altiris?
No, he went to every device and did it by hand because this customer did not have an automation tool in place.

Now, going to every device and do it by hand is something I would very much dislike… and there are days I wouldn’t even let an intern do it. Instead I would give them an objective: Automate the uninstallation of the application without using additional tools and only use what is available to you.

Since I don’t have an intern anymore, and it wasn’t my customer, there was no need for me to do this… but it got me thinking…

I found this nice little script from Ed ‘the scripting guy’ Wilson where he uses PowerShell to make an inventory of all applications installed in his environment.
Since his script only searches on devices specified in a file, I did some tweaking to search all devices from AD and use those instead of the content of the file.

$computers = Get-ADComputer
$array = @()
foreach($pc in $computers){
$computername=$pc.computername
#Define the variable to hold the location of Currently Installed Programs
$UninstallKey=”SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall”
#Create an instance of the Registry Object and open the HKLM base key
$reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey(‘LocalMachine’,$computername)
#Drill down into the Uninstall key using the OpenSubKey Method
$regkey=$reg.OpenSubKey($UninstallKey)
#Retrieve an array of string that contain all the subkey names
$subkeys=$regkey.GetSubKeyNames()
#Open each Subkey and use GetValue Method to return the required values for each
foreach($key in $subkeys){
$thisKey=$UninstallKey+”\\”+$key
$thisSubKey=$reg.OpenSubKey($thisKey)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name “ComputerName” -Value $computername
$obj | Add-Member -MemberType NoteProperty -Name “DisplayName” -Value $($thisSubKey.GetValue(“DisplayName”))
$obj | Add-Member -MemberType NoteProperty -Name “DisplayVersion” -Value $($thisSubKey.GetValue(“DisplayVersion”))
$obj | Add-Member -MemberType NoteProperty -Name “InstallLocation” -Value $($thisSubKey.GetValue(“InstallLocation”))
$obj | Add-Member -MemberType NoteProperty -Name “Publisher” -Value $($thisSubKey.GetValue(“Publisher”))
$array += $obj
}
}
$array | Where-Object { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion, Publisher | ft –auto

Now that you know which applications are installed, you can use the following script to uninstall them Glimlach

# Usage: RemoveApplication.ps1 “Microsoft Office 2007”
function Remove-Application ($ApplicationName) { $Application = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -match “$ApplicationName” } $Application.Uninstall() }

Posted By: Jeff Wouters
Last Edit: 30 Nov 2011 @ 09:33 PM

EmailPermalinkComments (0)
Tags
 30 Nov 2011 @ 2:57 PM 

Sometimes I’m at a customer where an end-user calls while testing the newly build environment and reports that some settings are not applied. Often these settings are applied through Group Policy or PowerShell scripts.
For the PowerShell part, the first thing I check is the execution policy.

Although I could ask the user to provide me the output of the Get-ExecutionPolicy command, they will probably not appreciate me for asking this.
Next to that, most of the times I get a incident ticket with the message and no contact with the end-user myself.
Because of this, I’ve written this PowerShell function to get the execution policy for a remote computer:

#Usage: Get-RemoteExecutionPolicy Laptop1
function Get-RemoteExecutionPolicy ($TargetName)
{
$RegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $TargetName)
$PSRegKey= $RegKey.OpenSubKey(“SOFTWARE\\Microsoft\\Powershell\\1\\ShellIds\\Microsoft.PowerShell”)
$Policy = ($PSRegKey.getvalue(“ExecutionPolicy”)).tostring()
Return $Policy
}

It is also possible to invoke a command on the remote machine… like this:

Invoke-Command –Computer Laptop1 –ScriptBlock {Get-ExecutionPolicy}

Another way of doing this, is by using a remote PowerShell session… like this:

New-PSSession –Computer Laptop1 | Get-ExecutionPolicy

For more information about the commands to get the execution policy for specific scopes you can read one of my previous posts: PowerShell and the execution policies explained

Posted By: Jeff Wouters
Last Edit: 30 Nov 2011 @ 02:58 PM

EmailPermalinkComments (2)
Tags

 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 333
  • Comments » 478
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.

Speaking



    No Child Pages.

Health Check



    No Child Pages.