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

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

问题描述

如何在Windows批处理脚本的同一文件中嵌入PowerShell脚本?

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

我知道这种情况在其他情况下也是可能的:

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

  • 使用sqlcmd将SQL嵌入批处理脚本中,并在文件的开头巧妙地排列goto和注释
  • 在* nix环境中,您希望运行脚本的程序的名称在脚本的第一行被注释掉,例如,#!/usr/local/bin/python.
  • Embedding SQL in a 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.

可能没有做到这一点的方法-在这种情况下,我将不得不从启动脚本中调用单独的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 script.

我考虑过的一种可能的解决方案是回显PowerShell脚本,然后运行它. 执行此操作的一个很好的理由是,尝试执行此操作的部分原因是要利用PowerShell环境的优势,而不必担心例如

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, 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:

  • 行尾的克拉^是连续的-就像Visual Basic中的下划线一样
  • 与号&通常用于分隔命令echo Hello & echo World会在单独的行上产生两次回波
  • %0将为您提供当前正在运行的脚本
  • A carat ^ at the end of a line is a continuation - like an underscore in Visual Basic
  • An ampersand & 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 console file "insideout.bat" extension is not psc1. Windows PowerShell console file extension must be psc1.
  • CMD也不完全满意这种情况-尽管它确实偶然发现了'#', it is not recognized as an internal or external command, operable program or batch file.
  • 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.
  • CMD 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" 

正则表达式排除以@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

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

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