2016-06-21 111 views
0

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

例如行:

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

返回此在PrimalScript:

ERROR: Get-CimInstance : The term 'Get-CimInstance' 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 C:\Users\sieredzkian\Desktop\New folder\User_Launched_Application_3.ps1:166 char:31 + $freeRam = Get-CimInstance <<<< -ClassName win32_operatingsystem | Select-Object -expand FreePhysicalMemory + CategoryInfo : ObjectNotFound: (Get-CimInstance:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

而在它运行PowerShell中......

为何不能找到的cmdlet?它也为其他一些小命令,如Out-GridView

编辑: 我的原始问题是由于使用primalscript 2011,其中只支持powershell V2引起的。我可以通过安装primalscript 2016的试用版来解决这个问题。这使我能够像在ISE中使用的那样拥有PowerShell V3。

+0

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

回答

1

Get-CimInstance要求的PowerShell版本3和更高......请务必将其设置在PowerShell的版本菜单

Serach for this menu

另一个选项(如果你的PowerShell版本号为2或更少)是使用Get-WmiObject而不是Get-CimInstance所以它应该看起来像这样:

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

由于某些原因,我无法在UI上选择v3 – twantheswan

+0

因此,如果不晚于v3,请将脚本更改为Get-WmiObject,如上所述。 – Avshalom

+0

我最终能够清除缓存并重新启动程序。 v3按钮变得可点击。谢谢你的帮助! – twantheswan