0

I have using a script in Powershell, which make a web request with user and password from an API. Since is a shared computer, for security reason, i want to encrypt the user and password. this is the basic web request i have using (that i obtain from other question in stack overflow ):

$user = 'user'
$pass = 'pass'

$pair = "$($user):$($pass)"

$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

$basicAuthValue = "Basic $encodedCreds"

$Headers = @{
    Authorization = $basicAuthValue
}

Invoke-WebRequest -Uri 'https://whatever' -Headers $Headers

My issue is that i do not found a way to encrypt the user and password variables, try to use the ConvertTo-SecureString and convertTFrom-SecureString, but it does not work. i wanna know if there is a way to save the encrypted credentials in a file or other way. without need to saved as an environment variable and avoid getting credentials, since i wanna automatizes the script as a task.

Please comment any doubt.

Thanks for looking, happy coding.

Regards

Darthgero
  • 15
  • 6
  • Have you tried any of the stuff detailed here: https://purple.telstra.com.au/blog/using-saved-credentials-securely-in-powershell-scripts Or you could try using the secrets management module: https://devblogs.microsoft.com/powershell/secrets-management-module-vault-extensions/ – Kulantan Jun 19 '20 at 04:30

1 Answers1

0

If anyone in the future have the same issue as me, you can use the next tool in powershell, Export-Clixml and Import-Clixml:

$cred = Get-Credential | Export-CliXml -Path ..\cred.ps1.credentials

and then use import to access the credentials $Credential = Import-CliXml -Path "..\cred.ps1.credentials"

now you have to added to the invoke-webrequest

Invoke-WebRequest -Uri 'https://whatever' -credentials $credential

Happy coding!

Darthgero
  • 15
  • 6