Solved

Seem to be using Invoke-Command wrong

Posted on 2016-08-18
9
53 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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