Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Seem to be using Invoke-Command wrong

Posted on 2016-08-18
9
52 Views
Last Modified: 2016-08-19
I'm using Invoke-Command to run the following code:
Invoke-Command -Session $sqlSession -ScriptBlock {"C:\Installs\migration_Install_SQL.ps1" -suiOU $c -ppw $pumaPW -rpw $rhinoPW -sqtype $sqlType}

Open in new window

In the target script I have the following:
param(
    [string]$ppw,
    [string]$rpw,
    [string]$suiOU,
    [string]$sqType
    )

Open in new window


But it doesn't seem to be passing the parameters.  I can't seem to figure out what I've got messed up here.
0
Comment
Question by:Dustin Saunders
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 40

Expert Comment

by:footech
ID: 41761648
If you want to pass arguments to a remote session, you need to use the -ArgumentList parameter of Invoke-Command.  Arguments passed in this way are done by position, not by name.
Another option is to define the variable in the remote session.
Last option is to use the $using scope modifier for locally defined variables.  A good resource is the about_Remote_Variables help topic.
0
 
LVL 12

Author Comment

by:Dustin Saunders
ID: 41761658
I tried that first, but when I used -ArgumentList $x,$y,$z, $args[x] was/is returning nothing.  

It works fine for me with one argument ($c is a string) here.  But doing multiple arguments it won't execute.
Invoke-Command -Session $usersSession -ScriptBlock {powershell.exe -file "D:\Backups\Scripts\dm3MigrationBackup.ps1" $args[0]} -Args $c

Open in new window


Could you do an example of passing 4 arguments to a remote .ps1?  I gotta be doing something stupid wrong.
0
 
LVL 80

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 100 total points
ID: 41761693
grab and test these 2 scripts..
my thinking is it is the params
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [string]$ppw,

local script
$remotecomputer = 'DJOHNSON-W10'
#remove-item C:\test\eetest.txt -ErrorAction SilentlyContinue
$ppw = 'Parameter1'
$rpw = 'Parameter2'
$suiOU = 'Parameter3'
$sqType = 'Parameter4'
$pso = New-PSSessionOption -NoMachineProfile 
#-OperationTimeout 10000
$session = new-pssession -computername $remotecomputer -SessionOption $pso
invoke-command -Session $session -ScriptBlock {
    'C:\test\eetest.ps1' 
        -suiOU $suiou
        -ppw $ppw
        -rpw $rpw
        -sqtype $sqType
        }
        $rem = "\\" + $remotecomputer + "\C$\test\eetest.txt"
        start-process $rem

Open in new window

remote script
<#
.Synopsis
   Short description
.DESCRIPTION
   Long description
.EXAMPLE
   Example of how to use this cmdlet
.EXAMPLE
   Another example of how to use this cmdlet
#>
#function Verb-Noun
#{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [string]$ppw,
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [string]$rpw,
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [string]$suiOU,
     [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [string]$sqType
    )

    Begin
    {
    }
    Process
    {
    write-output ('Parameter 1: {0}' -f $ppw)
    write-output ('Parameter 2: {0}' -f $rpw)
    write-output ('Parameter 3: {0}' -f $suiOU)
    write-output ('Parameter 4: {0}' -f $sqType)
    write-output ($ppw,$rpw,$suiOU,$sqType) | Out-File c:\test\eetest.txt
    }
    End
    {
    }
#}

Open in new window

0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 84

Expert Comment

by:oBdA
ID: 41761694
Sort of - all you do is put a string into the pipeline, followed by some more or less unidentifiable objects.
Put an ampersand in front of your script path, and it should work.
Invoke-Command -Session $sqlSession -ScriptBlock {& "C:\Installs\migration_Install_SQL.ps1" -suiOU $c -ppw $pumaPW -rpw $rhinoPW -sqtype $sqlType} 

Open in new window

0
 
LVL 40

Expert Comment

by:footech
ID: 41761705
The .PS1 is a file on the remote machine?
0
 
LVL 12

Author Comment

by:Dustin Saunders
ID: 41762579
@David - I'll test shortly, thank you.

@oBdA - That allows the script to execute, but it didn't actually pass in any of the parameters.
OU:              
SQL Type:        
Puma Password:   
Rhino Password:  
Transcript started, output file is c:\Installs\Logs\Setup-.log
Starting SQL Server Install

Open in new window


@footech - Yes, it's on a remote machine executed through a PS Session ($sqlSession)
0
 
LVL 12

Author Comment

by:Dustin Saunders
ID: 41762594
@David -
Same thing unfortunately, it doesn't accept the parameters and then after running I get:
The term '-suiOU' 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          : ObjectNotFound: (-suiOU:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : DM3SQL041
 
The term '-ppw' 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          : ObjectNotFound: (-ppw:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : DM3SQL041
 
The term '-rpw' 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          : ObjectNotFound: (-rpw:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : DM3SQL041
 
The term '-sqtype' 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          : ObjectNotFound: (-sqtype:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : DM3SQL041

Open in new window

0
 
LVL 84

Accepted Solution

by:
oBdA earned 400 total points
ID: 41762599
Sorry:
Invoke-Command -Session $sqlSession -ScriptBlock {
	Param($suiOU, $ppw, $rpw, $sqtype)
	& "C:\Installs\migration_Install_SQL.ps1" -suiOU $suiOU -ppw $ppw -rpw $rpw -sqtype $sqtype
} -ArgumentList $c, $pumaPW, $rhinoPW, $sqlType

Open in new window

0
 
LVL 12

Author Closing Comment

by:Dustin Saunders
ID: 41762612
@oBdA - Bingo!  I was missing Param(),  Thanks!

@David - Thanks for taking the time to put that together, I appreciate it.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
A procedure for exporting installed hotfix details of remote computers using powershell
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question