2017-08-17 4 views
0

私はServiceFabricSDK PowerShellモジュールを呼び出してAzureサービスファブリックアプリケーションの展開を起動するPowerShellスクリプトを持っています。 PowerShellを直接呼び出すとすべて正常に動作しますが、ファイルからpowershellコマンドを使用して呼び出すと、何かがうんざりしてしまい、PowerShellスクリプトと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. 

私も01を持っていますその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から実行しても正しい接続データを返します。 スクリプトに呼び出されたときに接続が失われるのはなぜですか?

+0

あなたが実行して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

+0

@iRon違いはありません。 – MarcinJuraszek

+0

パターンを試してください: 'powershell -Command {%ScriptsPath%\\ Deploy-Application.ps1 -applicationName \"%1 \ "-repoTargetPath \"%BaseDir%\\ target \\\ -targetEnvironment \ "dev。ローカル\ "}' –

答えて

関連する問題