Trying to figure out remote sessions issue but couldn’t find the answer so far, will appreciate any help.
I have a script used to perform user related actions in AD (new-aduser, get-aduser, remove-aduser). The script can be executed against domain controllers in 5 different forests and runs from a server in a different domain.
I’m using remote PowerShell sessions to execute the code on the remote DCs which works but is very slow. So, the script will create the session once starting then reuse the same sessions for all required actions however the session exits once the script is done.
After some testing I found that the new-psession was taking long time (up to a minute in some cases) then did some reading and understood this usually takes time (altough 1 minute might be too long). To speed things up, I changed the methodology and now having session to all servers kept up all the time and instead of doing new-psession I’m using connet-psession and disconnect-psession. I can see some improvement, times for connect-psession are relatively faster (20-30 secs) however this is still slow.
Once the session is connected, commands are executed fast enough so it seems like the initial connection is the issue and I’m trying to figure out if there is a way to farther improve this.
I was trying to keep the session connected i.e. not use disconnect-psession however when I do this, I get the following error when I try to reuse the session: “Invoke-Command : Specified RemoteRunspaceInfo objects have duplicates”.
To sum up I’m trying to figure out how to execute remote commands over sessions in a faster way. Seems like leaving the session active and using the connect/disconnect commands is still slow and when trying to tap into existing session without disconnecting, it will work on the first execution however for following ones I’ll receive the above error.