Store the PSSession for later use RRS feed

  • Question

  • Hey Everyone,

    I'm trying to do operations on a Windows machine remotely via Python by internally using PS Remoting.

    Currently, I'm launching separate PowerShell processes from Python, and passing them the credentials XML generated from the Export-Clixml cmdlet, and then executing the command / script on remote machine using the Invoke-Command cmdlet.

    It works perfectly fine for individual operations / group of operations.

    But I'm facing issues when doing operations related to network drives.

    I understand about the Double-Hop problem, but that is not applicable in my case, as I pass the credentials for the network share in the ScriptBlock, rather than delegating them over CredSSP.

    What I noticed was, if I mount the n/w share using ComputerName and the Credential argument for Invoke-Command, I'm unable to use the mounted drive later, and it is mounted with the status Unavailable.

    Instead if I create a session first using the New-PSSession cmdlet, and then pass the session as an argument to Invoke-Command, it works fine, and I'm able to do all kinds of operations on the drive.

    Currently I can easily export the credentials object, re-import it, and keep on using it. But the same does not apply to the PSSession object, as when doing Import-Clixml, it raises Error, when I try to use the session object:

    Invoke-Command : Cannot bind parameter 'Session'. Cannot convert the "[PSSession]WinRM13" value of type "Deserialized.System.Management.Automation.Runspaces.PSSession" to type "System.Management.Automation.Runspaces.PSSession".

    Here are some snapshots to help better understand the issue:

    1. Output of Net Use before mounting any drive
    • With Credentials: https://github.com/GoelJatin/PSRemotingIssue/blob/master/NetUseWithCredsBeforeMounting.png
    • With Session: https://github.com/GoelJatin/PSRemotingIssue/blob/master/NetUseWithSessionBeforeMounting.png

    1. Mounting drive:
    • Using creds: https://github.com/GoelJatin/PSRemotingIssue/blob/master/MountDriveWithCreds.png
    • As you may note in the snapshot above, I mounted a network drive with drive letter P: using the credentials object, and after that when I ran net use, it returned the drive with Status as Unavailable.

    • Using the Session object: https://github.com/GoelJatin/PSRemotingIssue/blob/master/MountDriveWithSession.png
    • Here I mounted the same network path to a different drive letter, X:, using the Session object, and after running net use again, using the Session object, it returned both the drives, but the drive P: (mounted using creds) as Unavailable, and the drive X: (mounted using Session) as OK.

    1. Now if I try to access the contents of the drive, it works only with the session, not with the creds.

    • With creds: https://github.com/GoelJatin/PSRemotingIssue/blob/master/DriveAccessWithCreds.png
    • With Session: https://github.com/GoelJatin/PSRemotingIssue/blob/master/DriveAccessWithSession.png

    If I export the session object, and then try to import and re-use it, it fails:


    If I do a New-PSSession for every PowerShell process, it still fails, as the drive status is again returned as Unavailable.


    Basically I want to be able to do operations on a network drive from Python.

    I'm currently trying via PS Remoting and facing above issues. Other way could be to use PsExec, but I'm keeping it as the last resolution.

    Any help here is much appreciated.

    Thanks in Advance!

    P.S.> I couldn't upload the snapshots here directly due to some account permission issues, so created a GitHub repo, and shared the URLs

    • Moved by Bill_Stewart Tuesday, December 18, 2018 12:26 AM This is not "research information for me" forum
    Thursday, October 4, 2018 11:25 AM

All replies

  • You can create a disconnected session and then just attach to it each time.

    help enter-pssession -online
    help Disconnect-PSSession -online


    Thursday, October 4, 2018 12:15 PM
  • Thanks for the update.

    Enter-PSSession works for interactive remote sessions, so not applicable as per my requirement here, which is why, I'm currently using Invoke-Command to run it programmatically.

    Will take a look into Disconnect-PSSession, and let you know how it goes.

    Thursday, October 4, 2018 5:00 PM
  • Disconnect-PSSession and Connect-PSSession didn't help here.

    They don't work for sessions which are serialized and exported into XML files, and then imported again.

    Trying to mount the drive using PsExec also returned the same issue, where the drive was mounted as Unavailable, and hence not able to do any operations on it.

    Is this due to any recent updates?

    Thursday, October 11, 2018 6:49 AM
  • You cannot save a session to a file.  The us of disconnected session only works by saving the session name then re-authenticating.

    You cannot access a third server from a remote session.  Look up "second hop restriction".


    Thursday, October 11, 2018 8:25 AM