测试 Powershell 脚本的正确性
他们无论如何都是为了测试 powershell 脚本的正确性而不执行它。 我知道你可以在 unix 中做类似的事情,但找不到 powershell 的等效项。
本质上我拥有的是一个脚本存储库,其中每个脚本都与规则相关联。 如果规则触发,脚本就会执行,但我需要确保脚本在放入存储库之前是有效的。
谢谢。
你好, 不幸的是,-whatif 命令还不够。 我想从 C# 运行时引擎检查脚本的语法,如果引擎规则触发,该引擎就会触发脚本。 在脚本触发之前,我将规则中的 IList 值写入其中,然后触发它。 但我需要确保它在语法上是正确的,否则我将不得不依赖规则编写者来确保它们是正确的,这是我做不到的,因为很多人都会编写规则,只有一个duff脚本会带来发动机熄火。 我不能这样做,因为该引擎是专为 6 sigma 正常运行时间而设计的企业级产品。
我认为做到这一点的方法是使用这个,它是一个 v2 库。
http://msdn.microsoft .com/en-us/library/system.management.automation.parseexception(VS.85).aspx
我认为有一种方法可以创建一个 ScriptBlock 并在其周围放置一个 try catch 来捕获 ParseException。 有人可以告诉我该怎么做吗? 这正在成为一个真正的挑战。
谢谢。
嘿, 嗯,我和 MS 的一个人(他是 sharepoint pm)交谈过,他又和一个 powershell 的人交谈过,他告诉我,powershell v1 中没有真正的方法可以在不执行的情况下检查脚本的语法。 所以我猜 -whatif 标志是最接近的,但不幸的是它不是静态检查器。 好消息是,在 v2 中,有一种方法可以通过 Automation.PsParser 类的 Tokenize 成员来标记输入流。 这将根据 PS 语法对脚本进行标记,因此如果它失败了,那么它就是一个垃圾脚本。 问题是 v2 还没有发布日期。
感谢帮助。 鲍勃.
Is their anyway to test the correctness of a powershell script without executing it. I know you could do something similar in unix, but can't find the equiv for powershell.
Essentially what I have is a script repository, where each script is tied to rule. If a rule fires, the script executes, but I need to be sure the script is valid, before its put in the repository.
Thanks.
Hi,
Well, Unfortunately the -whatif command is not sufficient. Its the syntax of scripts I want to check from a C# runtime engine, which fires a script if a engine rule fires. Before the script fires, I write into it, IList values from the rule, and then I fire it. But I need to ensure it is syntactically correct, otherwise I'm going to have to rely on the rule writer to ensure they are correct, which I can't do, as many people will wrote rules, only one duff script will bring the engine down. I can't do that as the engine is an enterpise class product designed for six sigma uptime.
I think the way to do it is to use this, which is a v2 lib.
http://msdn.microsoft.com/en-us/library/system.management.automation.parseexception(VS.85).aspx
I think there is way of creating a ScriptBlock and putting a try catch around it to catch the ParseException. Can somebody tell me how to do it. This is becoming a real challenge.
Thanks.
Hey,
Well I spoke to a guy in MS (he's sharepoint pm) and he spoke to one of the powershell guys, and he informed me that there is no real way in powershell v1 to check the syntax of the script without executing. So I guess the -whatif flag is the closest, but it not a static checker unfortumately. Good news is, in v2 there is a way to tokenize the input stream, via the Automation.PsParser class, Tokenize member. This will tokenize a script in accordance with the PS grammes, so if it craps out, its a crap script. Question is v2 doesn't have an release date as yet.
Thanks for help.
Bob.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想要的东西很可能是whatif/confirm。 它是来自 Unix 世界的直接移植。
The thing you want is prolly whatif / confirm. Its direct port from Unix world.
那么,您是在要求功能正确性还是语法正确性?
从功能上讲,您需要创建一个测试用例和环境来进行测试。
从语法上讲,您正在寻找用于 powershell 的静态代码检查器。
So, are you asking for functional correctness or syntactic correctness?
Functionally you'd need to create a test case and environment to test in.
Syntactically you're looking for a static code checker for powershell.
许多 PowerShell 命令提供 -whatif 参数。
Many PowerShell commands offer a -whatif parameter.
我认为你需要量化“有效”对你意味着什么。 这是一个模棱两可的术语,可能意味着很多事情。
如果您指的是 perl 的语法检查之类的内容,则 PowerShell V1 中没有与 PowerShell 脚本完全等效的内容。 我也不确定 V2 中是否有任何功能。
您给出的描述听起来好像您想要的不仅仅是语法/“它将运行”类型检查。 正如 @JoHannes Rossell 所说,这并不是一件小事,而且与您正在做的事情高度相关。
其他答案中前面提到的“whatif/confirm”方法仅适用于 PowerShell cmdlet,不适用于脚本。 它们显示了该 cmdlet 将执行的操作,而不是语法检查。
詹姆士
I think you need to quantify what 'valid' means to you. That is an ambiguous term and can mean alot of things.
If you mean something like perl's syntax check, there is no exact equivalent for a PowerShell script, with PowerShell V1. I'm not sure there is anything in V2 for that either.
The description you give makes it sound like you want something more than just a syntax/'will it run' type check. Like @JoHannes Rossell says, that is not trivial and is highly specific to what you are doing.
The previous mentioned 'whatif/confirm' methods in the other answers only work for PowerShell cmdlets, not scripts. They show what that cmdlet would do, not a syntax check.
james