title: Deploy Service Fabric from powershell. Error -> Get-ServiceFabricClusterManifest : Cluster connection instance is null
body: <p>I am trying to publish Service Fabric application to Azure with powershell. I want to connect to the cluster and then call script "Deploy-FabricApplication.ps1"(one that is generated when new project is created in visual studio)</p>
  
  <p>To do that I have created new script file which is responsible for connecting with cluster and then from that same script file I am calling "Deploy-FabricApplication.ps1". </p>
  
  <p>After launching my script I am getting following error:</p>
  
  <pre><code>Get-ServiceFabricClusterManifest : Cluster connection instance is null
  At C:\Program Files\Microsoft SDKs\Service Fabric\Tools\PSModule\ServiceFabricSDK\Publish-UpgradedServiceFabricApplication.ps1:116 char:28
  +     $clusterManifestText = Get-ServiceFabricClusterManifest
  +                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : ResourceUnavailable: (:) [Get-ServiceFabricClusterManifest], NullReferenceException
  + FullyQualifiedErrorId : GetClusterConnectionErrorId,Microsoft.ServiceFabric.Powershell.GetClusterManifest
  </code></pre>
  
  <p>In connection script and Deploy-FabricApplication.ps1 I am calling "Test-ServiceFabricClusterConnection" and in both scripts it returns True also in both scripts I am calling "Get-ServiceFabricClusterManifest" and in both cases it returns to me manifest but when I have added "Test-ServiceFabricClusterConnection" to "Publish-UpgradedServiceFabricApplication.ps1" then it has returned that same error as mentioned befor but for "Test-ServiceFabricClusterConnection" call. </p>
  
  <p>Code for connecting script:</p>
  
  <pre><code>Param
  (
      [String]
      $PublishProfileFile,
  
      [String]
      $ApplicationPackagePath,
  
      [Switch]
      $DeployOnly,
  
      [Boolean]
      $UnregisterUnusedApplicationVersionsAfterUpgrade,
  
      [String]
      [ValidateSet('None', 'ForceUpgrade', 'VetoUpgrade')]
      $OverrideUpgradeBehavior = 'None',
  
      [String]
      [ValidateSet('Never','Always','SameAppTypeAndVersion')]
      $OverwriteBehavior = 'Never',
  
      [Switch]
      $SkipPackageValidation,
  
      [String]
      $ConnectionEndpoint,
  
      [String]
      $ServerCertThumbprint,
  
      [String]
      $FindType,
  
      [String]
      $FindValue,
  
      [String]
      $StoreLocation,
  
      [String]
      $StoreName
  )
  
  $connectArgs = @{ ConnectionEndpoint = $ConnectionEndpoint;  X509Credential = $True;  StoreLocation = $StoreLocation;  StoreName = $StoreName;  ServerCertThumbprint = $ServerCertThumbprint; FindType = $FindType;  FindValue = $FindValue }
  
  try
  {
      Connect-ServiceFabricCluster @connectArgs;
      $connection = Get-ServiceFabricClusterConnection;
      Write-Host $connection;
      $m = Get-ServiceFabricClusterManifest
      Write-Host $m;
  }
  catch [System.Fabric.FabricObjectClosedException]
  {
      Write-Warning "Service Fabric cluster may not be connected."
      throw
  }
  
  .\Deploy-FabricApplication.ps1 -PublishProfileFile $PublishProfileFile -ApplicationPackagePath $ApplicationPackagePath -OverrideUpgradeBehavior $OverrideUpgradeBehavior -OverwriteBehavior $OverwriteBehavior -DeployOnly:$DeployOnly -UnregisterUnusedApplicationVersionsAfterUpgrade:$UnregisterUnusedApplicationVersionsAfterUpgrade -UseExistingClusterConnection:$true -SkipPackageValidation:$SkipPackageValidation
  </code></pre>
  
  <p>To summarize, I have no idea how to establish connection to cluster and then use it in Deploy-FabricApplication.ps1</p>
  
  <p>Thanks for help</p>