0

I am trying to pass a local function to remote server with multiple arguments. I plan on running this for a number of servers, so the argument list will change for each server type. Here is a snippet of the function I am trying to call with the command I am using. The remote computer accepts the first argument, but never gets the second it seems.

Function Backup-Server{
[CmdletBinding()]
param
(
    [Parameter(Mandatory=$true)]
    [string]$BackupTarget,
    [Parameter(Mandatory=$false)]
    [switch]$BareMetal,
    [Parameter(Mandatory=$false)]
    [switch]$SystemState,
    [Parameter(Mandatory=$false)]
    [string[]]$Volume,
    [Parameter(Mandatory=$false)]
    [switch]$CriticalVolumes,
    [Parameter(Mandatory=$false)]
    [string[]]$File
)

Do stuff...
}

Invoke-Command -ComputerName dc-01.test.com -ScriptBlock ${Function:Backup-Server} -ArgumentList @("$BackupTarget","$SystemState") -AsJob

Any thoughts? I appreciate any help!

1 Answers1

0

I suggest you include everything in a script file locally on your machine (e.g c:\script.ps1) and then invoke the file on the target machines as they were run locally:

Invoke-Command -FilePath c:\script.ps1 -ComputerName server1,server2
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • That code is working in the same fashion as mine, it gets the first argument, but not the second. I am thinking it has something to do with specifying which argument goes with which parameter since they are not all mandatory... Also how would I pass a switch type argument to the switch parameters? – user3066290 Dec 04 '13 at 15:09
  • What I meant is to include the command in the script so the call to the function will be made locally on the target server. – Shay Levy Dec 04 '13 at 15:39
  • I'm not understanding what you are saying... It doesn't matter to me if the Server-Backup function is a function or a script. I want to call that function/script with different parameters for different servers. The problem is it isn't passing all the arguments to the command. Think this thread has something to do with it http://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command but it still isn't working for me. – user3066290 Dec 04 '13 at 16:43