27
Jan 15

The background for this post was I needed to uninstall our software application(s) from multiple servers and each server could have multiple components installed.

#==================================================================
#==== Must have a text file with servers names in it to run against
#==== Be careful...this scripts just goes and executes....be careful with keeping server names in the txt file.
#==================================================================

$servers = get-content -Path "C:\ScriptyBits\ServerNames.txt"
ForEach ($server in $servers){
$apps=(gwmi win32_product -CN $server | where-object {$_.Name -match "Awesome Application One" -or $_.Name -match "Awesome Application Two"})
ForEach($app in $apps){
$app.Uninstall()
}
}

The above script worked just fine. However, I often setup development environments and rapidly iterate through code versions in which I need to quickly uninstall/re-install our software.

Rather than having to explicitly name each application we install using the "-match" switch I converted it to a use the "-like" switch. This allowed me to point to a list of servers and assuming I had admin rights over all of them it would allow me to uninstall the applications by using wildcards "*"

#==================================================================
#==== Must have a text file with servers names in it to run against
#==== Be careful...this scripts just goes and executes....be careful with keeping server names in the txt file.
#==================================================================

$servers = get-content -Path "C:\ScriptyBits\ServerNames.txt"

ForEach ($server in $servers){
$apps=(gwmi win32_product -CN $server | where-object {$_.Name -Like "*Awesome*"})
write-host Connecting to $server
ForEach($app in $apps){
write-host Uninstalling $app
$app.Uninstall()
}
}
12
Apr 14

The background on this script is I needed a way to quick verify software versions of beta code installed on my system. Our software installs in several directories with multiple .EXE's in each directory. All executables are contained in a root directory, but I wanted a quick way to search all directories to verify installation was running on the correct version. Less time fighting with developers about me having the wrong version. <insert happy face here> This little script saves me tons of time as I have about 15 servers I run this on and it outputs to a text file very quickly.  I learned a lot on this script as I was not that familiar with invoke-command and it was cool to get it going.

$servers = Get-Content -Path C:\PowershellScripts\Servers.txt
$out = ForEach ($server in $servers) 
     { invoke-command -computername $server {Get-ChildItem "C:\Program Files\ccleaner\" -Include *.exe -Recurse |
     Select-Object -ExpandProperty VersionInfo | 
     select-object FileDescription, OriginalFilename, FileVersion, @{Name='ServerName';Expression={ $env:COMPUTERNAME }} | 
    ft -AutoSize
    }} 
$out|Out-File C:\PowershellScripts\Versions.log

Sample output:

FileDescription              OriginalFilename     FileVersion ServerName 
---------------              ----------------     ----------- ---------- 
Agent Application            Agent.exe            1,0,0,14873 SVR01
DataGatherNET                DataGatherNET.exe    1,0,0,14873 SVR01
LoggingService Application   Logger.exe           1,0,0,14873 SVR01
JBoss Service wrapper        jbosssvc.exe         1.2.4.0     SVR01
JBoss Web Service wrapper    jbossweb.exe         2.0.4.1     SVR01
JBoss Web Service manager    jbosswebw.exe        2.0.4.1     SVR01
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
}
}