从 cmd 调用 PowerShell,当 PowerShell 调用另一个模块时上下文丢失

Invoke PowerShell from cmd, context gets lost when that PowerShell calls into another module

我有一个 PowerShell 脚本,它调用 ServiceFabricSDK PowerShell 模块来触发 Azure Service Fabric 应用程序部署。当我直接调用 PowerShell 时一切正常,但如果我使用 powershell 命令从 cmd 文件调用它,事情就会变得一团糟,看起来我的 PowerShell 脚本和模块之间的某些上下文丢失了它调用了。

我的意思是:

我的 PowerShell 脚本采用应用程序名称和更多参数,导入 ServiceFabricSDK.psm1、运行s Connect-ServiceFabricCluster 以连接到本地 Service Fabric 集群并最终调用 Publish-NewServiceFabricApplication 以开始部署:

param (
    [Parameter()]
    [ValidateNotNullOrEmpty()]
    [string]
    $applicationName,

    [Parameter()]
    [ValidateNotNullOrEmpty()]
    [string]
    $repoTargetPath,

    [Switch]
    $retail,

    [Parameter()]
    [ValidateNotNullOrEmpty()]
    $targetEnvironment = "dev.local"
)

# Load ServiceFabricSDK first
Write-Output "Loading Service Fabric SDK module"
Import-Module "$ENV:ProgramFiles\Microsoft SDKs\Service Fabric\Tools\PSModule\ServiceFabricSDK\ServiceFabricSDK.psm1"

# Connect to the local cluster
Write-Output "Connecting to local Service Fabric cluster"
Connect-ServiceFabricCluster | Out-Null
Write-Output "Successfully connected to local Service Fabric cluster"

$repoTargetPath = $repoTargetPath.TrimEnd('\')
$target = if($retail.IsPresent) { "retail" } Else { "debug" }
$applicationTargetBasePath = "$repoTargetPath\distrib$target\applications$applicationName"
$applicationPackagePath = "$applicationTargetBasePath\package\"
$applicationParametersFilePath = "$applicationTargetBasePath\applicationParameters$applicationName.$targetEnvironment.xml"

Write-Output "Deploying application $applicationName to local Service Fabric cluster"

# Deploy the application
Publish-NewServiceFabricApplication -ApplicationPackagePath $applicationPackagePath -ApplicationParameterFilePath $applicationParametersFilePath -ApplicationName "fabric:/$applicationName" -OverwriteBehavior Always -Verbose -SkipPackageValidation

如果我直接 运行 它一切正常:

PS G:\repo> .\build\scripts\Deploy-Application.ps1 -applicationName HelloWorld -targetEnvironment "dev.local" -repoTargetPath G:\repo\target\
Loading Service Fabric SDK module
Connecting to local Service Fabric cluster
WARNING: Cluster connection with the same name already existed, the old connection will be deleted
Successfully connected to local Service Fabric cluster
Validating the package and application parameters
Deploying application HelloWorld to local Service Fabric cluster
An application with name 'fabric:/HelloWorld' already exists in the cluster with Application Type 'HelloWorldType' and Version '1.0.20170816-seed-1    207928760'. Removing it.
Remove application instance succeeded
Unregister application type succeeded.
Copying application to image store...
Copy application package succeeded
Registering application type...
Register application type succeeded
Removing application package from image store...
Remove application package succeeded
Creating application...

ApplicationName        : fabric:/HelloWorld
ApplicationTypeName    : HelloWorldType
ApplicationTypeVersion : 1.0.20170816-seed-1207928760
ApplicationParameters  : { "HelloWorldServiceType_InstanceCount" = "2" }

Create application succeeded.

我还有 appStart.cmd 调用该 PowerShell 的文件:

@if "%_echo%"=="" echo off

pushd
cd %BaseDir%

if "%1" == "" goto error

echo powershell %ScriptsPath%\Deploy-Application.ps1 -applicationName "%1" -repoTargetPath "%BaseDir%\target\" -targetEnvironment "dev.local"
call powershell %ScriptsPath%\Deploy-Application.ps1 -applicationName "%1" -repoTargetPath "%BaseDir%\target\" -targetEnvironment "dev.local"

GOTO :EOF

除了调用那个 PowerShell 之外别无他法,但不幸的是部署失败,因为模块看不到集群连接:

G:\repo>appStart HelloWorld
powershell G:\repo\build\scripts\Deploy-Application.ps1 -applicationName "HelloWorld" -repoTargetPath "G:\repo\target\" -targetEnvironment "dev.local"
Loading Service Fabric SDK module
Connecting to local Service Fabric cluster
Successfully connected to local Service Fabric cluster
Validating the package and application parameters
Deploying application HelloWorld to local Service Fabric cluster
VERBOSE: System.NullReferenceException: Cluster connection instance is null
WARNING: Unable to Verify connection to Service Fabric cluster.
Get-ServiceFabricClusterConnection : Cluster connection instance is null
At C:\Program Files\Microsoft SDKs\Service
Fabric\Tools\PSModule\ServiceFabricSDK\Publish-NewServiceFabricApplication.ps1:144 char:1
+ Get-ServiceFabricClusterConnection
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Get-ServiceFabricClusterConnection], NullReferenceException
    + FullyQualifiedErrorId : GetClusterConnectionErrorId,Microsoft.ServiceFabric.Powershell.GetClusterConnection

我玩过它 Connect-ServiceFabricCluster 结果可以通过 Get-ServiceFabricClusterConnection 检查,这就是 Publish-NewServiceFabricApplication.ps1 正在做的事情。我可以在调用 Publish-NewServiceFabricApplication.ps1 之前在我的 PowerShell 中 运行 它 returns 正确的连接数据,即使 运行 来自 appStart.cmd为什么在调用脚本时连接丢失?

你可以尝试在Connect-ServiceFabricCluster:

之后将集群连接放入一个全局变量中
$global:clusterConnection = $clusterConnection  

这里有更多信息