Solved

Seem to be using Invoke-Command wrong

Posted on 2016-08-18
9
60 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 13

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 81

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

 
LVL 85

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 13

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 13

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 85

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 13

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

Is Your AD Toolbox Looking More Like a Toybox?

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

A brief introduction to what I consider to be the best editor for PowerShell.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 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