Skip to content

Instantly share code, notes, and snippets.

@Xainey
Created March 31, 2016 14:16
Show Gist options
  • Save Xainey/de3ca6ff854889c717a35c68375a153d to your computer and use it in GitHub Desktop.
Save Xainey/de3ca6ff854889c717a35c68375a153d to your computer and use it in GitHub Desktop.
function SIDtoUsername($sid)
{
$objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
return $objUser.Value
}
function getPrinters($computer)
{
$spacer = "-" * 99
[console]::writeline("`n{0}`n{1}`n{2}", $spacer, "MAPPED PRINTERS", $spacer)
$collection = @()
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $computer)
$userKeys = $reg.GetSubKeyNames()
foreach($user in $userKeys)
{
$connections = $reg.OpenSubKey("$user\\Printers\\Connections")
if ($connections -ne $null)
{
foreach($printerKey in $connections.GetSubKeyNames())
{
$printer = $connections.OpenSubKey("$printerKey")
$arrPrinterParts = $printerKey.Split(",")
$printServer = $arrPrinterParts[2]
$printerName = $arrPrinterParts[3]
$temp = New-Object psobject
$temp | Add-Member NoteProperty UserSID $user
$temp | Add-Member NoteProperty UserName $(SIDtoUsername($user))
$temp | Add-Member NoteProperty PrintServer $printServer
$temp | Add-Member NoteProperty PrinterName $printerName
$temp | Add-Member NoteProperty PrinterUNC "\\$printServer\$printerName"
$temp | Add-Member NoteProperty PrinterGUID $printer.GetValue("GUIDPrinter")
$collection += $temp
}
}
}
return $collection
}
function getMappedDrives($computer)
{
$spacer = "-" * 99
[console]::writeline("`n{0}`n{1}`n{2}", $spacer, "MAPPED DRIVES", $spacer)
$driveCollection = @()
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $computer)
$userKeys = $reg.GetSubKeyNames()
foreach($user in $userKeys)
{
$netKey = $reg.OpenSubKey("$user\Network")
if ($netKey -ne $null)
{
foreach($driveKey in $netKey.GetSubKeyNames())
{
$drive = $netKey.OpenSubKey("$driveKey")
$temp = New-Object psobject
$temp | Add-Member NoteProperty UserSID $user
$temp | Add-Member NoteProperty UserName $(SIDtoUsername($user))
$temp | Add-Member NoteProperty DriveLetter $($drive | Split-Path -Leaf)
$temp | Add-Member NoteProperty NetworkPath $drive.GetValue("RemotePath")
$driveCollection += $temp
}
}
}
return $driveCollection
}
cls
$pc = Read-Host 'Enter PC Name'
# SELECTION EXAMPLE QUERIES
# getMappedDrives($pc) | Select-Object UserName, UserSID, DriveLetter, NetworkPath;
# getPrinters($pc) | Select-Object UserName, UserSID, PrintServer, PrinterName, PrinterUNC, PrinterGUID;
# FORMAT TABLE EXAMPLES
# getPrinters($pc) | Format-Table -Wrap -AutoSize -Property UserName, PrintServer, PrinterName, PrinterUNC # -GroupBy UserName
# getMappedDrives($pc) | Format-Table -Wrap -AutoSize -Property UserName, DriveLetter, NetworkPath #-GroupBy UserName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment