locked
Invoke-Command ArgumentList RRS feed

  • Question

  • 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?
    Wednesday, September 9, 2015 8:40 PM

Answers

  • 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


    \_(ツ)_/


    • Edited by jrv Wednesday, September 9, 2015 9:52 PM
    • Marked as answer by BrianG_WPSIC Wednesday, September 9, 2015 10:09 PM
    Wednesday, September 9, 2015 9:49 PM

All replies

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


    \_(ツ)_/

    Wednesday, September 9, 2015 9:04 PM
  • 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


    Wednesday, September 9, 2015 9:08 PM
  • 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:


    Write-Output "Name: $EmpName"
    Write-Output "Manager: $empManager"
    ...
    

    Technically, even the Write-Output at the beginning of each line is not necessary, because PowerShell outputs string literals. Also, PowerShell does variable interpolation inside double-quote strings.

    You also don't need the exit statement at the end of the script. The exit statement is only needed when you want to terminate the script before it reaches the end (e.g., in a conditional statement).


    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 9, 2015 9:18 PM
  • 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
    


    \_(ツ)_/


    • Edited by jrv Wednesday, September 9, 2015 9:20 PM
    Wednesday, September 9, 2015 9:19 PM
  • 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.

    Wednesday, September 9, 2015 9:20 PM
  • 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...
    Wednesday, September 9, 2015 9:35 PM
  • 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


    \_(ツ)_/


    • Edited by jrv Wednesday, September 9, 2015 9:52 PM
    • Marked as answer by BrianG_WPSIC Wednesday, September 9, 2015 10:09 PM
    Wednesday, September 9, 2015 9:49 PM
  • 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.
    Wednesday, September 9, 2015 9:53 PM
  • 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.

    \_(ツ)_/

    Wednesday, September 9, 2015 9:56 PM
  • 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.
    Wednesday, September 9, 2015 10:00 PM
  • Place this line in your remote script:

    $args


    \_(ツ)_/

    Wednesday, September 9, 2015 10:05 PM
  • 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!
    Wednesday, September 9, 2015 10:08 PM
  • I just tested it and my script works perfectly.

    \_(ツ)_/

    Wednesday, September 9, 2015 10:12 PM
  • I pasted your remote script into my test file and it works correctly.


    \_(ツ)_/

    Wednesday, September 9, 2015 10:14 PM
  • The named arguments worked fine for me.

    Well - good that you are up.


    \_(ツ)_/

    Wednesday, September 9, 2015 10:15 PM