powershellrarwinrarunrar

PowerShell/Unrar - Use securestring with Unrar Password switch


I have a configuration file with secure strings. These strings are passwords I use for automation.

Typically, I can encrypt my passwords with

Read-Host -AsSecureString | ConvertFrom-SecureString

A prompt comes up for me to type in the password, and it outputs the secure string. I store these secure strings in the configuration file.

I can query these secure strings into a powershell script like so:

SecurePassword = ConvertTo-SecureString $Config.Configuration.password

This works for my WinSCP application, so I do not have credentials stored in plain text within my configuration file.

My problem:

I am trying to do something similar with WinRAR's Unrar utility. The following plain text example works:

$PW = "mypassword"
& $unrar_path x $Source_Path.exe -p"$PW" $Destination_Path\

If I attempt to query the secure string from the configuration file:

$PW = ConvertTo-SecureString $Config.Configuration.ExtractPass
& $unrar_path x $Source_Path.exe -p"$PW" $Destination_Path\

It does not accept the password.

I have tried a lot of different syntax versions attempting to get the secure string to work. Is there another approach to this I am not figuring out?

Any help or suggestions are most welcome. Thank you.


Solution

  • WinRAR won't take a secure string as argument, it has no concept of it, if you start from a secure string and want to pass the plain text password as argument you will need to decrypt it, either with ConvertFrom-SecureString -AsPlainText (powershell 7+) or NetworkCredential (powershell 5.1).

    $secureString = ConvertTo-SecureString $Config.Configuration.ExtractPass
    $plainTextPassw = [System.Net.NetworkCredential]::new('', $secureString).Password
    & $unrar_path x $Source_Path.exe -p $plainTextPassw $Destination_Path\