MY PS Script:
$admin = "TEST"
$Password = ConvertTo-SecureString "Test12345" -AsPlainText -Force
$Credential = [PSCredential]::new( $admin, $Password)
if ($Credential -is [System.Management.Automation.PSCredential]) {
return "PS Credentials - true"
} else {
return "PS Credentials - false"
}
RESULT in PS ISE:
enter image description here
MY NODE JS Script:
"use strict";
const { exec } = require("child_process");
exec(
"powershell.exe -File C:\Users\fschiller\Desktop\Projekt_Node\Libary\TEST.ps1",
(error, stdout, stderr) => {
console.log(stdout)
}
)
RESULT in VSCode from Node Script:
enter image description here
I Dont know how to Fix it. Can Someone Help ? or know the Problem and how to work around it?
tried ExecFile,
other ways to creat PSCredential Object,
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
2
Answers
It works if I specify the module to the full path - Import-Module -Name “C:WindowsSystem32WindowsPowerShellv1.0ModulesMicrosoft.PowerShell.SecurityMicrosoft.PowerShell.Security.psd1"
The problem isn’t specific to any particular PowerShell host program (console vs. ISE vs. integrated terminal in Visual Studio Code), it comes down to indirectly launching
powershell.exe
, the Windows PowerShell CLI from PowerShell (Core) 7+:This cause Windows PowerShell to inherit an inappropriate
PSModulePath
environment-variable value ($env:PSModulePath
), with entries that pertain to PowerShell 7+ only, which results in attempts to load incompatible modules – which is what you saw:It is only if you call
powershell.exe
directly from PowerShell 7+ that the latter anticipates the problem and itself applies the solution described below.Specifically, the symptoms with respect to the
Microsoft.PowerShell.Security
module that contains theConvertTo-SecureString
cmdlet in your case are:The first time you call
ConvertTo-SecureString
in a given session, its module is auto-loaded – but because an attempt is made to load the incompatible PowerShell 7+ version of the module, auto-loading fails with the following error:Running
Import-Module Microsoft.PowerShell.Security
then yields the following error – its technical details aren’t important, and unfortunately, it doesn’t provide much of a clue as to what the problem is:The solution, per a comment from a PowerShell team member in GitHub issue #18530, is to undefine the
PSModulePath
environment variable before indirectly callingpowershell.exe
:Note:
delete
, as shown below, must be used to undefine (remove) a variable fromprocess.env
– assigning''
(ornull
orundefined
) toprocess.env.PSModulePath
does not work.So as not to modify the caller’s environment, a copy of the
process.env
is created, from which the variable is then removed, and passed via theenv:
property of an object as theoptions
argument tochild_process.exec()
A simplified call to
powershell.exe
is used below, which simply prints the value of$env:PSModulePath
as seen by the child process, which should then contain only the usual, Windows PowerShell-only entries.