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.

Greetings,
I noticed that documentation shows that PowerShell 7.3.x with net7.0 is supported for Packaging.

I tried it and get:
Merge-Script: Invalid .NET Versions specified for selected PowerShell version. Valid values are: netcoreapp31, net50, net60

I am running on PS 7.3.0.
My package.psd1 file cpntains:
DotNetVersion = ‘net7.0’
PowerShellVersion = “7.3.0”

Any suggestions?
Thanks

Any updates on this?
I have the same issue. (twice the security string and the merge-script error when using PW 7.3.x and net7.0)

Nope, I still get the same thing. I simply reverted to using PS 5 for now. I will ultimately need to follow up.

Thanks. In fact this is the only piece of code which doesnt work for me.

Same here. It works really well overall.

I have found the solution, thanks to a github article:

Then env trick is not yet working for me, but what I used instead is:

Import-Module -Name “C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Security\Microsoft.PowerShell.Security.psd1” -Verbose -Force

(-Verbose -Force just to see extra output an make things a bit easier to read)

I need to so some smart detection about 32 or 64 bit version is running and adjust the path above. But my first test show the is doing the job.

That is very cool, thanks for sharing. I’ll try it when I can. This won’t however fix the merge-script problem, have you tried?

No but that is a Pro tools thing I guess. They dont support yet the combination of PW 7.3.x and .Net7