title: ConvertTo-SecureString is failing in remoting session
body: <p>A function containing a call for <code>ConvertTo-SecureString</code> is failing in a Powershell remoting session when started using <code>Invoke-Command</code>. </p>
  
  <p>I have created a new PSSessionConfiguration with the <code>RunAsVirtualAccount = $true</code> property. This session configuration is loading a module containing the <code>foo</code> function which is making a call to <code>Convert-ToSecureString -AsPlaintext -Force</code>. When trying to run foo remotely using</p>
  
  <pre><code>Invoke-Command -Session $PSSession `
                 -Scriptblock { foo }
  </code></pre>
  
  <p>I am getting an exception:</p>
  
  <blockquote>
    <p>The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread's user context, which may be the case when the thread is impersonating</p>
  </blockquote>
  
  <p>The secure string is subsequently used to construct a <code>[PSCredential]</code> object.</p>
  
  <p>I've seen <a href="https://blog.stangroome.com/2012/05/17/powershell-remoting-user-profiles-delegation-and-dpapi/" rel="nofollow noreferrer">this blog post about ConvertTo-SecureString's dependency on DPAPI</a> and the user profile, which is consistent with the returned exception. But it does not contain any resolution options.</p>