Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Store the PSSession for later use

$
0
0

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 statusUnavailable.

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 letterP: 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 driveP: (mounted using creds) as Unavailable, and the driveX: (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:

https://github.com/GoelJatin/PSRemotingIssue/blob/master/SessionImportError.png

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

https://github.com/GoelJatin/PSRemotingIssue/blob/master/NewPsSessionAfterMountDifferentPSProcess.png

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


Viewing all articles
Browse latest Browse all 15028

Latest Images

Trending Articles



Latest Images