Solved

Seem to be using Invoke-Command wrong

Posted on 2016-08-18
9
37 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 82

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 82

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

19 Experts available now in Live!

Get 1:1 Help Now