Invoke-Command ArgumentList

I've seen more articles online about this than I can count today, but none of them have solved my problem so I'm hoping I can get help here.  I'm trying to pass six variables from a script running locally on one server to a script that I'm calling on another server.  I keep getting the "Parameter set cannot be resolved using the specified named parameters" error.

Here's what I'm running now:

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "domain\user",$Password

$Session = New-PSSession -CN MADDW81IT14180 -Credential $Credential -Authentication Credssp

Invoke-Command -Session $Session -ComputerName <computername> -ScriptBlock {C:\Powershell_Logging\Process_AD_Account.ps1} -ArgumentList $EmpName,$EmpManager,$EmpTitle,$EmpDept,$EmpLocationOU,$EmpNumber
Does anyone know how I can get this running in Powershell V2?
September 9th, 2015 4:41pm

No one can help you if you do not post your script.  The above is not the script that is causing the problems.

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 5:06pm

Interesting, since I thought the problem was in the Invoke-Command statement.  Here's the relevant portion of the script it's calling.

Param(
	[string]$EmpName,
	[string]$EmpManager,
	[string]$EmpTitle,
	[string]$EmpDept,
	[string]$EmpLocationOU,
	[string]$EmpNumber
	)
# Import the AD module for PowerShell (requires RSAT installed locally with AD tools enabled)
# Find the user account AD
# Find the manager's user account in AD
# Pre-populate variables as able
# Set account information for location
# Set account information for RDS, organization, etc.
# Write the change back to AD
Start-Transcript -Path C:\PowerShell_Logging\Log.txt -Append -NoClobber
Import-Module ActiveDirectory
echo "Imported AD Module"

# Echo back variables for testing
echo "Name: " $EmpName + $args[0]
echo "Manager: " $EmpManager
echo "Title: " $EmpTitle
echo "Dept: " $EmpDept
echo "Location: " $EmpLocationOU
echo "Number: " $EmpNumber

# Exit after variable echo.  Remove after variables transfer properly.
Exit


September 9th, 2015 5:10pm

I don't think those echo statements are doing what you think they're doing.

echo is an alias for Write-Output. You are using multiple parameters with Write-Output.

I think you probably mean to write something like this:

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 5:19pm

Here is a clean way to do this:

$cred=New-Object -TypeName System.Management.Automation.PSCredential('domain\user',$Password)
$Session=New-PSSession -CN MADDW81IT14180 -Credential $Cred -Authentication Credssp
$props=@{
	Session=$Session
	FilePath='C:\Powershell_Logging\Process_AD_Account.ps1'
	ArgumentList=@($EmpName,$EmpManager,$EmpTitle,$EmpDept,$EmpLocationOU,$EmpNumber)
}
Invoke-Command @props
September 9th, 2015 5:20pm

Bill-

There is more to the script that isn't here, hence the exit statement.  It just doesn't seem relevant to the current problem.  The echo/w-o statements are only for testing, so I'm not really concerned with what the output looks like.

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 5:21pm

Thanks!  I updated to match your code and now I'm getting an error saying it can't find the PS1 file (Cannot find path <ps1 path> because it does not exist).  It's a different error at least...
September 9th, 2015 5:36pm

Thanks!  I updated to match your code and now I'm getting an error saying it can't find the PS1 file (Cannot find path <ps1 path> because it does not exist).  It's a different error at least...

Where is the PS1 file.  If you are remoting it should be on the local system. TO use it on the remote system you ill need to rethink your whole approach.

Something like this.

$props=@{
	Session=$Session
	Command={C:\Powershell_Logging\Process_AD_Account.ps1 $args[0] $args[1] $args[2] $args[3] $args[4] $args[5]}
	ArgumentList=@($EmpName,$EmpManager,$EmpTitle,$EmpDept,$EmpLocationOU,$EmpNumber)
}
Invoke-Command @props
Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 5:51pm

The Process_AD_Account.ps1 script is on a remote machine, MADDW81IT14180 (referenced in the Session definition statement).  I tried the new code you provided and got an error saying it couldn't convert the command string from a string to a scriptblock.
September 9th, 2015 5:54pm

The Process_AD_Account.ps1 script is on a remote machine, MADDW81IT14180 (referenced in the Session definition statement).  I tried the new code you provided and got an error saying it couldn't convert the command string from a string to a scriptblock.

Copy the code again.
Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 5:57pm

I found my mistake in the copy.  The only problem now is that the remote script is only reading the first variable instead of all six.
September 9th, 2015 6:01pm

Place this line in your remote script:

$args

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 6:06pm

Good thought, which indeed confirmed they're all being passed.  I added the lines to map the $args[i] to my named variables and that solved it.  Thank you so much for all of your help!
September 9th, 2015 6:10pm

I just tested it and my script works perfectly.
Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 6:13pm

I pasted your remote script into my test file and it works correctly.

September 9th, 2015 6:15pm

The named arguments worked fine for me.

Well - good that you are up.

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 6:17pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics