Implicit remoting with PSDrive

This topic contains 2 replies, has 2 voices, and was last updated by  James Sudbury 1 year ago.

  • Author
    Posts
  • #56089

    James Sudbury
    Participant

    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

  • #56104

    Don Jones
    Keymaster

    Nope. Providers aren't hooked up to implicit remoting. That's because implicit remoting works by creating local proxy commands, which do all the actual remoting; a provider doesn't have a proxy construct available in the technology.

    You're welcome to create one, though ;).

  • #56128

    James Sudbury
    Participant

    I expected that to be the answer, but I had hoped I was just missing something.

    Thanks for your help. If I come up with a solution, I'll post it here.

    -James

You must be logged in to reply to this topic.