简体   繁体   English

脚本在ISE中运行,但不在Powershell中运行

[英]Script runs in ISE but not in Powershell

I have a powershell script . 我有一个powershell脚本 This script runs perfectly fine if I open it in Powershell ISE, however, if I right click on the file and click 'run with powershell' the script throws an error. 如果我在Powershell ISE中打开该脚本,则该脚本运行完全正常,但是,如果右键单击该文件并单击“使用powershell运行”,则该脚本会引发错误。

Furthermore, I read in previous threads that the following execution pattern solved the issue for some people: 此外,我在以前的线程中读到以下执行模式为某些人解决了该问题:

powershell -STA -File script.ps1

In this case this didn't solve the issue, however it did allow me to read the error: 在这种情况下,这不能解决问题,但是确实允许我读取错误:

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

This having been said, I'm not sure I can really do anything about this error specifically... I already load System.Windows.Forms and System.Drawing ... Does anyone have any idea how to execute this file properly? 话虽这么说,我不确定我是否真的可以针对此错误做任何事情...我已经加载了System.Windows.FormsSystem.Drawing ...是否有人知道如何正确执行此文件?

Edit 编辑

Other attempts at trying to fix the issue: 其他尝试解决此问题的尝试:

powershell -NoExit -STA -File script.ps1
powershell -NoExit -STA -File script.ps1 -Scope Global

Edit 2 编辑2

I have also tried adding: 我也尝试添加:

Add-Type -AssemblyName System.Windows.Forms

To the top of the powershell script. 在powershell脚本的顶部。 The issue remains unresolved however. 但是,该问题仍未解决。


Edit: 编辑:

Not sure why this is being flagged as a duplicate after 不知道为什么之后将其标记为重复

  1. this answer already has a recommended answer and 该答案已经有推荐的答案,并且
  2. the recommended answer demonstrates why this is different. 推荐的答案说明了为什么这有所不同。

... ...

As @PetSerAl said in his comment, the answer is in https://stackoverflow.com/a/34637458 正如@PetSerAl在评论中所说,答案在https://stackoverflow.com/a/34637458

Every PowerShell script is completely parsed before the first statement in the script is executed. 在执行脚本中的第一条语句之前,将完全解析每个PowerShell脚本。 An unresolvable type name token inside a class definition is considered a parse error. 类定义中无法解析的类型名称标记被视为解析错误。 To solve your problem, you have to load your types before the class definition is parsed, so the class definition has to be in a separate file. 为了解决您的问题,您必须在解析类定义之前加载类型,因此类定义必须位于单独的文件中。

Ultimately to resolve the issue I need to either load my class definitions as a separate file, or store my declarations in a header file, which calls the class definitions (and the rest of the script). 最终,要解决此问题,我需要将类定义作为单独的文件加载,或将声明存储在头文件中,该头文件调用类定义(以及脚本的其余部分)。

I am amazed this was even an issue but it works now, so that's good... 我很惊讶,这甚至是一个问题,但现在可以使用,所以很好...

Edit 编辑

In my case it is a bit different to the post linked by the solution author. 就我而言,它与解决方案作者所链接的帖子有些不同。 I'm actually building a ruby library for executing Powershell on windows machines. 我实际上是在构建用于在Windows机器上执行Powershell的ruby库。 Currently I don't write data to a file and I can't guarantee that a here-string/invoke-expression will work. 目前,我不将数据写入文件,并且不能保证here-string / invoke-expression可以正常工作。

Invoke-Expression @"
$anotherString=@"
    hello world!
"@    
"@ <--- Powershell will throw an error here!

Instead I've decided to encode the body if a header is present, and then decode and execute at runtime 相反,我决定对标头进行编码(如果存在标头),然后在运行时解码并执行

require 'base64'
if header!=""
    encoded_body = Base64.strict_encode64(body.encode("utf-16le"))
    body = <<-END_TRANSFORMATION
        #{header}

        $encoded_body = "#{encoded_body}"
        Invoke-Expression $([System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($encoded_body)))
    END_TRANSFORMATION
end

So far this has appeared to work in all cases, doesn't need external files, and will work even if users use here-doc strings in their Powershell scripts! 到目前为止,这似乎可以在所有情况下使用,不需要外部文件,即使用户在Powershell脚本中使用here-doc字符串也可以使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell 脚本在 ISE 中运行,但不作为脚本运行 - PowerShell script runs in ISE but not as a script 脚本可以在ISE中正常运行,但不能在Powershell控制台中运行 - Script runs fine in ISE, but not in Powershell Console 脚本使用 ISE 运行,但不通过 PowerShell 控制台运行 - Script runs with ISE but not via PowerShell console 脚本与 ISE 一起运行,但不是 powershell.exe - Script runs with ISE but not powershell.exe Powershell:脚本不是从命令行运行,而是从ISE运行? - Powershell: Script not running from command line but runs from ISE? 在 PowerShell ISE 中作为 PowerShell 脚本的一部分运行的 c# 代码中使用 ReadLine 从用户获取输入 - Get input from the user using ReadLine in c# code that runs as part of PowerShell script in PowerShell ISE 无论脚本在“常规” PowerShell控制台还是在Powershell ISE中运行,如何以相同的方式处理错误? - How to handle errors in a way that works the same whether the script runs in “regular” powershell console or in powershell ISE? Powershell 脚本仅在 ISE 中运行 - Powershell script only running in ISE Powershell脚本可在ISE中使用,但不能在控制台中使用 - Powershell script works in ISE but not in console Powershell脚本可在ISE中使用,但不能在Power With Powershell中运行 - Powershell script works in ISE but not in Run With Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM