Mick's IT Blogs

Mick's IT Blogs

23 August 2013

Find out who is logged on and logged off

Posted By: Mick Pletcher - 3:26 PM
Being an SCCM administrator, I often have to log into machines to see if a deployment went ok. Trying to login to machines and getting the message that someone is already logged in gets tiring. Ed Wilson originally wrote a script to tell you whether a user was logged onto a machine or not. I took his script, compacted it and made it more presentable. This script will read from a text file named Computers.txt located in the same directory as the script. PsLoggedon.exe also needs to be present in the same directory. This can be found in the package called PSTools. It has come to be a great asset to my work.

You can download this script from here.


 #*******************************************************************************  
 #   Author: Mick Pletcher  
 #    Date: 23 August 2013  
 #  
 #   Program: Who's Logged On and Logged Off?  
 #*******************************************************************************  

 #Declare Global Variables  
 Set-Variable -Name Command -Scope Global -Force  
 Set-Variable -Name Computer -Scope Global -Force  
 Set-Variable -Name Computers -Scope Global -Force  
 Set-Variable -Name i -Scope Global -Value 1 -Force  
 Set-Variable -Name Output -Scope Global -Force  
 Set-Variable -Name RelativePath -Scope Global -Force  
 Set-Variable -Name Results -Scope Global -Force  
 Set-Variable -Name Username -Scope Global -Force  

 Function RenameWindow ($Title) {  
      #Declare Local Memory  
      Set-Variable -Name a -Scope Local -Force  
      $a = (Get-Host).UI.RawUI  
      $a.WindowTitle = $Title  
      #Cleanup Local Memory  
      Remove-Variable -Name a -Scope Local -Force  
 }  

 Function GetRelativePath{  
      $Global:RelativePath=(split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)+"\"  
 }  

 cls  
 RenameWindow "Who's Logged On and Logged Off?"  
 GetRelativePath  
 $Results = @()  
 $Username = @()  
 $Computers = Get-Content -Path $Global:RelativePath"Computers.txt"  
 $Size = $Computers.Length  
 Foreach ($Computer in $Computers) {  
      Write-Host "Scanning System "$i" of "$Size  
      Write-Host  
      Write-Host "System:"$Computer  
      $Command = $Global:RelativePath+"PsLoggedon.exe -x -l \\$Computer"  
      $Output = Invoke-Expression $Command  
      If ($Output.SyncRoot[2] -eq "Users logged on locally:") {  
           $Username += $Output.SyncRoot[3].Trim()  
      } else {  
           $Username += "N/A"  
      }  
      $Output = $Output.SyncRoot[2]  
      $Output = $Output.Substring(0,$Output.Length-1)  
      $Results += $Output  
      $i = $i + 1  
      cls  
 }  
 cls  
 for ($i=0 ; $i -lt $Results.length ; $i++) {  
       If ($Results[$i] -eq "No one is logged on locally") {  
           Write-Host $Computers[$i]": "$Results[$i] -BackgroundColor Yellow -ForegroundColor Black  
      } elseIf ($Results[$i] -eq "Users logged on locally") {  
           Write-Host $Computers[$i]": "$Results[$i]" -- "$Username[$i]-ForegroundColor White  
      } else {  
           Write-Host $Computers[$i]": "$Results[$i]" -- "$Username[$i]-ForegroundColor Red  
      }  
 }  

 #Cleanup Global Variables  
 Remove-Variable -Name Command -Scope Global -Force  
 Remove-Variable -Name Computer -Scope Global -Force  
 Remove-Variable -Name Computers -Scope Global -Force  
 Remove-Variable -Name i -Scope Global -Force  
 Remove-Variable -Name Output -Scope Global -Force  
 Remove-Variable -Name RelativePath -Scope Global -Force  
 Remove-Variable -Name Results -Scope Global -Force  
 Remove-Variable -Name Username -Scope Global -Force  

07 August 2013

Application Uninstall Script

Posted By: Mick Pletcher - 9:59 AM
This script will uninstall an application with just the partial description that is listed in Add/Remove programs. It searches the product list and then grabs the GUID to use in the MSI uninstallation. At current, it will only uninstall apps that use an MSI installer. I am going to be updating this script to be able to use executables also.



 Function UninstallOldApplication($Description) {  
      #Declare Local Memory  
      Set-Variable -Name AppName -Scope Local -Force  
      Set-Variable -Name Arguments -Scope Local -Force  
      Set-Variable -Name Code -Scope Local -Force  
      Set-Variable -Name GUID -Scope Local -Force  
      Set-Variable -Name Output -Scope Local -Force  
      #Change '%application%' to whatever app you are calling  
      $Description = [char]34+"description like"+[char]32+[char]39+[char]37+$Description+[char]37+[char]39+[char]34  
      $Output = wmic product where $Description get IdentifyingNumber  
      $Output1 = wmic product where $Description get Description  
      $Output1 | ForEach-Object {  
           $_ = $_.Trim()  
        if(($_ -ne "Description")-and($_ -ne "")){  
          $AppName = $_  
        }  
      }  
      Write-Host "Uninstalling"$AppName"....." -NoNewline  
      $Output | ForEach-Object {  
           $_ = $_.Trim()  
        if(($_ -ne "IdentifyingNumber")-and($_ -ne "")){  
          $GUID = $_  
        }  
      }  
      $Arguments = "/X"+[char]32+$GUID+[char]32+"/qb- /norestart"  
      $Code = (Start-Process -FilePath "msiexec.exe" -ArgumentList $Arguments -Wait -Passthru).ExitCode  
      If ($Code -eq 0) {  
           Write-Host "Uninstalled" -ForegroundColor Yellow  
      } else {  
           Write-Host "Failed" -ForegroundColor Red  
      }  
      #Cleanup Local Memory  
      Remove-Variable -Name AppName -Scope Local -Force  
      Remove-Variable -Name Arguments -Scope Local -Force  
      Remove-Variable -Name Code -Scope Local -Force  
      Remove-Variable -Name GUID -Scope Local -Force  
      Remove-Variable -Name Output -Scope Local -Force  
      Remove-Variable -Name Output1 -Scope Local -Force  
 }  

Copyright © 2013 Mick's IT Blogs™ is a registered trademark.