Author Posts

January 1, 2012 at 12:00 am

by lopyeg at 2013-01-10 08:56:20

Hey guys. please help
i have a txt file with AD logins as input data. I get it with Get-Content cmdlet
after this i need to do some operations with them in pssession at another PC (because i have no AD cmdlets on local PC)
but script in the PSSession didn't find the path to txt file at local PC

in another words
i need to run this
Get-Content users.txt | Foreach-Object { (get-aduser $_ -Properties mail).mail }
inside the PSSession

but users.txt is located at my local PC and this is trouble

can you help me?

by ArtB0514 at 2013-01-10 11:47:52

One way to pass data to a PSSession is to use Invoke-Command with the -ArgumentList parameter. Something like this might work:

Invoke-Command -ScriptBlock {$MailUsers = $Args | foreach {(Get-ADUser $_ -Properties Mail).Mail}} -Session $PSSession -ArgumentList (Get-Content users.txt)

by lopyeg at 2013-01-10 12:13:12

Art80514, thank you a lot
Can you explain the "$MailUsers = $Args" what does alias $Args do? Where is the input data for it ?

like in technet blog people say:

"For example, the following commands define the $ps variable in the local
session and then use it in a remote command. The command uses $log as
the parameter name and the local variable, $ps, as its value.

C:\PS>$ps = "Windows PowerShell"

C:\PS>Invoke-Command -ComputerName S1 -ScriptBlock {param($log) Get-WinEvent -logname $log} -ArgumentList $ps"

$MailUsers — parameter name
$Args = -ArgumentList (Get-Content users.txt) — parameter value

Did I understand right ?

by ArtB0514 at 2013-01-10 13:46:50

Yes, $Args is the passed-in value of the -ArgumentList parameter. Note that it is an array object by default, so $Args[0] will always be the first element and $Args[-1] the last element. Check Get-Help Invoke-Command -Full for complete details and examples. Also see Don's excellent books and topics on remoting.

There's also quite a few about_ help topics to check out, starting with about_Remoting and about_PSSessions.

by lopyeg at 2013-01-11 04:19:20

nothing works (((
Hello guy
Can anybody help me once more ?

i sent Invoke-Command to remote PC (Server has no AD role, there is only AD module there)
and if the cmdlet inside -Scriptblock is about AD – i got a failure

Invoke-Command -Session $ps -ScriptBlock {dir} –works
Invoke-Command -Session $ps -ScriptBlock {get-aduser mylogin} -failure "The term 'get-aduser' is not recognized as the name of a cmdlet, function blablabla"

I also tried to insert Import-Module activedirectory in side -Scriptblock {} -nothing too

by lopyeg at 2013-01-11 04:27:07

even if
PS C:\WINDOWS> Enter-PSSession $ps
[RemoteSRV]: PS C:\Users\x\Documents> get-aduser green
The term 'get-aduser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo :
+ FullyQualifiedErrorId : CommandNotFoundException

[RemoteSRV]: PS C:\Users\x\Documents> Import-Module activedirectory
WARNING: Error initializing default drive: 'Unable to contact the server. This may be because this server does not
exist, it is currently down, or it does not have the Active Directory Web Services running.'.

by lopyeg at 2013-01-11 05:30:32

Invoke-Command -session $session -script { Import-Module ActiveDirectory }
Import-PSSession -session $session -module ActiveDirectory -prefix Rem
get-remaduser username

gives

Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have
the Active Directory Web Services running.
+ CategoryInfo : ResourceUnavailable: (lopatin:ADUser) [Get-ADUser], ADServerDownException
+ FullyQualifiedErrorId : Unable to contact the server. This may be because this server does not exist, it is curr
ently down, or it does not have the Active Directory Web Services running.,Microsoft.ActiveDirectory.Management.Co
mmands.GetADUser

by ArtB0514 at 2013-01-11 06:47:05

Invoke-Command -session $session -script { Import-Module ActiveDirectory }
Invoke-Command -Session $Session -ScriptBlock { Get-ADUser $Args[0] } -ArgumentList "UserSamAccountName"

by lopyeg at 2013-01-11 07:00:00

Invoke-Command -session $session -script { Import-Module ActiveDirectory }
WARNING: Error initializing default drive: 'Unable to contact the server. This may be because this server does not
exist, it is currently down, or it does not have the Active Directory Web Services running.'.

by ArtB0514 at 2013-01-11 09:13:34

So how did you create the PSSession? Is it connected to a Domain Controller? What does Get-PSSession $Session tell you?

by lopyeg at 2013-01-12 10:21:33

Hello ArtB0514 and thx U for responses.

U R right about my bad $ session ((

PS C:\WINDOWS> $session = New-PSSession remotepc
PS C:\WINDOWS> Get-PSSession $Session
Get-PSSession : Remote Session is not available for System.Management.Automation.Runspaces.PSSession.
At line:1 char:14
+ Get-PSSession < <<< $Session
+ CategoryInfo : InvalidArgument: (System.Manageme...paces.PSSession:String) [Get-PSSession], ArgumentException
+ FullyQualifiedErrorId : RemoteRunspaceNotAvailableForSpecifiedComputer,Microsoft.PowerShell.Commands.GetPSSessionCommand

$session = {New-PSSession remotepc} like in Trainsignal courses, arises same failure

i dont know where i was wrong, and i tired of googling((

by ArtB0514 at 2013-01-14 06:16:10

Do you have $ErrorActionPreference set to "SilentlyContinue"? If so, try this to see what the issue with creating the session is.
$Session = New-PSSession remotepc -ErrorAction 'Stop'
I would guess that your problem is one of a) WinRM isn't running on the remote machine (execute Enable-PSRemoting on it) or b) you need to pass credentials that have local administrator permissions.

by RichardSiddaway at 2013-01-15 01:43:06

Is the ActiveDirectory module definitely available on the remote machine. If I remember correctly you need to start PowerShell with elevated privileges for the AD provider to create the drive. You can't do that through a remote session.

In this case it would seem to be an easier approach to install the module on your workstation or even switch to the Quest cmdlets

by lopyeg at 2013-01-16 03:50:56

I will test this approach, when return to workplace
http://www.powershellmagazine.com/2012/ ... -remoting/

by lopyeg at 2013-01-18 00:09:15

Quest cmdlets are easier, faster )