0

I need to connect to a server that is in another domain. From that server I connect to 3 other domains to look up user accounts in each of those domains.

I connect to the terminal server, and then run 'invoke-command'. A few problems occur:

1st - I specify what my credentials are, yet when i run the script it prompts for credentials.

2nd - i get an error stating that the invokemethod is null

Here's the script:

PARAM (
    [Parameter(ValueFromPipeline)]
    $FullName
)
###Creds###
$Password = Read-Host -AsSecureString "Enter Your Password" 
$apcred = New-Object System.Management.Automation.PSCredential 
("server\username",$Password) 

### Double Hop  ###
$Session = New-PSSession -Name RDP -ComputerName Server.com -Credential
($apcred)
Enable-WSManCredSSP -Role Client -DelegateComputer server.com -Force 
Invoke-Command -Session $session -ScriptBlock {Enable-WSManCredSSP -Role 
Server -Force}

    Invoke-Command -Session $session -ScriptBlock {
$rrs = Get-ADUser -Filter * -Server server2.com -Credential ($apcred)
$rrsusers = ($rrs | Where-Object{$_.name -eq $Name})

This is the error I get:

You cannot call a method on a null-valued expression.
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Justin Beagley
  • 279
  • 2
  • 5
  • 14
  • So I've tried both methods that were specified in that other question (passing variable from local scope to remote). The param method didn't work - it still prompted me for user/pass - and gave same error: You cannot call a method on a null-valued expression. + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull | However, when i used the $using: it didn't prompt me for the password... but it still gave the error - null-valued expression. Any ideas? – Justin Beagley Aug 18 '16 at 14:16

1 Answers1

0

Script blocks have their own scope, so $apcred is null in the context of the script block you're running. You can pass the object in in the -ArgumentList parameter, or prefix the variable with "using", as in $using:apcred. This causes the ScriptBlock to reach into the outer scope to get the $apcred variable.