Powershell ConvertTo-SecureString ObjectNotFound

岁酱吖の 提交于 2021-02-19 05:42:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!