Package to exe fails when calling ConvertTo-SecureString

Visual Studio Code
Version: 1.73.1

I have run into an issue when packaging an executable. ConvertTo-SecureString is failing.
This was working fine before but sometime in the last few weeks it stopped.
Note: In that time PowerShell was upgraded to 7.3.

Have a simple script:
$PSVersionTable.PSVersion
$pw = “01000000d08c9ddf0115d1118c7a00c04fc297eb01000000e6a902…”
$ss = ConvertTo-SecureString $pw
Write-Host “Press any key to close window.”
[void][System.Console]::ReadKey($true)

Running executable:
Major Minor Patch PreReleaseLabel BuildLabel


7 2 0
ConvertTo-SecureString:
Line |
10 | $ss = ConvertTo-SecureString $pw
| ~~~~~~~~~~~~~~~~~~~~~~
| The ‘ConvertTo-SecureString’ command was found in the module ‘Microsoft.PowerShell.Security’, but the module could not be loaded. For more information, run ‘Import-Module Microsoft.PowerShell.Security’.
Press any key to close window.

My package.psd1 file:
@{
Root = ‘C:_PS\test\test_token.ps1’
OutputPath = 'C:_PS\test'
Package = @{
Enabled = $true
DotNetVersion = ‘net6.0’
PowerShellVersion = “7.2.0”
PackageType = ‘Console’
}
Bundle = @{
Enabled = $true
Modules = $true
}
}

I tried using:
Import-Module Microsoft.PowerShell.Security
But I get:
Error in TypeData “System.Security.AccessControl.ObjectSecurity”: The member AuditToString is already present.
And still get the ConvertTo-SecureString error.

Any ideas?
thanks,

Are you running this on a machine that has PS7.3 installed? I wonder if there is a module version mismatch.

Yes running 7.3.0. Was installed a week or two ago. Pretty sure this is when the issue happened.

In fact Microsoft.PowerShell.Security is called out in What’s New under Breaking Changes and Improvements.

We should be able to add support for 7.3 packaging pretty easily now that .NET 7 is GA’d. That might be the easiest solution.

Perfect.