pass local script to remote script block

rated by 0 users
This post has 1 Reply | 1 Follower

Not Ranked
Posts 2
scottw512 Posted: 12-20-2010 4:24 PM

Hi,

 

I'm using invoke-command to run script code on a remote computer. I have a file with a library of functions I'd like to pass into the remotely executed script, but I'm only getting the first line. The problem is something to do with my ArgumentList but I can't get one that works. Here's what I have

 

$scr = Get-Content ".\function_lib.ps1"

invoke-command -ComputerName remote-computer -ArgumentList $scr -ScriptBlock {

param ([string]$scr)

invoke-expression -Command $scr

use-function-from-lib -parm1 "parm1"

}

 

If I write the contents of $scr to a file, all I get is the first line. How can I pass the string representation of the function library to the remote script?

 

Thanks,

Scott

Not Ranked
Posts 2

I finally figured this out. The Get-Content cmdlet was stripping out line breaks, and the parameter cast looks like it was messing something up. Here's the fixed code

 

$scr = [io.file]::ReadAllText("function_lib.ps1")

invoke-command -ComputerName remote-computer -ArgumentList $scr -ScriptBlock {

    param ($scr)

    invoke-expression -Command $scr

    use-function-from-lib -parm1 "parm1"

}

Page 1 of 1 (2 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.