如何在 start-job 的 -InitializationScript 中使用 2 个以上的函数

how to use more than 2 functions in -InitializationScript in start-job

我想在以下 PowerShell 启动作业代码中的 -InitializationScript 中使用 2 个函数:

$IISfunc={ Function pool_RemoteServer ($PoolName,$server)
{
    "doing something in iis"
} 
}

$AppState= { Function GetAppState($AppServer,$AppName)
           {
            "doing something in appserver"

            }
            }

$block ={
        Param([string] $AppServer,
              [string] $AppName,
              [string] $PoolName,
              [string] $AppState,
              [string] $IISfunc
             )
              pool_RemoteServer -PoolName $PoolName -server $AppServer
              GetAppState -AppServer $AppServer -AppName $AppName
               }


Get-Job | Remove-Job
$MaxThreads = 20
invoke-service -JobserviceName  $JobserviceName

    foreach ($AppServer in $AppServers -split ",")
            {
                While ($(Get-Job -state running).count -ge $MaxThreads){
                    Start-Sleep -Milliseconds 3
            }
        Start-Job -Scriptblock $block -ArgumentList $AppServer,$AppName,$PoolName -InitializationScript $AppState,$IISfunc

            }

While ($(Get-Job -State Running).count -gt 0){
    start-sleep 1
}

foreach($job in Get-Job){
    $info= Receive-Job -Id ($job.Id)
}

Get-Job | Remove-Job

我遇到过这个错误: Start-Job:无法将 'System.Object[]' 转换为参数 'InitializationScript' 所需的类型 'System.Management.Automation.ScriptBlock'。不支持指定的方法。 在 line:21 char:95 + ... lizationScript $AppState,$IISfunc

哦!它是如此简单的问题!请在您的代码中包含以下解决方案:

  $func={ Function pool_RemoteServer ($PoolName,$server)
        {
            "doing something in iis"
        } 

          Function GetAppState($AppServer,$AppName)
        {
        "doing something in appserver"
        }
        }
  • 亲切的问候