4
Apr 14

I work at a software company and sometimes all I do is install and uninstall our product to test new versions. I wanted to speed things up a little and for my little dev environment write a script to call the uninstall process depending on which OS version was running on the target device. It was an interesting find for me because some of my target devices are WYSE thinclients running Windows Embedded (old....think xp). The process for looking for applications installed on a Windows 7 device and my lovely WYSE device are different. Win7 can use the common Win32_Product whereas the WYSE device I had to use WMI to call Win32Reg_AddRemovePrograms to find my application. Thought process for script

    Target a server
    Check version of OS running
    if running Win7 -> run the uninstall and reboot
    If running WinXP -> run the uninstall and reboot
param([string]$strComputer ="%1")
 
$CheckOS =(Get-WmiObject-Class win32_OperatingSystem -namespace"root\CIMV2"-ComputerName $strComputer).caption.trim()
Write-Host=========================================================
Write-Host Going against $strComputer to process the Uninstall
Write-Host=========================================================
	
	If($CheckOS -eq "Microsoft Windows 7 Professional")
		{
			Write-Host=========================================================
			write-host $strComputer "has $CheckOS installed"Write-Host
			write-host RunningUninstall on $strComputer
			Write-Host=========================================================
		Get-WmiObject-NameRoot\Cimv2 -classWin32_Product–computername $strComputer |Where-Object{$_.Name-like "Blah*"}|
			foreach-object-process {$_.Uninstall()}Start-Sleep-Seconds 5 
			
			Write-Host=========================================================
			Write-Host
			Write-Host $strComputer is going to reboot now
			Write-Host
			Write-Host=========================================================
			Restart-Computer-cn $strComputer -Force
		}
	Else
		{
			Write-Host=========================================================
			write-host $strComputer "has $CheckOS installed"
			Write-Host
			write-host RunningUninstall on $strComputer
			Write-Host=========================================================
	
	$ProdID = $null
	$prodid =(get-wmiobject -OutVariable prdid -ClassWin32Reg_AddRemovePrograms -ComputerName rob_wyse |where{$_.Displayname-like "Blah*"}|selectProdID).ProdIdIf($ProdID)
		{
			Foreach($PrdID in $ProdID)
		{
			start-process psexec -argumentlist "-accepteula \\$strComputer msiexec /X$PrdID /q /norestart"-PassThru
			Write-Host=========================================================
			Write-Host
			Write-Host $strComputer is going to reboot now
			Write-HostWrite-Host=========================================================Start-Sleep30Restart-Computer-cn $strComputer
		}
		}
		}
4
Apr 14

At my company we are about to perform and full power shutdown and I wanted to create a script to shutdown the servers.  I have never used test-connection before so this is what I came up with.

$servers = get-content -path 'c:\rcj\scripts\powershell\servers.txt'
$outfile = 'c:\rcj\scripts\powershell\servershutdownresults.txt'
foreach($server in $servers)
{
if(test-connection $server -count 1 -quiet)
{
write-output "$server is pingable" | out-file -append -filepath $outfile
}
else
{
write-output "$server is down" | out-file -append -filepath $outfile
}
}