我有一个调用ServiceFabricSDK PowerShell模块来触发Azure Service Fabric应用程序部署的PowerShell脚本。当我直接调用PowerShell时,所有工作都正常,但如果我使用powershell
命令从cmd
文件中调用它,事情就会变得混乱起来,看起来在我的PowerShell脚本和它调用的模块之间会丢失一些上下文。从cmd调用PowerShell,当PowerShell调用另一个模块时,上下文会丢失
这里就是我的意思是:
我的PowerShell脚本需要的应用程序名称和夫妇更多的参数,进口ServiceFabricSDK.psm1
,运行Connect-ServiceFabricCluster
连接到本地服务织物集群,并最终调用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中运行它,并且它会返回正确的连接数据,即使从appStart.cmd
运行。 为什么在调用脚本时连接丢失?
根据运行PowerShell的版本,会发生什么,当你添加[-MTA或-STA](https://stackoverflow.com/questions/127188/could-you-explain-sta-and-mta)参数:'call powershell -MTA%ScriptsPath%\\ Deploy-Application.ps1 -applicationName“% 1“-repoTargetPath”%BaseDir%\\ target \\“-targetEnvironment”dev.local“' – iRon
@iRon没有区别。 – MarcinJuraszek
试试模式: 'powershell -Command {%ScriptsPath%\\ Deploy-Application.ps1 -applicationName \“%1 \”-repoTargetPath \“%BaseDir%\\ target \\\”-targetEnvironment \“dev。 local \“}' –