title: Import-AzurePublishSettingsFile throws CryptographicException, Internal Error when called from IIS
body: <p>I am experiencing a problem similar to the one described here:</p>
  
  <p><a href="https://stackoverflow.com/questions/20040425/import-azurepublishsettingsfile-throws-cryptographicexception">Import-AzurePublishSettingsFile throws CryptographicException</a></p>
  
  <p>We are calling Powershell from IIS to programmatically deploy VMs. Part of this process involves loading new Publish Settings Files in for new customers. We are getting the following error when attempting to do so:</p>
  
  <pre><code>Import-AzurePublishSettingsFile : An internal error occurred.
  
  At C:\WebApps\Provisioning\PowerShellScripts\vmDeploy.ps1:152 char:2
  +     Import-AzurePublishSettingsFile ($outputDir + "\" + $azSettingsFile) 
  -ErrorActi ...
  +    
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : CloseError: (:) 
  [Import-AzurePublishSettingsFile], CryptographicException
      + FullyQualifiedErrorId : 
  Microsoft.WindowsAzure.Commands.Subscription.ImportAzurePublishSettingsCommand
  </code></pre>
  
  <p>I have a hunch this has something to do with the user space, as if I run exactly the same script from an interactive PS session, on the same server, it works fine.</p>
  
  <p>Any ideas on how to troubleshoot this?</p>
  
  <p>Edit: Stack trace from Powershell:</p>
  
  <pre><code>PSMessageDetails      : 
  Exception             : System.Security.Cryptography.CryptographicException: An internal error occurred.
  
                             at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
                             at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle&amp; pCertCtx)
                             at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)
                             at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password)
                             at Microsoft.WindowsAzure.Commands.Utilities.Common.PublishSettingsImporter.PublishSubscriptionToAzureSubscription(PublishDataPublishProfile profile, PublishDataPublishProfileSubscription s) in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands.Utilities\Common\PublishSettingsImporter.cs:line 56
                             at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
                             at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
                             at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
                             at Microsoft.WindowsAzure.Commands.Utilities.Common.WindowsAzureProfile.ImportPublishSettings(String fileName) in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands.Utilities\Common\WindowsAzureProfile.cs:line 293
                             at Microsoft.WindowsAzure.Commands.Subscription.ImportAzurePublishSettingsCommand.ImportFile(String fileName) in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands\Subscription\ImportAzurePublishSettings.cs:line 95
                             at Microsoft.WindowsAzure.Commands.Subscription.ImportAzurePublishSettingsCommand.ExecuteCmdlet() in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands\Subscription\ImportAzurePublishSettings.cs:line 46
                             at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletBase.ProcessRecord() in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands.Utilities\Common\CmdletBase.cs:line 96
  TargetObject          : 
  CategoryInfo          : CloseError: (:) [Import-AzurePublishSettingsFile], 
                          CryptographicException
  FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Subscription.ImportAzurePublishSettingsCommand
  ErrorDetails          : 
  InvocationInfo        : System.Management.Automation.InvocationInfo
  ScriptStackTrace      : at &lt;ScriptBlock&gt;, C:\WebApps\Provisioning\PowerShellScripts\vmDeploy.ps1: line 163
  PipelineIterationInfo : {}
  
  
  
  
  
  MyCommand             : Import-AzurePublishSettingsFile
  BoundParameters       : {}
  UnboundArguments      : {}
  ScriptLineNumber      : 163
  OffsetInLine          : 2
  HistoryId             : 1
  ScriptName            : C:\WebApps\Provisioning\PowerShellScripts\vmDeploy.ps1
  Line                  :     Import-AzurePublishSettingsFile ($outputDir + "\" + 
                          $azSettingsFile) -ErrorAction Stop
  
  PositionMessage       : At C:\WebApps\Provisioning\PowerShellScripts\vmDeploy.ps
                          1:163 char:2
                          +     Import-AzurePublishSettingsFile ($outputDir + "\" 
                          + $azSettingsFile) -ErrorActi ...
                          +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  PSScriptRoot          : C:\WebApps\Provisioning\PowerShellScripts
  PSCommandPath         : C:\WebApps\Provisioning\PowerShellScripts\vmDeploy.ps1
  InvocationName        : Import-AzurePublishSettingsFile
  PipelineLength        : 0
  PipelinePosition      : 0
  ExpectingInput        : False
  CommandOrigin         : Internal
  DisplayScriptPosition : 
  
  
  
  00000000000000000000000000000000000000000000000000000000000000000000000000000000
  
  
  Message        : An internal error occurred.
  
  Data           : {}
  InnerException : 
  TargetSite     : Void ThrowCryptographicException(Int32)
  StackTrace     :    at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
                      at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle&amp;pCertCtx)
                      at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)
                      at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password)
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.PublishSettingsImporter.PublishSubscriptionToAzureSubscription(PublishDataPublishProfile profile, PublishDataPublishProfileSubscription s) in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands.Utilities\Common\PublishSettingsImporter.cs:line 56
                      at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
                      at System.Collections.Generic.List`1..ctor(IEnumerable`1collection)
                      at System.Linq.Enumerable.ToList[TSource](IEnumerable`1source)
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.WindowsAzureProfile.ImportPublishSettings(String fileName) in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands.Utilities\Common\WindowsAzureProfile.cs:line 293
                      at Microsoft.WindowsAzure.Commands.Subscription.ImportAzurePublishSettingsCommand.ImportFile(String fileName) in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands\Subscription\ImportAzurePublishSettings.cs:line 95
                      at Microsoft.WindowsAzure.Commands.Subscription.ImportAzurePublishSettingsCommand.ExecuteCmdlet() in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands\Subscription\ImportAzurePublishSettings.cs:line 46
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletBase.ProcessRecord() in c:\workspace\workspace\build-azure-sdk-tools-msi\WindowsAzurePowershell\src\Commands.Utilities\Common\CmdletBase.cs:line 96
  HelpLink       : 
  Source         : mscorlib
  HResult        : -2146893792
  </code></pre>