Solved

Seem to be using Invoke-Command wrong

Posted on 2016-08-18
9
45 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 39

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 78

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
 
LVL 83

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 39

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 83

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now