Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Connect-ServiceFabricCluster / Publish-NewServiceFabricApplication / etc depend on magic global variable $clusterConnection #137

Closed
troshko111 opened this issue Jan 18, 2017 · 1 comment

Comments

@troshko111
Copy link

troshko111 commented Jan 18, 2017

Wrote automated Service Fabric app deployment (cluster + app), publishing SF app looks like this:

Connect-ServiceFabricCluster -ConnectionEndpoint $clusterEndpoint `
                             -X509Credential `
                             -ServerCertThumbprint $sfCommunicationCertThumbprint `
                             -FindType FindByThumbprint `
                             -FindValue $sfDeploymentCertThumbprint `
                             -StoreLocation "CurrentUser" `
                             -StoreName "My" | Out-Null

# Later, different function
return Publish-NewServiceFabricApplication -ApplicationName $name `
                                           -Action "RegisterAndCreate" `
                                           -OverwriteBehavior "Always" `
                                           -ApplicationPackagePath $package `
                                           -ApplicationParameter $settings

Apparently, this will work if ran from interactive PS session and never work if called from a PowerShell function, even if you retry it forever. The reason?

Because this Connect-ServiceFabricCluster defines a variable $clusterConnection which is then expected to be accessible in current scope. In case of a function, it just won't be, since it's declared as local variable.

Work around would be

        Connect-ServiceFabricCluster -ConnectionEndpoint $clusterEndpoint `
                                     -X509Credential `
                                     -ServerCertThumbprint $sfCommunicationCertThumbprint `
                                     -FindType FindByThumbprint `
                                     -FindValue $sfDeploymentCertThumbprint `
                                     -StoreLocation "CurrentUser" `
                                     -StoreName "My"

        $global:clusterConnection = $clusterConnection

To manually global it

@heeldin
Copy link

heeldin commented Aug 28, 2017

Trying to connect in a function will set the connection at the function’s scope which is lost once they return to their script again. I think this is the simplest we can get with PowerShell.
Setting it manually global is correct.
Another solution would be :
To create another cmdlet called “Set-ServiceFabricClusterConnection –ClusterConnection ” . This cmdlet will take a cluster connection returned by our connect cluster cmdlet and set it in the $clusterconnection variable.
So in the below scenario they will need to modify their connect command to return the connect-servicefabriccluster result.
Function connect{
Return connect-serivefabriccluster
}
and then set it in the script by calling the new Set-ServiceFabricClusterConnection command
Connect | Set-ServiceFabricClusterConnection

@heeldin heeldin closed this as completed Aug 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants