【问题标题】:Primalscript/Primalforms not recognizing powershell cmdletsPrimalscript/Primalforms 无法识别 powershell cmdlet
【发布时间】:2016-10-22 16:26:23
【问题描述】:

我有一个在 powershell ISE 中运行和编译的 powershell 脚本。但是,我需要将其压缩为 .exe,以便使用我的应用程序的用户可以轻松打开它。为此,我发现我应该使用 primalcript 或 primalforms。问题是当我尝试运行脚本时,某些 cmdlet 会在这两个程序中的任何一个上输出错误消息。

例如这一行:

$freeRam = Get-CimInstance -ClassName win32_operatingsystem | 
Select-Object -expand FreePhysicalMemory

在 PrimalScript 中返回:

错误:Get-CimInstance:无法识别术语“Get-CimInstance” 作为 cmdlet、函数、脚本文件或可运行程序的名称。 检查名称的拼写,或者如果包含路径,请验证 路径正确,然后在 C:\Users\sieredzkian\Desktop\New 重试 文件夹\User_Launched_Application_3.ps1:166 char:31 + $freeRam = 获取 CimInstance

而在 powershell 中它运行...

为什么找不到 cmdlet?它也为其他一些 cmdlet 执行此操作,例如 Out-GridView

编辑: 我最初的问题是由于使用了仅支持 powershell V2 的 primalscript 2011 引起的。我能够通过安装 primalscript 2016 的试用版来解决此问题。这使我能够像在 ISE 中使用的那样拥有 powershell V3。

【问题讨论】:

  • 您是否设置了 powershell 版本首选项? Primalscript (2016) 应该将代码编译到 Powershell v5

标签: powershell


【解决方案1】:

Get-CimInstance 需要 PowerShell 版本 3 及更高版本...确保在 PowerShell 版本菜单中设置它

另一种选择(如果您的 PowerShell 版本为 2 或更低)是使用 Get-WmiObject 而不是 Get-CimInstance,因此它应该如下所示:

$freeRam = Get-WmiObject -ClassName win32_operatingsystem | Select-Object -expand FreePhysicalMemory 

【讨论】:

  • 由于某种原因我无法在 UI 上选择 v3
  • 因此,如果不晚于 v3,请按照说明将脚本更改为 Get-WmiObject。
  • 我最终能够清除缓存并重新启动程序。 v3 按钮变得可点击。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 2018-10-10
  • 2014-07-08
  • 2014-01-08
  • 2012-09-13
相关资源
最近更新 更多