简体   繁体   English

Powershell无法从CMD(批处理)脚本执行

[英]Powershell not executing from CMD (Batch) script

There are quite some questions asked already regarding this issue, but none of the answers resolved mine. 关于此问题,已经有很多问题要问,但是我的答案都没有解决。

I wrote following batch script: 我写了以下批处理脚本:

powershell.exe -executionpolicy bypass  -file "./myps.ps1"

I saved the file as autorun.cmd 我将文件另存为autorun.cmd

Whenever I double-click the cmd file, nothing happens, CLI quickly shows up and closes but powershell was not executed. 每当我双击cmd文件时,都不会发生任何事情,CLI会快速显示并关闭,但未执行powershell。

If I run the same command directly in CLI, it works. 如果我直接在CLI中运行相同的命令,它将起作用。

I am really confused on what's going on here. 我真的对这里发生的事情感到困惑。 Any help would be appreciated. 任何帮助,将不胜感激。

Saving it as a .bat extension will fix it. 将其另存为.bat扩展名将对其进行修复。 Like you, I was never able to get a .cmd file to work as a batch file as well. 像您一样,我也永远无法获得.cmd文件作为批处理文件。

So, hi and welcome to this platform... I try run this powershel code above, that convert ansi to unicode file, by bat file using your command inside the bat: 因此,您好,欢迎来到这个平台...我尝试运行上面的powershel代码,使用bat内的命令通过bat文件将ansi转换为unicode文件:

Set-Location ".\"
Get-Content 'ansi.txt' | Set-Content -Encoding unicode 'outunicode.txt'

And all running fine, my bat call to execute .ps1 code (copied from you to test propose), above the code inside bat file, like your code: 一切运行正常,我的bat调用执行bat文件中代码上方的.ps1代码(从您复制以测试建议),就像您的代码一样:

powershell.exe -executionpolicy bypass -file "./myps.ps1"

All run OK, no error, and the result file OK too! 全部运行正常,没有错误,结果文件也正常!

So, this make me suggest to you, that maybe you need try running the bat file direct in your command line for to be able to see possible error message, and filling if this is a bat/powershell/security/other error, and, if possible, post/share the content .ps1 file, I'm 100% sure, that here are someone (a lot of them) will be capable help you precisely and quickly. 因此,这使我建议您,也许您需要尝试直接在命令行中运行bat文件,以便能够看到可能的错误消息,并在出现bat / powershell / security / other错误的情况下进行填充,并且,如果可能,请发布/共享内容.ps1文件,我100%确信,这里有人(很多人)将能够准确,快速地为您提供帮助。 Sorry my English. 对不起,我的英语。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM