【发布时间】:2019-02-14 06:58:00
【问题描述】:
我有一个powershell script。如果我在 Powershell ISE 中打开此脚本,它运行得非常好,但是,如果我右键单击该文件并单击“使用 powershell 运行”,该脚本会引发错误。
此外,我在之前的帖子中读到以下执行模式为某些人解决了这个问题:
powershell -STA -File script.ps1
在这种情况下,这并没有解决问题,但它确实让我能够阅读错误:
At C:\Users\sancarn\AppData\Local\Temp\script.ps1:20 char:20
+ $parent = [System.Windows.Forms.TreeNode]$global:database.Ite ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.TreeNode].
At C:\Users\sancarn\AppData\Local\Temp\script.ps1:27 char:36
+ ... [void]$node.nodes.add([System.Windows.Forms.TreeNode]::new(" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.TreeNode].
At C:\Users\sancarn\AppData\Local\Temp\script.ps1:33 char:45
+ ... PSCustomObject]IWDBGetChildren([System.Windows.Forms.TreeNode]$node) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.TreeNode].
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound
话虽如此,我不确定我是否真的可以针对此错误做任何事情...我已经加载了System.Windows.Forms 和System.Drawing...有谁知道如何正确执行此文件?
编辑
尝试解决此问题的其他尝试:
powershell -NoExit -STA -File script.ps1
powershell -NoExit -STA -File script.ps1 -Scope Global
编辑 2
我也试过添加:
Add-Type -AssemblyName System.Windows.Forms
到 powershell 脚本的顶部。然而,这个问题仍未解决。
编辑:
不知道为什么这被标记为重复
- 此答案已有推荐答案,并且
- 推荐的答案说明了为什么这是不同的。
...
【问题讨论】:
-
Add-Type -AssemblyName System.Windows.Forms -
嗨彼得,这不是和
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")一样吗? -
另外,我刚刚尝试过,但它根本没有帮助。仍然遇到同样的错误。
标签: powershell