如果从命令提示符中运行脚本内联,则无法识别Powershell ConvertTo-SecureString。

5

当从cmd.exe运行时

powershell .\scripts\scriptname.ps1

提供了将普通文本转换为安全字符串的方法

[securestring]$secString = ConvertTo-SecureString $SampleString -AsPlainText -Force

I get error:

ConvertTo-SecureString : 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'.

然而,如果我直接在 PowerShell 中打开/运行脚本,它可以正常运行?

如果我尝试手动导入模块,会得到以下错误:

Import-Module : The following error occurred while loading the extended type data file: Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member AuditToString is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member AccessToString is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Sddl is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Access is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Group is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Owner is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Path is already present.

Powershell的版本是

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      22621  963
1个回答

9
这个症状表明你是从PowerShell (Core) 7+中间接启动powershell.exe,Windows PowerShell(powershell.exe)会尝试加载PowerShell(Core)的模块,因为后者定义了$env:PSModulePath变量。根据 GitHub问题#18530 ,解决方法是(暂时)重置PSModulePath环境变量。
set "PSModulePath="
powershell .\scripts\scriptname.ps1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接