J'ai un script PowerShell qui appelle le module PowerShell de ServiceFabricSDK pour déclencher le déploiement d'applications Azure Service Fabric. Tout fonctionne correctement lorsque j'appelle PowerShell directement, mais si je l'appelle à partir d'un fichier cmd
en utilisant la commande powershell
, les choses se gâchent et il semble que le contexte se perde entre mon script PowerShell et le module qu'il appelle.Appel de PowerShell à partir de cmd, le contexte est perdu lorsque ce PowerShell appelle un autre module
Voici ce que je veux dire:
Mon script PowerShell prend le nom de l'application et quelques autres paramètres, les importations ServiceFabricSDK.psm1
, exécute Connect-ServiceFabricCluster
pour se connecter au cluster Fabric de service local et invoque finalement Publish-NewServiceFabricApplication
pour démarrer le déploiement:
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
Si je lance directement tout cela fonctionne bien:
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.
J'ai aussi 01 fichierqui appelle dans cette 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
Il n'y a rien de plus que simplement appeler dans cette PowerShell, mais malheureusement, le déploiement échoue, car le module ne voit aucune connexion de cluster:
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
j'ai joué avec et Connect-ServiceFabricCluster
résultat peut être vérifié par Get-ServiceFabricClusterConnection
, ce qui est ce que Publish-NewServiceFabricApplication.ps1
fait. Je peux l'exécuter dans mon PowerShell juste avant d'appeler au Publish-NewServiceFabricApplication.ps1
et il renvoie les bonnes données de connexion, même lorsqu'il est exécuté à partir de appStart.cmd
. Pourquoi la connexion est-elle perdue lorsqu'elle est appelée dans le script?
Selon la version PowerShell vous exécutez, ce qui se passe lorsque vous ajoutez le [-MTA ou -STA] (https://stackoverflow.com/questions/127188/could-you-explain-sta-and-mta) paramètre: 'call powershell -MTA% ScriptsPath% \\ Deploy-Application.ps1 -applicationName"% 1 "-repoTargetPath"% BaseDir% \\ target \\ "-targetEnvironment" dev.local "' – iRon
@iRon Aucune différence. – MarcinJuraszek
Essayez le modèle: 'powershell -Command {% ScriptsPath% \\ Deploy-Application.ps1 -applicationName \"% 1 \ "-repoTargetPath \"% BaseDir% \\ cible \\\ "-targetEnvironment \" dev. local \ "}' –