2013-07-21 50 views
3

是否有方法使用Powershell啓動過程cmdlet啓動新的Powershell會話並將局部變量(一次是數組)傳遞給腳本塊?Powershell啓動 - 啓動Powershell會話並傳遞本地變量的過程

實施例:

$Array = @(1,2,3,4) 

$String = "This is string number" 

$Scriptblock = {$Array | ForEach-Object {Write-Host $String $_}} 

Start-Process Powershell -ArgumentList "$Scriptblock" 

感謝。

+0

請退後一步,並說明你正在試圖解決的,而不是你所認爲的實際問題作爲解決方案。你爲什麼認爲你需要這個? –

+1

您可以使用'&'運算符調用表達式。像你的例子中的'&$ Scriptblock'一樣。 –

+0

我想開始一個新的PowerShell會話,並在該會話中運行一個腳本塊。該腳本塊將需要包含來自初始會話的局部變量。 – atownson

回答

2

我能得到這個用「/」創建一個字符串加入陣列並進入腳本塊到另一個的.ps1腳本一起工作相應的參數和分裂加入字符串返回到第二個腳本中的數組,並使用

Start-Process Powershell -ArgumentList "&C:\script.ps1 $JoinedArray $String" 

醜陋,但它是我能得到它的工作的唯一途徑。感謝所有的答覆。

2

我很確定沒有直接的方法將變量從一個PowerShell會話傳遞到另一個會話。你可以做的最好的是一些解決方法,比如在-ArgumentList中傳遞的代碼中聲明變量,在調用會話中插值。如何將變量插值到-ArgumentList中的聲明取決於哪些類型的變量。對於數組和字符串,你可以做這樣的事情:

$command = '<contents of your scriptblock without the curly braces>' 

Start-Process powershell -ArgumentList ("`$Array = echo $Array; `$String = '$String';" + $command) 
1

您可以將腳本塊的內容封裝在函數中,然後從ArgumentList中調用該函數,並將變量作爲參數傳遞給函數as I do on this post

$ScriptBlock = { 
    function Test([string]$someParameter) 
    { 
     # Use $someParameter to do something... 
    } 
} 

# Run the script block and pass in parameters. 
$myString = "Hello" 
Start-Process -FilePath PowerShell -ArgumentList "-Command & {$ScriptBlock Test('$myString')}" 
0

command line options for PowerShell.exe說,你應該使用腳本塊時可以傳遞參數加入-args:

PowerShell.exe -Command { - | <script-block> [-args <arg-array>] | <string> [<CommandParameters>] } 

然而,當我嘗試這樣做,我得到以下錯誤:

-args : The term '-args' 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.

我將$MyInvocation | fl添加到腳本塊以查看發生了什麼,看起來-args僅附加到腳本塊中的反序列化命令(因此-args不是有效命令)。我也嘗試過使用GetNewClosure() and $Using:VariableName,但是這些只有在腳本塊被調用時才起作用(與我們使用它來序列化/反序列化命令的情況相反)。

我能夠通過將其包裝在deadlydog's answer之類的功能中來使其工作。

$var = "this is a test" 

$scriptblock = { 
    $MyInvocation | fl #Show deserialized commands 
    function AdminTasks($message){ 
     write-host "hello world: $message" 
    } 
} 

Start-Process powershell -ArgumentList '-noexit','-nologo','-noprofile','-NonInteractive','-Command',$scriptblock,"AdminTasks('$var')" -Verb runAs #-WindowStyle Hidden 

#Output: 
MyCommand    : 
         $MyInvocation | fl #Show deserialized commands 
         function AdminTasks($message){ 
         write-host hello world: $message 
         } 
         AdminTasks('this is a test') 
BoundParameters  : {} 
UnboundArguments  : {} 
ScriptLineNumber  : 0 
OffsetInLine   : 0 
HistoryId    : 1 
ScriptName   : 
Line     : 
PositionMessage  : 
PSScriptRoot   : 
PSCommandPath   : 
InvocationName  : 
PipelineLength  : 2 
PipelinePosition  : 1 
ExpectingInput  : False 
CommandOrigin   : Runspace 
DisplayScriptPosition : 


hello world: this is a test 

結束語在腳本塊,並使用$args[0]$args[1]也工作,要知道,你很多需要包裝的$ var0或$ VAR1引號,如果有當它被反序列化和使用問題`$爲了防止$ SB從「」自變量不來電者的範圍存在被替代:

$var0 = "hello" 
$var1 = "world" 

$scriptblock = { 
    $MyInvocation | fl #Show deserialized commands 
    $sb = { 
     write-host $args[0] $args[1] 
    } 
} 

Start-Process powershell -ArgumentList '-noexit','-nologo','-noprofile','-NonInteractive','-Command',$scriptblock,"& `$sb $var0 $var1" 
相關問題