问题
After upgrading to powershell 3.0 existing scripts stopped working with an error
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
I found that ConvertTo-SecureString is supported on PS 3.0. Do I need to include it somehow?
回答1:
The following does NOT work.
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>
The following does work.
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>
This also works.
C:\contoso>powershell "& {$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;$secured}"
System.Security.SecureString
C:\contoso>
I'll leave why it doesn't work as a -command to someone else as I'm only a powershell novice.
S.
回答2:
Import-Module 'Microsoft.PowerShell.Security'
fixes the issue. I don't know why this module is not loaded by default.
来源:https://stackoverflow.com/questions/19957340/powershell-convertto-securestring-objectnotfound