Last active
August 29, 2015 14:00
-
-
Save mbrownnycnyc/f4c123345da95eb14f81 to your computer and use it in GitHub Desktop.
quick script to pull some info from a given list of machines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Note: | |
If you're going to schedule this task to run, use the following action: | |
Program/script: powershell | |
add arguments: -command &{[path to find_pc_owner.ps1]} | |
Start in: [the working directory] | |
see: http://blogs.technet.com/b/heyscriptingguy/archive/2011/0/12/use-scheduled-tasks-to-run-powershell-commands-on-windows.aspx | |
#> | |
Function veryfastping { | |
[CmdletBinding()] | |
param( | |
[String]$computername = "127.0.0.1", | |
[int]$delay = 100 | |
) | |
$ping = new-object System.Net.NetworkInformation.Ping | |
$pingresult = $ping.send($computername,$delay).status | |
if ($pingresult -ne "Success") { | |
throw $false; | |
return $false; | |
} | |
else { | |
return $true; | |
} | |
} | |
$debug = $true | |
Function main { | |
[CmdletBinding()] | |
param( | |
[string]$outputcsv = ".\out.csv", | |
[string]$referencecsv = ".\out.csv", | |
[string]$errorcsv = ".\outerrors.csv", | |
[string]$inputlist = ".\list.txt" | |
) | |
if (test-path $errorcsv ) { | |
rm -force $errorcsv | |
} | |
cat $inputlist | foreach { | |
$targethost = $_ | |
#conditional, checking if $targethost is found in $referencecsv | |
if ( -not ( get-content $referencecsv -ea "SilentlyContinue" | select-string "$targethost$" ) ) { | |
if ( $debug ) { write-host "Connecting to $targethost" } | |
try { | |
if ( veryfastping -ComputerName $targethost -delay 100 ) { | |
if ( $debug ) { write-host "attempting connection on tcp 445 to $targethost" } | |
$socket = new-object System.Net.Sockets.TcpClient($targethost, '445') -erroraction "stop" | |
try { | |
#to get the last logged on user via registry | |
if ( $debug ) { write-host "attempting registry query on $targethost" -backgroundcolor "darkyellow" } | |
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $targethost) | |
$RegKey = $Reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI") | |
$lastloggedonuser = $RegKey.GetValue("LastLoggedOnUser") | |
if ( $debug ) { write-host "Last logged on user: $lastloggedonuser" -backgroundcolor "darkgreen" } | |
} | |
catch { | |
#if we can't get the last logged on user, then set the variable to "n/a" | |
$lastloggedonuser = "n/a (host allowed connection to tcp 445)" | |
if ( $debug ) { write-host "Last logged on user: $lastloggedonuser" -backgroundcolor "red" } | |
} | |
if ( $debug ) { write-host "starting WMI querying of $targethost" -backgroundcolor "darkgreen" } | |
$obj1 = new-object system.object | |
$obj1 | Add-Member -MemberType NoteProperty -Name "date" -Value $(get-date -format "yyyy-MM-dd HH:mm:ss") | |
$obj2 = get-wmiobject win32_computersystem -ComputerName $targethost -EA "Stop" | select-object name,username,manufacturer,model,totalphysicalmemory | |
$obj3 = get-wmiobject win32_bios -ComputerName $targethost -EA "Stop" | select-object serialnumber | |
$obj4 = get-wmiobject win32_operatingsystem -ComputerName $targethost -EA "Stop" | select-object version, osarchitecture | |
$obj5 = new-object system.object | |
$obj5 | add-member -membertype noteproperty -name "lastloggedonuser" -value $lastloggedonuser | |
$obj6 = new-object system.object | |
$obj6 | add-member -membertype noteproperty -name "inputvalue" -Value $targethost | |
#it's possible that the WMI query has failed, but no error was thrown, This is just the way it goes with get-wmiobject, kid. | |
# so we'll check the name of the system (since this is the essential purpose of what we're doing) | |
if ( $obj2.name.length -gt 0 ) { | |
$Combined = New-Object -Type PSObject | |
$obj1,$obj2,$obj3,$obj4,$obj5,$obj6|%{$CurObj = $_;$_|gm|?{$_.MemberType -match "NoteProperty"}|%{$NewMember = $_.Name;$Combined|Add-Member -MemberType NoteProperty -Name $NewMember -Value $CurObj.$NewMember}} | |
$Combined | export-csv -append -notypeinformation $outputcsv -encoding utf8 | |
} | |
else { | |
#weird, we can connect, but aren't getting good WMI stuff back. totally inSAN. | |
throw | |
} | |
$lastloggedonuser = "" | |
} | |
} | |
catch { | |
#then we will not add to the CSV | |
$objectionunderruled = new-object system.object | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "date" -Value $(get-date -format "yyyy-MM-dd HH:mm:ss") -ea 0 | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "manufacturer" -Value "" | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "model" -Value "" | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "name" -Value "" | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "totalphysicalmemory" -Value "" | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "username" -Value "" | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "serialnumber" -Value "" | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "version" -Value "" | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "osarchitecture" -Value "" | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "lastloggedonuser" -Value "$lastloggedonuser" -ea 0 | |
$objectionunderruled | Add-Member -MemberType NoteProperty -Name "inputvalue" -Value "$targethost is inaccessible" | |
$objectionunderruled | export-csv -append -notypeinformation $errorcsv -encoding utf8 | |
$lastloggedonuser = "" | |
} | |
} | |
} | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment