简体   繁体   English

使用 Powershell 运行 a.bat 脚本时避免“不被识别为内部或外部命令”

[英]Avoiding “is not recognized as an internal or external command” when running a .bat script with Powershell

I wrote a basic.bat script that you can find below:我写了一个 basic.bat 脚本,你可以在下面找到:

@echo off
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
    param([string]$zipfile, [string]$outpath)
    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
$import="E:\directory\import"
cd $import
Unzip $import\file1.zip $import\extract\
Unzip $import\file2.zip $import\extract\
Unzip $import\file3.zip $import\extract\

When I run these command lines directly into the PowerShell terminal (by copy-paste), it's working fine!当我将这些命令行直接运行到 PowerShell 终端(通过复制粘贴)时,它工作正常!

But, when I save all these command lines into a "test.bat" and - then - run this.bat file, it's not working:但是,当我将所有这些命令行保存到“test.bat”中并 - 然后 - 运行 this.bat 文件时,它不起作用:

错误消息

Sorry, errors messages are in French, but it means "is not recognized as an internal or external command, executable program or command file" for each command line.抱歉,错误消息是法语的,但它表示每个命令行的“不被识别为内部或外部命令、可执行程序或命令文件”。

I've tried to run the.bat file from different ways:我尝试从不同的方式运行 .bat 文件:

  1. .\test.bat directly into the directory from PowerShell terminal .\test.bat直接从PowerShell终端进入目录

  2. [FULL_PATH]\test.bat

  3. powershell E:\directory\import\test.bat from the CMD app powershell E:\directory\import\test.bat来自 CMD 应用程序

  4. C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe E:\directory\import\test.bat from the CMD app C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe E:\directory\import\test.bat来自 CMD 应用程序

  5. Etc.等等。

And I still get the same error messages.而且我仍然收到相同的错误消息。

You can find below my Path configuration:您可以在下面找到我的路径配置:

路径配置

You are mixing batch scripting and PowerShell Scripting.您正在混合批处理脚本和 PowerShell 脚本。 You need to save your script as a .ps1 file instead of a .bat and remove any cmd only commands.您需要将脚本保存为.ps1文件而不是.bat文件,并删除所有 cmd 专用命令。

test.ps1测试.ps1

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
    param([string]$zipfile, [string]$outpath)
    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
$import="E:\directory\import"
cd $import
Unzip $import\file1.zip $import\extract\
Unzip $import\file2.zip $import\extract\
Unzip $import\file3.zip $import\extract\

The reason it works when copy pasting is because each command is interpreted independently and therefore can use different interpreters.它在复制粘贴时起作用的原因是因为每个命令都是独立解释的,因此可以使用不同的解释器。 However, this is not the case in a script in which the terminal must decide which interpreter to use.但是,在终端必须决定使用哪个解释器的脚本中,情况并非如此。 Based on the file extension, it chooses cmd.根据文件扩展名,它选择 cmd。 This only allows the @echo off to work and everything else will fail.这只允许@echo off工作,其他一切都会失败。

暂无
暂无

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

相关问题 '#!' 执行 awk 脚本时未被识别为内部或外部命令 - '#!' is not recognized as an internal or external command when executing awk script $不被识别为内部或外部命令 - $ is not recognized as an internal or external command '$'不被识别为内部或外部命令 - '$' is not recognized as an internal or external command '和'不被视为内部或外部命令 - 'and' is not recognized as an internal or external command Powershell' 不是内部或外部命令,也不是可运行的程序或批处理文件 - Powershell' is not recognized as an internal or external command, operable program or batch file Windows上的脚本/生成:'script'不被识别为内部或外部命令 - script/generate on Windows: 'script' is not recognized as an internal or external command / t不被识别为内部或外部命令。 C#批处理脚本 - /t is not recognized as an internal or external command. C# batch script 无法从另一个内部调用 .bat 文件 - “未识别为内部或外部命令”错误 - can't call .bat file from within another - “not recognized as an internal or external command” error bat文件令牌或for循环出错:未被识别为内部或外部命令,可操作程序或批处理文件 - Error with bat file token or for loop: not recognized as an internal or external command, operable program or batch file XCOPY无法识别为内部或外部命令 - XCOPY not recognized as internal or external command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM