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

查看:76
本文介绍了如何在 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 控制台文件insideout.bat"扩展名不是 psc1.Windows PowerShell 控制台文件扩展名必须是 psc1.
  • CMD 对这种情况也不太满意 - 尽管它确实遇到了 '#',但它不被识别为内部或外部命令、可运行的程序或批处理文件.莉>

推荐答案

这个只将正确的行传递给 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:	mp>dosps2
Hello World
Hello some@com & again

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

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