【问题标题】:Add-MpPreference is not recognized as a nameAdd-MpPreference 未被识别为名称
【发布时间】:2020-08-21 04:22:47
【问题描述】:

你好,我正在做一个小脚本,它将使用Add-MpPreference 运行一个小型 powershell。就我而言,我这样做是因为有时我想在我的 Windows 10 中包含一些路径,但我必须手动完成。所以我试图让它自动化。我这样做了:

ShellExecute("powershell", '-noexit Add-MpPreference -ExclusionPath C:/')

错误返回是我:

The term 'Add-MpPreference' is not recognized as the name of a cmdlet, function
, script file, or operable program. Check the spelling of the name, or if a pat
h was included, verify that the path is correct and try again.
At line:1 char:17
+ Add-MpPreference <<<<  -ExclusionPath C:/
    + CategoryInfo          : ObjectNotFound: (Add-MpPreference:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

这个错误是什么意思?还是可以修?

【问题讨论】:

标签: powershell


【解决方案1】:

我遇到了类似的问题。我发现在 64 位操作系统上以 32 位模式运行 Powershell 时,找不到 Add-MpPreference 命令。

您可以通过在 32 位模式下打开 Powershell 控制台并调用此函数来轻松地重新创建它。它会给出同样的错误。

出现此问题的原因是我从以 32 位运行的 C# 代码运行 Powershell 脚本。这导致被调用的powershell.exe也是32位版本。

我调整代码调用64位版本,然后找到命令。

为了做到这一点,让它从这里 C:\Windows\SysNative\WindowsPowerShell\v1.0\powershell.exe 运行 Powershell

有关如何设置powershell版本的更多信息,请访问https://web.archive.org/web/20180314210451/http://www.madwithpowershell.com/2015/06/64-bit-vs-32-bit-powershell.html

【讨论】:

  • 我遇到了同样的问题,并且您的解决方案有效,我只是想补充一点,显然并非所有 Windows 10 版本都受到影响:我可以重现构建 1607 中的行为,但不能重现 1809 中的行为,所以它似乎微软已经在较新的版本中修复了这个问题。有人可以证实这一观察吗?
猜你喜欢
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多