简体   繁体   English

我的 PowerShell 脚本仅在从 ISE 运行时有效

[英]My PowerShell script only works when running from ISE

I can't post all of the script contenet, but the basic idea is that it downloads JSON and converts it to objects using the ConvertFrom-Json cmdlet.我无法发布所有脚本内容,但基本思想是它下载 JSON 并使用ConvertFrom-Json cmdlet 将其转换为对象。 Some objects are filtered out, and the rest are written to an XML/XLS document (in the Excel 2003 format).一些对象被过滤掉,其余的被写入 XML/XLS 文档(Excel 2003 格式)。 This file is then attached to an email and sent to various people.然后将此文件附加到电子邮件并发送给不同的人。

The problem I'm having is that it only works when run from the Powershell ISE.我遇到的问题是它仅在从 Powershell ISE 运行时才有效。 Once I try setting up a scheduled task, calling it from cmd, or even calling it from powershell, the attached file is completely empty.一旦我尝试设置计划任务,从 cmd 调用它,甚至从 powershell 调用它,附加的文件就完全是空的。 It is as if some functions do not run (the one that loops through and creates all rows).就好像某些函数没有运行(循环遍历并创建所有行的函数)。

I can continue to run from ISE for the time being, but the idea of this script is to send out an automatic email that will require no intervention.我暂时可以继续从 ISE 运行,但此脚本的想法是发送一封无需干预的自动电子邮件。 Any ideas as to what could be causing this?关于可能导致这种情况的任何想法?

You need to run the script "dot sourced" which can be done like this您需要运行脚本“dot sourced”,可以像这样完成

powershell.exe -noexit -file c:\test.ps1

or要么

pwershell.exe -noexit ". c:\test.ps1"

See this link under the -File section http://technet.microsoft.com/en-us/library/hh847736.aspx请参阅 -File 部分下的此链接http://technet.microsoft.com/en-us/library/hh847736.aspx

Based on the answer from the comments to the original post, it seems that the script was (unexpectedly?) working in the ISE because of the bug/quirk/feature that allows scripts run in the ISE to be aware of variables used in the console window and vice-versa.根据对原始帖子的评论的回答,似乎脚本(意外?)在 ISE 中工作,因为存在允许在 ISE 中运行的脚本知道控制台中使用的变量的错误/怪癖/功能窗口,反之亦然。

This can often cause logic problems or unexpected results when a script runs with a variable already defined (and not initialized carefully in the script).当脚本使用已定义的变量(并且未在脚本中仔细初始化)运行时,这通常会导致逻辑问题或意外结果。

Ways to avoid this problem:避免这个问题的方法:

  • Try to test your code in as clean an environment as possible.尝试在尽可能干净的环境中测试您的代码。

http://powershell.com/cs/blogs/tips/archive/2015/02/12/getting-a-clean-powershell-environment.aspx http://powershell.com/cs/blogs/tips/archive/2015/02/12/getting-a-clean-powershell-environment.aspx

To make sure a script runs in a completely clean test environment, you could of course restart the PowerShell ISE.为了确保脚本在完全干净的测试环境中运行,您当然可以重新启动 PowerShell ISE。 A more convenient way is to open another PowerShell tab: in the PowerShell ISE, choose File/New PowerShell Tab.更方便的方法是打开另一个 PowerShell 选项卡:在 PowerShell ISE 中,选择 File/New PowerShell Tab。

  • Use Set-StrictMode 2 in your script to catch undefined variables, etc.在脚本中使用Set-StrictMode 2来捕获未定义的变量等。

https://technet.microsoft.com/en-us/library/hh849692.aspx https://technet.microsoft.com/en-us/library/hh849692.aspx

Set-StrictMode -Version 2.0

  • Prohibits references to uninitialized variables (including uninitialized variables in strings).禁止引用未初始化的变量(包括字符串中的未初始化变量)。
  • Prohibits references to non-existent properties of an object.禁止引用对象不存在的属性。
  • Prohibits function calls that use the syntax for calling methods.禁止使用调用方法的语法的函数调用。
  • Prohibits a variable without a name (${}).禁止没有名称的变量 (${})。

I have had this problem be for and for me executing the scrip using single-threaded function from powershell worked.我遇到了这个问题,因为我使用来自 powershell 的单线程函数执行脚本。 You could also try some other options, go to this link to find more info.您还可以尝试其他一些选项,请转到此链接以查找更多信息。

Example例子

powershell.exe -noexit c:\test.ps1 -sta

将缺少的变量或函数更改为全局。

function global:mycall{}

开始你的脚本:

[cmdletbinding()]

暂无
暂无

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

相关问题 PowerShell脚本仅可从PowerShell控制台运行,而不能从ISE运行 - PowerShell Script only works from PowerShell Console and not from ISE Powershell 脚本仅在 ISE 中运行 - Powershell script only running in ISE 从命令行运行时,简单的PowerShell脚本拒绝写入文件,但在ISE中可以正常工作 - Simple PowerShell script refuses to write file when running from command line but works fine in ISE 用于显示通知气球和执行操作的PowerShell脚本仅适用于ISE gui,而不适用于命令行 - PowerShell script to display Notification balloon and take action only works in ISE gui, not from commandline 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 脚本可以在Powershell ISE中工作,但不能在PowerShell控制台中工作吗? - Script Works in Powershell ISE But not In PowerShell Console? Powershell:脚本不是从命令行运行,而是从ISE运行? - Powershell: Script not running from command line but runs from ISE? powershell 脚本从 ISE 运行良好,但从 cli 遇到问题 - powershell script running good from ISE but having problem from cli 从 Powershell ISE 和 Windows 资源管理器运行脚本的不同行为 - Different behaviour running a script from Powershell ISE and Windows Explorer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM