Advertisement
Pavix

CheckComputer.ps1 Updated

Jun 26th, 2017
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Written by Scott Callaway
  2. # sock0pen@gmail.com
  3. # Updated script to list total and used RAM slots and to show if AD account is Lync enabled.
  4. # Parameters. This must be before anything else.
  5. param
  6.   (
  7.     [string]$strComp
  8.   )
  9. #Set global variable for the Admin account
  10. #This is a generic user account. Replace "Administrator" with the admin user for your environment
  11. $myadmin = "Administrator"
  12.  
  13.  #Since we run this with our A account, we need to set the background color to something readable. Default is DarkBlue so lets use that.
  14.  $Host.UI.RawUI.BackgroundColor = ($bckgrnd = 'DarkBlue')
  15.  $Host.UI.RawUI.ForegroundColor = ($frgrnd = 'White')
  16.  
  17. #Clear the screen
  18. cls
  19. # Check parameters input
  20. if( $strComp -eq "" )
  21.   {
  22.     $res = "Missing parameters - Usage: .\CheckComputer.ps1 hostname"
  23.     echo $res
  24.     exit
  25.   }
  26.  
  27.  
  28. #Setting ErrorHandling to silently continue
  29. $ErrorActionPreference = "SilentlyContinue"
  30. #Load AD module if it's not loaded
  31. If (!(Get-module ActiveDirectory )) {
  32. Import-Module ActiveDirectory
  33. }
  34.  
  35. #The below function was borrowed from http://stackoverflow.com/questions/2688547/muliple-foreground-colors-in-powershell-in-one-command
  36. #This helps set the color of the text
  37. function Write-Color([String[]]$Text, [ConsoleColor[]]$Color = "White", [int]$StartTab = 0, [int] $LinesBefore = 0,[int] $LinesAfter = 0) {
  38. $DefaultColor = $Color[0]
  39. if ($LinesBefore -ne 0) {  for ($i = 0; $i -lt $LinesBefore; $i++) { Write-Host "`n" -NoNewline } } # Add empty line before
  40. if ($StartTab -ne 0) {  for ($i = 0; $i -lt $StartTab; $i++) { Write-Host "`t" -NoNewLine } }  # Add TABS before text
  41. if ($Color.Count -ge $Text.Count) {
  42.     for ($i = 0; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine }
  43. } else {
  44.     for ($i = 0; $i -lt $Color.Length ; $i++) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine }
  45.     for ($i = $Color.Length; $i -lt $Text.Length; $i++) { Write-Host $Text[$i] -ForegroundColor $DefaultColor -NoNewLine }
  46. }
  47. Write-Host
  48. if ($LinesAfter -ne 0) {  for ($i = 0; $i -lt $LinesAfter; $i++) { Write-Host "`n" } }  # Add empty line after
  49. }
  50.  
  51.  
  52.  
  53.  
  54. #This was borrowed from http://poshcode.org/5827
  55. function Get-Monitorinfo {
  56.            
  57.                         $Monitors = get-wmiobject -ComputerName $strComp -ClassName wmimonitorid -Namespace root/wmi -ErrorAction SilentlyContinue
  58.                         Foreach($Monitor in $Monitors){
  59.                             $Manufacturer = [System.Text.Encoding]::ASCII.GetString($Monitor.ManufacturerName)
  60.                             $Model = [System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)
  61.                             $SerialNumber = [System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID)
  62.                             New-Object -TypeName PSCustomObject -Property @{
  63.                                 Manufacturer = $Manufacturer
  64.                                 Model = $Model
  65.                                 SerialNumber = $SerialNumber
  66.                             }
  67.                         }
  68.  
  69. }
  70.  
  71.  
  72. #Lets test if the computer is online first before throwing a metric ton of commands at it.
  73. #As a side note, I tried wrapping all this into a single function but that didn't work.
  74. if(Test-Connection -cn $strComp -Quiet -BufferSize 16 -Count 1)
  75.    {
  76.  
  77. #This is to check if Imprivata exists
  78. #$Imprivata = $False
  79. #if(Test-Path -Path "\\$strComp\C$\Program files\Imprivata\OneSign Agent" -pathtype Container)
  80. #{
  81. #    $Imprivata = $True
  82. #}
  83. #if(Test-Path -Path "\\$strComp\C$\Program files (x86)\Imprivata\OneSign Agent" -pathtype Container)
  84. #{
  85. #    $Imprivata = $True
  86. #}
  87.  
  88. #This code is to check for the presence of $myadmin
  89. #This ran slowly. The fix is to tell it to not search AD for the user by specifying the Domain string.
  90. $found = (Get-WmiObject -ComputerName $strComp -Class Win32_UserAccount -Filter "Domain='$strComp' and Name='$myadmin'")
  91.   if($found -eq $null)
  92.   { $MyAdmins = "Not Present" }
  93.    Else
  94.    { $MyAdmins = "Present" }
  95.  
  96. #This gets the users running processes on the computer
  97. $UsersRunningProc = (Get-WmiObject -cn $strComp win32_process).getowner().user | Select -Unique
  98. $RunningProc = Foreach ($ProRunner in $UsersRunningProc) {
  99. If (($ProRunner -ne "SYSTEM") -and ($ProRunner -ne "NETWORK SERVICE") -and ($ProRunner -ne "LOCAL SERVICE")) {
  100.    $RunProc = "$RunProc `n $ProRunner"
  101.  }
  102. }
  103.  
  104.  
  105. #Instead of running several Get-WMIObject we're going to run it three times and get our data from there
  106. #Once for ComputerSystem, Once for OperatingSystem, and Once for NetworkAdapterconfiguration
  107. $win32_CS = Get-WmiObject -cn $strComp Win32_ComputerSystem
  108. $win32_OS = Get-WmiObject -cn $strComp Win32_OperatingSystem
  109. $win32_NIC = Get-WMIObject -cn $strComp Win32_NetworkAdapterConfiguration
  110.  
  111.  
  112. #Checks if computer is 32-Bit or 64-Bit
  113. $Archi = $win32_OS.OSArchitecture
  114. #Get the computer model
  115. $ModelInfo = $win32_CS.model
  116. #Get the computer serial number
  117. $SerNum = (Get-WmiObject -computername $strComp -Class Win32_BIOS).SerialNumber
  118.  
  119. #If we add things that require the Get-ADComputer function later then it will speed up the script slightly
  120. #By running one Get-ADComputer and then getting our data from that variable.
  121. $ADComp = Get-ADCOmputer $strComp -Properties *
  122.  
  123. #Gets the computer's OU
  124. $OU = ($ADComp).CanonicalName
  125.  
  126. #Gets currently logged in user. Returns in a format of either <Domain>\<User> or <Computername>\<user> for local accounts
  127. $LoggedOnUser = $win32_CS.username
  128.  
  129. #Split so domain is $Split[0] and user is $Split[1]
  130. $Split = $LoggedOnUser.Split('\')
  131.  
  132. # Get just the Username
  133. $User = $Split[1]
  134.  
  135. #Lets set the domain as a variable too
  136. $DomainName = $Split[0]
  137.  
  138. #One query to AD. Speeds things up
  139. $ad_USER = Get-ADUser -Identity $User -Properties *
  140.  
  141. #Function for days since password change borrowed from http://www.workingsysadmin.com/powershell-function-to-get-time-since-a-users-password-was-last-changed/
  142. function Get-TimeSinceLastPWSet {
  143.     [CmdletBinding()]
  144.     param (
  145.         [Parameter(Mandatory=$True,
  146.                 Position=1,
  147.         ValueFromPipeline=$True)]
  148.         [string]$Username
  149.     )
  150.    
  151.     $tsSinceLastPWSet = New-TimeSpan $($ad_USER).Passwordlastset $(get-date)
  152.     $strFormatted = '{0:dd} days, {0:hh} hours' -f $tsSinceLastPWSet
  153.     return $strFormatted
  154.     }
  155.  
  156. $LoggedOnName = $ad_USER.Name
  157.  
  158.  
  159. $ExpirationDate=$ad_USER.AccountExpirationDate
  160. if ($ExpirationDate -ne $Null) {
  161. $Result = ($ExpirationDate -lt (Get-Date))
  162. }
  163. else { $Result = "Never" }
  164.  
  165. $DaysSinceChange = Get-TimeSinceLastPWSet $User
  166.  
  167. #Is the user locked out now?
  168. $LockedOut = $ad_USER.LockedOut
  169.  
  170.  
  171.  
  172. #When did they change thier password last?
  173. $PasswdLastSet = $ad_USER.PasswordLastSet
  174.  
  175. #What department are they with?
  176. $Department = $ad_USER | Select-Object -expandproperty Department
  177.  
  178. #Get Network home drive
  179. $HomeDir = $ad_USER | Select-Object -expandproperty homeDirectory
  180.  
  181. #Get logon script
  182. $LogonScript = $ad_USER | Select-Object -expandproperty ScriptPath
  183.  
  184. #Get phone number
  185. $PhoneNum =  $ad_USER | Select-Object -expandproperty officephone
  186.  
  187. #Sadly this needs to be formatted this way. When I tried ($win32_OS).LastBootUpTime it didn't return correctly
  188. #Anyway, the next few lines are the magic to get it in a readable format
  189. $BootTime = Get-WmiObject -ComputerName $strComp -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
  190.  
  191. $now = Get-Date
  192.  
  193. $bootedTime = $BootTime.ConvertToDateTime($BootTime.LastBootUpTime)
  194.  
  195. $uptime = $now - $bootedTime
  196.  
  197. $d =$uptime.days
  198.  
  199. $h =$uptime.hours
  200.  
  201. $m =$uptime.Minutes
  202.  
  203. $s = $uptime.Seconds
  204.  
  205. #This Get's what OS is installed
  206. $OS = $win32_OS.Caption
  207.  
  208. #Get C drive total size and free space
  209. $disk = Get-WmiObject Win32_LogicalDisk -ComputerName $strComp -Filter "DeviceID='C:'" | Select-Object Size, FreeSpace
  210.  
  211. #Get installed RAM
  212. $RAM = [Math]::Round($win32_CS.TotalPhysicalMemory/1Gb)
  213.  
  214. #Get the amount of available RAM
  215. $RAMFree = [Math]::Round(($freemem.FreePhysicalMemory / 1024 / 1024), 2)
  216.  
  217. #Get RAM Slots
  218. $colSlots = Get-WmiObject -Class "win32_PhysicalMemoryArray" -namespace "root\CIMV2" -cn $strComp
  219. $colRAM = Get-WmiObject -Class "win32_PhysicalMemory" -namespace "root\CIMV2" -cn $strComp
  220. $NumSlots = 0
  221.  
  222.  $colSlots | ForEach {
  223.        “Total Number of Memory Slots: ” + $_.MemoryDevices
  224.        $NumSlots = $_.MemoryDevices
  225.  }
  226.  
  227.  
  228. $SlotsFilled = 0
  229.  $TotMemPopulated = 0
  230.  
  231. $colRAM | ForEach {
  232.         “Memory Installed: ” + $_.DeviceLocator
  233.         “Memory Size: ” + ($_.Capacity / 1GB) + ” GB”      
  234.         $SlotsFilled = $SlotsFilled + 1
  235.         $TotMemPopulated = $TotMemPopulated + ($_.Capacity / 1GB)
  236.  #      if ($_.Capacity = 0)
  237.  #      {write-host "found free slot"}
  238.  
  239. }
  240.  
  241.  
  242. #Get description of wired NIC
  243. $NICDesc = ($win32_NIC | ? { $_.IPAddress -ne $null }).Description
  244.  
  245. #The next few lines is the secret sauce to get the IP address and gateway
  246. $Desc = $NICDesc.Split(',')
  247.  
  248. $TrueNIC = $Desc[0]
  249.  
  250. $NIC = ($win32_NIC | ? { $_.IPAddress -ne $null }).ipaddress
  251.  
  252. $IPs = $NIC.Split(',')
  253.  
  254. $IPv4 = $IPs[0]
  255.  
  256. $Gateway = ($win32_NIC | ? { $_.IPAddress -ne $null }).DefaultIPGateway
  257.  
  258. $GW = $Gateway | Select-Object -First 1
  259.  
  260. #Windows install date
  261. $Insdate = ([WMI] '').ConvertToDateTime($win32_OS.InstallDate)
  262.  
  263. #Lets get the Bitlocker Key
  264. $BitLockComp = Get-ADComputer -Filter {Name -eq $strComp}
  265. $BitLockerObjects = Get-ADObject -Filter {objectclass -eq 'msFVE-RecoveryInformation'} -SearchBase $BitLockComp.DistinguishedName -Properties 'msFVE-RecoveryPassword'
  266. $BitLockerKey = $BitLockerObjects | Select -expandproperty msFVE-RecoveryPassword
  267.  
  268. }
  269. #This function was to get services that are set to auto for startup, but are currently not running.
  270. #Yes, even though I love powershell, I get frustrated.
  271. function IHatePowershell {
  272. $Services = (Get-WmiObject -cn $strComp win32_service | Where-Object -FilterScript { $_.state -ne "Running" -and $_.StartMode -eq "Auto" }).Name
  273. foreach ($service in $services) {
  274. Write-Color "$service" -Color Green
  275. }
  276. }
  277.  
  278. #This function is kind of a break-fix function. If we ran this script against a computer with a local account logged in,
  279. #it pukes up errors. So we have to create two seperate functions. One for an AD account and one for a local account and one is if noone is logged in
  280. #It's ugly, but works.
  281.  
  282. #This is the function for if no one is logged into the workstation
  283. Function NoLoggedInUser {
  284. Write-Color "#########################################" -Color Red
  285. Write-Color "There is currently nobody actively logged into this machine" -Color White
  286. Write-Color "#########################################" -Color Red
  287. }
  288.  
  289. #This is the function if an AD account is logged in.
  290. Function DisplayADAccount {
  291. Write-Color "#########################################" -Color Red
  292. Write-Color "Currently logged in name: ", "$LoggedOnName" -Color White, Green
  293. Write-Color "Currently logged on user: ", "$User" -Color White, Green
  294. Write-Color "Current User locked: ", "$LockedOut" -Color White, Green
  295. Write-Color "Current User Password last set: ", "$DaysSinceChange ago" -Color White, Green
  296. Write-Color "Current User Account Expiration: ", "$Result" -Color White, Green
  297. Write-Color "Current User Department: ", "$Department" -Color White, Green
  298. Write-Color "Current User Home Directory: ", "$HomeDir" -Color White, Green
  299. Write-Color "Current User Logon Script: ", "$LogonScript" -Color White, Green
  300. Write-Color "Current User Phone Number: ", "$PhoneNum" -Color White, Green
  301.  
  302. $LyncEnabled = $ad_USER.'msRTCSIP-PrimaryUserAddress'
  303.  
  304. If ($LyncEnabled -ne $null) {
  305.  
  306. $LyncStatus = Write-Color "Current User Lync Enabled:", " Yes" -Color White, Green
  307.  }
  308. Elseif (LyncEnabled -eq $null) {
  309.  
  310. $LyncStatus = Write-Color "Current User Lync Enabled:", "No" -Color White, Red
  311. }
  312. Write-Color "#########################################" -Color Red
  313. }
  314.  
  315. #This is the function if a non-AD account is logged in.
  316. Function DisplayLocalAccount {
  317. Write-Color "#########################################" -Color Red
  318. Write-Color "Current logged in user is a local account" -Color White
  319. Write-Color "Currently logged on user: $User" -Color White
  320. Write-Color "#########################################" -Color Red
  321. }
  322.  
  323.  
  324.  
  325. #Lets test if the computer is on the network before we throw lots of commands at it
  326. if(Test-Connection -cn $strComp -Quiet -BufferSize 16 -Count 1)
  327.    {
  328.    Write-Color "#########################################" -Color Red
  329.    Write-Color "Information for ", "$strComp" -Color White, Green
  330.    Write-Color "#########################################" -Color Red
  331.    Write-Color "Computer OU: ", "$OU" -Color White, Green
  332.    Write-Color "#########################################" -Color Red
  333.    Write-Color "Operating System: ", "$OS $Archi" -Color White, Green
  334.    Write-Color "Computer Model: ", "$ModelInfo" -Color White, Green
  335.    Write-Color "Computer Serial Number: ", "$SerNum" -Color White, Green
  336.    Write-Color "Computer uptime: Days:", "$d ", "Hours:", "$h ", "Minutes:", "$m ", "Seconds:", "$s" -Color White, Green, White, Green, White, Green, White, Green
  337.    Write-Color "Computer OS was installed on ", "$InsDate" -Color White, Green
  338.    Write-Color "Computer Network information: ", "$Info" -Color White, Green
  339.    Write-Color "Network Card: ", "$TrueNIC" -Color White, Green
  340.    Write-Color "IP Address: ", "$IPv4" -Color White, Green
  341.    Write-Color "Default Gateway: ", "$GW" -Color White, Green
  342.    Write-Color "MyAdmin account: ", "$MyAdmins" -Color White, Green
  343. If ($BitlockerKey -eq $Null) {
  344.    Write-Color "Bitlocker Key: " "Not present" -Color White, Green
  345.    }
  346.    else {
  347.    Write-Color "Bitlocker Key: " "$BitLockerKey" -Color White, Green
  348.    }
  349.    Write-Color "Bitlocker key: ", "$BitLockerKey" -Color White, Green
  350.    if($User -eq $Null)
  351.       {
  352.       NoLoggedInUser
  353.       }
  354.     elseif($DomainName -eq $strComp)
  355.       {
  356.       DisplayLocalAccount
  357.       }
  358.     elseif($DomainName -ne $strComp)
  359.              {
  360.              DisplayADAccount
  361.              }
  362.    Write-Color "Hard drive information for C:" -Color White
  363.    Write-Color ("{0}GB total" -f [math]::truncate($disk.Size / 1GB)) -Color Green
  364.    Write-Color ("{0}GB free" -f [math]::truncate($disk.FreeSpace / 1GB)) -Color Green
  365.    Write-Color "#########################################" -Color Red
  366.    Write-Color "RAM information for ", " $strComp" -Color White, Green
  367.    Write-Color "Installed RAM: ", "$RAM GB" -Color White, Green
  368.    Write-Color "Number of RAM Slots: ", "$NumSlots" -Color White, Green
  369.    Write-Color "Slots in use: ", "$SlotsFilled" -Color White, Green
  370.    Write-Color "#########################################" -Color Red
  371.    Write-Color "Has Imprivata: ", "$Imprivata" -Color White, Green
  372.    Write-Color "#########################################" -Color Red
  373.    Write-Color "Services that are set to auto but not running:" -Color White, Green
  374.    IHatePowershell
  375.    Write-Color "#########################################" -Color Red
  376.    Write-Color "Users running processes: ", "$RunProc" -Color White, Green
  377.    Write-Color "#########################################" -Color Red
  378.    Write-Color "Monitor Information:" -Color White, Green
  379.    Get-Monitorinfo | Format-Table -Auto
  380.    }
  381. Else
  382.   {
  383.    Write-Color "$strComp is offline" -Color Red
  384.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement