Start a new topic

PrivateKeyContent usage with PowerShell

i have keys working via PrivateKeyPath and am now trying to implement keyfiles as content.


https://docs.royalapps.com/r2023/scripting/objects/organization/royalcredential.html#privatekeypath

> PrivateKeyContent
>
> Type: byte[], Default Value: new byte[] { }
> Assign a key file to the credential by embedding the key file's content.

now i need a PowerShell code example on how to use the content of one of the cert files (that i am using successully via PrivateKeyPath).


Hi Falk!


I responded on Mastodon already but maybe it got lost somewhere. Can you check if this helps you to set the byte array:
https://stackoverflow.com/questions/10672092/read-parse-binary-files-with-powershell


Regards,
Stefan

 my answers here from mastodon via auto translate :

"@royalapps@dotnet.socialthat's unfortunately not what I'm looking for. I have my problem here: https://docs.royalapps.com/r2023/scripting/objects/organization/royalcredential.html:

"PrivateKeyContent
Type: byte[], Default Value: new byte[] { }
Assign a key file to the credential by embedding the key file's content."

I can't do anything with that part of the documentation, unfortunately. How do I get a format like that - it's surely been tested?"


"It works via PrivateKeyPath with a path to exactly the key files that I want to use via PrivateKeyContent.
I can leave it like that, but if you can tell me how to do it via content, I would prefer that :)"


so. no. the link provided did not have a solution for.
i would appreciate if you could provide me with an example

"now i need a PowerShell code example on how to use the content of one of the cert files (that i am using successully via PrivateKeyPath)."

that way i could figure out the rest. i would also provide the soultion (my code) here :)

Sure, here's a simple example:


Install-Module RoyalDocument.PowerShell
Import-Module RoyalDocument.PowerShell
$RoyalStore = New-RoyalStore -UserName ($env:USERDOMAIN + '\' + $env:USERNAME)
$RoyalDocumentPath = Join-Path -Path $env:USERPROFILE -ChildPath ('Documents\' + $env:USERDOMAIN + '.rtsz')
$RoyalDocument = New-RoyalDocument -Name $env:USERDOMAIN -FileName $RoyalDocumentPath -Store $RoyalStore

$Credential = New-RoyalObject -Folder $RoyalDocument -Type RoyalCredential -Name "Credential with KeyFile"

Set-RoyalObjectValue -Object $Credential -Property UserName -Value "user1" | Out-Null

# set to embedded private key
Set-RoyalObjectValue -Object $Credential -Property PrivateKeyMode -Value 1 | Out-Null

# read key content as shown here: https://stackoverflow.com/questions/10672092/read-parse-binary-files-with-powershell
$PrivateKeyPath = Join-Path -Path $env:USERPROFILE -ChildPath ('Documents\test.pri')
$Bytes = [System.IO.File]::ReadAllBytes($PrivateKeyPath)

# set the private key content
Set-RoyalObjectValue -Object $Credential -Property PrivateKeyContent -Value $Bytes | Out-Null

# depending on how your key is set up you may also need to set the Passphrase
Set-RoyalObjectValue -Object $Credential -Property Passphrase -Value 'secret passphrase here' | Out-Null

Out-RoyalDocument -Document $RoyalDocument
Close-RoyalDocument -Document $RoyalDocument



I hope this helps.


Regards,
Stefan

Perfect, works great! Thanks!

You're welcome! Glad I could help!

Login or Signup to post a comment