Skip to content

Instantly share code, notes, and snippets.

@mbrownnycnyc
Created March 12, 2015 12:50
Show Gist options
  • Save mbrownnycnyc/9c806d1dc8dea69dddbf to your computer and use it in GitHub Desktop.
Save mbrownnycnyc/9c806d1dc8dea69dddbf to your computer and use it in GitHub Desktop.
Printer inventory function... Enumerates the registry (via powershell remoting) and gets a list of mapped printers for the currently logged on user, and their default printer. "cheap" because it just uses write-outpuit and tee, because I'm too lazy to comform to other methods. (note some comments are from when I was checking that all outlook pro…
function get-adusersid{
#http://community.spiceworks.com/how_to/show/2776-powershell-sid-to-user-and-user-to-sid
param(
[string]$domain,
[string]$user
)
$objUser = New-Object System.Security.Principal.NTAccount("$domain", "$user")
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$strSID.Value
}
function get-mappedprinterlist {
param(
[string]$computername
)
# get user who is logged on at target
$fullusername = @(Get-WmiObject -computer $computername -class win32_computersystem)[0].username
$user = $fullusername.substring($fullusername.LastIndexOf('\')+1,$fullusername.length-$fullusername.LastIndexOf('\')-1)
$domain = $fullusername.substring(0,$fullusername.LastIndexOf('\'))
$usersid = get-adusersid -domain $domain -user $user
#query registry of the machine
# we will use powershell remoting for this, although there are other methods for remote access to the registry.
# HKEY_USERS\[SID]\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\*\ (where * is the GUID of the outlook profile)
# REG_SZ: 001e6602 <= this value should be present and be the CAS hub server
# also grab the following values for the log:
# REG_SZ: 001e6603
# REG_SZ: 001e6612
#search for 001e6602
# http://www.leeholmes.com/blog/2006/07/12/how-do-i-search-the-registry-for-a-value-in-powershell/
#should make this a bit more extensible... storing the results in an object, so that user can easily use convertto- cmdlets.
$invokable = '$regpath = "HKEY_USERS\' + $usersid + '\printers\connections"; new-psdrive -psprovider registry -name HKCUP -Root $regpath | out-null ; get-childitem HKCUP:\ -recurse | foreach {$printerconnection = $((get-itemproperty -Path $_.PsPath).pschildname | out-string); write-output "printer connection: $printerconnection"}'
$scriptbblock = $ExecutionContext.InvokeCommand.NewScriptBlock($invokable)
#the use of `tee` is part of the "cheapness" of the above usage of `write-output`
write-output $computername | tee -append -filepath c:\totalreport.log
Invoke-Command -scriptblock $scriptbblock -computername $computername 2>&1 | tee -append -filepath c:\totalreport.log
#http://support.microsoft.com/kb/156212
# HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows
$invokable = '$regpath = "HKEY_USERS\' + $usersid + '\Software\Microsoft\Windows NT\CurrentVersion\Windows";new-psdrive -psprovider registry -name HKCUDP -Root "$regpath" | out-null;write-output "default printer: $((get-itemproperty -path HKCUDP:\).Device | out-string) "'
$scriptbblock = $ExecutionContext.InvokeCommand.NewScriptBlock($invokable)
#the use of `tee` is part of the "cheapness" of the above usage of `write-output`
Invoke-Command -scriptblock $scriptbblock -computername $computername 2>&1 | tee -append -filepath c:\totalreport.log
}
#if there is a value:
if ($computername) {
get-mappedprinterlist -computername $computername
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment