Get-MappedPrinters.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 0a393d2b-df90-4d5e-87b5-d185ff888386
 
.AUTHOR Musa Ugurlu
 
.COMPANYNAME Musa Ugurlu
 
.COPYRIGHT Musa Ugurlu
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI https://gist.github.com/musaugurlu/b849f968d3f9eb3b062736498918f150
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.DESCRIPTION The cmdlet get mapped printers from the given computer for the given user.
 
#>



<#
.SYNOPSIS
Gets mapped printers from given computer for given user
 
.DESCRIPTION
The cmdlet get mapped printers from the given computer for the given user. Requires ActiveDirectory module to gets user's SID.
What it does is to query given computer's registery for given user
 
.PARAMETER ComputerName
Remote computer name
 
.PARAMETER Username
Domain username
 
.EXAMPLE
Get-MappedPrinters -ComputerName MySweetComputer -Username JohnDoe
 
.NOTES
This scripts requires ActiveDirectory module to get user's SID
 
#>
 

function Get-MappedPrinters {
    
    [CmdletBinding()]
    param (
        # ComputerName
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        [string] $ComputerName,
        # Username
        [Parameter(Mandatory=$true)]
        [string] $Username
    )
    
    begin {
        try {
            $UserSid = (Get-ADUser -Identity $Username).sid.Value  
        }
        catch {
            Write-Error "Couldn't get user information"
            break
        }
    }
    
    process {
        Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-ChildItem "Registry::\HKEY_Users\$($args[0])\Printers\Connections"} -ArgumentList $UserSid
    }
    
    end {
    }
}