【问题标题】:Powershell ConvertTo-SecureString ObjectNotFoundPowershell ConvertTo-SecureString ObjectNotFound
【发布时间】:2019-06-07 12:33:25
【问题描述】:

升级到 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 是 supported。我需要以某种方式包含它吗?

【问题讨论】:

  • all 脚本是否停止工作?还是只包含 ContertTo-SecureString 的脚本?
  • 在我的 3.0 上,相应的 DLL 位于此处C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Security\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Security.dll。您可能会检查该 dll 是否在该路径中。你还应该有这个目录:C:\Windows\System32\WindowsPowerShell\v1.0\modules\Microsoft.PowerShell.Security
  • 顺便说一句,您使用的是哪个操作系统?您是否安装了任何服务器产品,例如 Exchange、SharePoint Server、SCCM 等?

标签: windows powershell powershell-3.0


【解决方案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.

【讨论】:

  • 你的回答和上面的问题无关,但是我来这里是因为我觉得我和你有同样的问题(从cmd或PS用ConvertTo-SecureString调用PS)。您的解决方法不是基于您使用其他变量的事实,而是基于您调用命令更深一层的事实。澄清一下,这也可以:powershell "& {ConvertTo-SecureString -AsPlainText 'random text' -Force}"
【解决方案2】:
Import-Module 'Microsoft.PowerShell.Security'

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

【讨论】:

  • 该导入执行时未显示错误,但没有为我解决问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2011-07-15
  • 2017-10-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多