title: How to write a secret to azure key vault from Azure DevOps pipeline?
body: <p>I have created a Key Vault in Azure DevOps using ARM and I now want to write a secret to it within the same pipeline.</p>
  
  <p>I created an Azure PowerShell task with the following inline PowerShell:</p>
  
  <pre><code>$secretvalue = ConvertTo-SecureString $(Secret) -AsPlainText -Force
  Set-AzureKeyVaultSecret -VaultName $(VaultName) -Name $(SecretName) -SecretValue (ConvertTo-SecureString $(Secret) -AsPlainText -Force)
  </code></pre>
  
  <p>The key vault gets created fine, but I get the following error when trying to write the secret:</p>
  
  <p><code>[error]Access denied</code></p>
  
  <p>If I run <code>Set-AzureKeyVaultSecret</code> on my local machine it works fine.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here are some screenshots of what I've done. In Azure DevOps I created a Service Connection and verified it:</p>
  
  <p><a href="https://i.stack.imgur.com/HdYi3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HdYi3.png" alt="enter image description here"></a></p>
  
  <p>Here's what my Azure PowerShell task looks like to write the secret to the vault:</p>
  
  <p><a href="https://i.stack.imgur.com/ofrzi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ofrzi.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/vu4Tn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vu4Tn.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/FEdLb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FEdLb.png" alt="enter image description here"></a></p>
  
  <p>Key Vault permissions:</p>
  
  <p><a href="https://i.stack.imgur.com/Klmoy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Klmoy.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/rvxmU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rvxmU.png" alt="enter image description here"></a></p>
  
  <p>Do I need a separate Service Principal or do I need to create an App Registration?</p>