Remote Powershell Session oddity

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Remote Powershell Session oddity

Post by IanUoY »

I cannot get a remote session working properly.

If I use the following code:

$cred = Get-Credential
$session = New-PSSession -ComputerName DC1 -Credential $cred
Invoke-Command $session -Scriptblock { Import-Module ActiveDirectory }
Import-PSSession -Session $session -Module ActiveDirectory #-Prefix Rem

$user = Get-ADUser ???? -Properties *

The get-aduser cmdlet does not work.

However, if I don't try to import the session :

$cred = Get-Credential
$session = New-PSSession -ComputerName DC1 -Credential $cred

$user = Invoke-Command -Session $session { Get-ADUser ???? -Properties * }

This works fine and the user is retrieved.

Must be doing something fundamentally wrong, however, I just can't spot it.

Any help most appreciated.

Thanks

Ian
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remote Powershell Session oddity

Post by jvierra »

AD does not create an importable session. It can only be used remotely. The module has to be designed for importing and remoting the session.
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: Remote Powershell Session oddity

Post by IanUoY »

I thought it just imported the reference to the cmdlets, a bit like having RSAT.

Certainly there are many blogs etc out there referencing the ability to Import the active directory module into the current session.

Rather confusing. However, as I now have working, I will continue to use the Invoke Command using the current session.

Regards

Ian
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remote Powershell Session oddity

Post by jvierra »

I have never seen a blog that claimed that was possible. You can use Enter-PsSession to access remote AD interactively.
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: Remote Powershell Session oddity

Post by IanUoY »

I thought that was what I was doing. Creating a new session to my DC and importing the AD commands into the local session.

Indeed, if I run the commands above in a powershell windows on a PC that doesn't have RSAT, the commands produce the desired effect and I can run the get-aduser cmdlet as expected. However, the same code in a ps studio form does not unless I use the second block of code.

As such I'm assuming the issue lies somewhere in the forms structure.

Regards

Ian
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Remote Powershell Session oddity

Post by jvierra »

help enter-possession -online

You cannot use Enter-PsSession in a form or script. It can only be used interactively.
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: Remote Powershell Session oddity

Post by IanUoY »

Aha....thanks for clearing that up for me.

Ian
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked