2

升级到 powershell 3.0 后,现有脚本停止工作并出现错误

ConvertTo-SecureString : The term 'ConvertTo-SecureString' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:1 char:1
+ ConvertTo-SecureString
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (ConvertTo-SecureString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我发现PS 3.0支持ConvertTo-SecureString。我需要以某种方式包含它吗?

4

2 回答 2

1

以下不起作用。

C:\contoso>powershell -command {$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;$secured;}

'ConvertTo-SecureString' is not recognized as an internal or external command,
operable program or batch file.

C:\contoso>

以下确实有效。

C:\contoso>copy con: tfile1.ps1
$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;
$secured;
^Z
        1 file(s) copied.

C:\contoso>powershell -file tfile1.ps1
System.Security.SecureString

C:\contoso> 

这也有效。

C:\contoso>powershell "& {$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;$secured}"
System.Security.SecureString

C:\contoso>

我将把它为什么不能作为 -command 的原因留给其他人,因为我只是一个 powershell 新手。

S。

于 2019-06-07T04:33:25.323 回答
0
Import-Module 'Microsoft.PowerShell.Security'

解决了这个问题。我不知道为什么默认情况下不加载此模块。

于 2013-11-14T07:00:21.370 回答