如何运行DOS批处理文件中的PowerShell脚本 [英] How to run a PowerShell script within a DOS batch file

查看:356
本文介绍了如何运行DOS批处理文件中的PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何嵌入同一个文件作为DOS批处理脚本中的PowerShell脚本?

How do I have a PowerShell script embedded within the same file as a DOS batch script?

我知道这种事情是可能的其他情况:

I know this kind of thing is possible in other scenarios:


  • 使用DOS批处理脚本嵌入SQL SQLCMD 和GOTO的和评论在文件的开头巧安排

  • 在有您希望与对剧本的第一行注释掉运行脚本,例如,#程序名的* nix的环境!在/ usr / local / bin目录/蟒蛇

  • Embedding SQL in a DOS batch script using sqlcmd and a clever arrangements of goto's and comments at the beginning of the file
  • In a *nix environment having the name of the program you wish to run the script with on the first line of the script commented out, for example, #!/usr/local/bin/python.

有可能不是一个办法做到这一点 - 在这种情况下,我将不得不从DOS启动脚本中调用单独的PowerShell脚本

There may not be a way to do this - in which case I will have to call the separate PowerShell script from the launching DOS script.

一个可行的办法,我认为是呼应了PowerShell脚本,然后运行它。一个很好的理由的的做到这一点的原因,部分尝试这是要使用PowerShell的环境的优势,并且没有痛苦,例如,的 DOS转义字符

One possible solution I've considered is to echo out the PowerShell script, and then run it. A good reason to not do this is that part of the reason to attempt this is to be using the advantages of the PowerShell environment without the pain of, for example, DOS escape characters

我有一些不寻常的约束,想找到一个优雅的解决方案。我怀疑这个问题可能被引诱的各种反应:你为什么不尝试,而不是解决这个不同的问题。我只想说,这些都是我的限制,我们对此深感抱歉。

I have some unusual constraints and would like to find an elegant solution. I suspect this question may be baiting responses of the variety: "Why don't you try and solve this different problem instead." Suffice to say these are my constraints, sorry about that.

任何想法?是否有聪明的评论的合适的组合和转义字符,使我实现这一目标?

Any ideas? Is there a suitable combination of clever comments and escape characters that will enable me to achieve this?

这是如何实现这一目标的几点思考:

Some thoughts on how to achieve this:


  • 克拉 ^ 在DOS一行的末尾是一个延续 - 就像在Visual Basic中的下划线

  • 一个符号&安培; 在DOS下通常被用于单独的命令呼应你好&安培;回声世界导致在不同的线相互回声

  • %0会给你当前正在运行脚本

  • A carat ^ at the end of a line in DOS is a continuation - like an underscore in Visual Basic
  • An ampersand & in DOS typically is used to separate commands echo Hello & echo World results in two echos on separate lines
  • %0 will give you the script that's currently running

因此​​,像这样(如果我可以使它工作)将是很好的:

So something like this (if I could make it work) would be good:

# & call powershell -psconsolefile %0
# & goto :EOF
/* From here on in we're running nice juicy powershell code */
Write-Output "Hello World"

除...


  • 它不工作...因为

  • 文件的扩展名不是按PowerShell中的喜好: Windows PowerShell控制台文件insideout.bat扩展没有PSC1。 Windows PowerShell控制台文件的扩展名必须是PSC1。

  • DOS是不是真的完全的情况或者高兴 - 虽然它在'#'绊倒,它不被识别为一个内部或外部命令,可操作的程序或批处理文件

  • It doesn't work... because
  • the extension of the file isn't as per PowerShell's liking: Windows PowerShell console file "insideout.bat" extension is not psc1. Windows PowerShell console file extension must be psc1.
  • DOS isn't really altogether happy with the situation either - although it does stumble on '#', it is not recognized as an internal or external command, operable program or batch file.

推荐答案

这其中只有通过正确的行PowerShell的:

This one only passes the right lines to PowerShell:

dosps2.cmd

@findstr/v "^@f.*&" "%~f0"|powershell -&goto:eof
Write-Output "Hello World" 
Write-Output "Hello some@com & again" 

前pression 排除开始 @行˚F键,包括&放大器; 并通过其他一切PowerShell的

The regular expression excludes the lines starting with @f and including an & and passes everything else to PowerShell.

C:\tmp>dosps2
Hello World
Hello some@com & again

这篇关于如何运行DOS批处理文件中的PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆