将变量传递给InlineScript内部的Invoke-Command,工作流内部

时间:2018-05-08 11:14:04

标签: powershell

我正在尝试创建一个连接到各种服务器的脚本,应该附加PSDrive并复制文件。问题在于我无法将变量传递给Invoke-Command脚本块。

workflow kopijobb {
    param ([string[]]$serverList, $creds, $basePath)

    foreach -parallel ($server in $serverList){

        # Use the sequence keyword, to ensure everything inside of it runs in order on each computer.
        sequence {

            #Use the inlinescript keyword to allow PowerShell workflow to run regular PowerShell cmdlets
            inlineScript{

                $path = $using:basePath

                Write-Host "Starting $using:server using $path"
                #Create session for New-PSSession
                $session = New-PSSession -ComputerName $using:server -Credential $using:creds

                # Copy Java and recreate symlink
                Invoke-Command -Session $session -ScriptBlock {

                    # Make a PSDrive, since directly copying from UNC-path doesn't work due to credential-issues
                    New-PSDrive -Name N -PSProvider FileSystem -root $using:path -Credential $using:creds | out-null

我将网络路径传递给$ basePath,我能够在inlineScript块中读取它(我已经尝试将其存储在一个新的变量中进行测试),但是一旦我尝试在New-PSDrive中访问它命令,变量突然变空/无法访问,并且驱动器的安装失败并显示错误Cannot bind argument to parameter 'Root' because it is null.

我不知道为什么失败了,所以我转而采用集体智慧。

1 个答案:

答案 0 :(得分:1)

如果对回答我自己的问题感到尴尬,特别是在同一天,但我在工作中碰到了PowerShell大师,他瞥了一眼脚本并看到了问题:

我必须将-Args添加到Invoke-Command

{{1}}

这当然意味着我必须将所有需要的参数从顶层导入工作流,然后导入Invoke-Command。

相关问题