Consider the following scenario: I want to use implicit remoting to gain access to the ActiveDirectory module on my Domain Controller. Easily enough done:
function Use-DCSession {
$DomainController = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().RootDomain.FindDomainController().Name
Use-RemoteModule $DomainController ActiveDirectory
}
function Use-RemoteModule([string]$ComputerName, [string]$Module) {
Write-Verbose "Importing $Module module from $Computername"
$session = New-PSSession $ComputerName
New-Variable -Name "$($Module)_Session" -Value $session -Scope Global
Invoke-Command -Session $session -ScriptBlock { import-module $args[0] } -ArgumentList $Module
$moduleName = (Import-Module (Import-PSSession -Module $Module -AllowClobber -Session $session) -Global -PassThru).Name
Write-Verbose "$Module imported as $moduleName"
}
Use-DCSession
However, I do not have access to the AD: PSDrive that was created in the remote session. Is there any way to get the PSDrive injected into a session like we get with modules? I can work around it by wrapping Invoke-Commands in custom functions as below, but it is a cludge and does not integrate well.
$global:ActiveDirectory_Session = New-PSSession -ComputerName DC
function Get-ADChildItem($Path, $session=$global:ActiveDirectory_Session) {
Invoke-Command -Session $session -ScriptBlock { ls $args[0] } -ArgumentList $Path
}
Get-ADChildItem "ad:\dc=AD,ad=LOCALDOMAIN" | select Name, objectClass
Expected Results:
name objectClass
---- -----------
AD domainDNS
Configuration configuration
Schema dMD
ForestDnsZones domainDNS
DomainDnsZones domainDNS
Is there something like a “RemoteDriveProxyProvider” which would redirect standard command calls through to the remote session’s drive and return the appropriate results? I’d be happy to write a custom provider if needed, but I’m not to clear on how the powershell would map to C# in this scenario.
Thanks,
James