如何从Windows Powershell执行脚本?

时间:2023-01-12 19:43:50

I created a small aws.bat script and then in PowerShell I navigated to the script directory.

我创建了一个小的aws.bat脚本,然后在PowerShell中导航到脚本目录。

But when I type aws then I get an error message and it does not execute the script. Is there a special thing I should be doing?

但是当我键入aws然后我收到一条错误消息并且它不执行脚本。我应该做一件特别的事吗?

Here's what I have tried:

这是我尝试过的:

PS C:\G> .\aws.bat

C:\G>$BucketName = "ab-user"
'$BucketName' is not recognized as an internal or external command,
operable program or batch file.

The very first and every othre command has a similar message.

第一个和每个othre命令都有类似的消息。

1 个解决方案

#1


You can't just enter the name on its own, with or without file extension, you first have to tell powershell what to do with it.

您不能单独输入名称,无论是否有文件扩展名,您首先必须告诉powershell如何处理它。

You can call it as follows:

您可以按如下方式调用它:

& .\aws.bat

If you need the LastExitCode returned in powershell to check if the bat file worked (0 usually means it worked, anything else usually means it didn't), use Start-Process as follows:

如果你需要powershell中返回的LastExitCode来检查bat文件是否有效(0通常意味着它工作,其他任何东西通常意味着它没有),使用Start-Process,如下所示:

$batch = Start-Process -FilePath .\aws.bat -Wait -passthru;$batch.ExitCode

And if you want the batch file to be hidden when it is run by powershell do this:

如果您希望在PowerShell运行时隐藏批处理文件,请执行以下操作:

$batch = Start-Process -FilePath .\aws.bat -Wait -passthru -WindowStyle Hidden;$batch.ExitCode

#1


You can't just enter the name on its own, with or without file extension, you first have to tell powershell what to do with it.

您不能单独输入名称,无论是否有文件扩展名,您首先必须告诉powershell如何处理它。

You can call it as follows:

您可以按如下方式调用它:

& .\aws.bat

If you need the LastExitCode returned in powershell to check if the bat file worked (0 usually means it worked, anything else usually means it didn't), use Start-Process as follows:

如果你需要powershell中返回的LastExitCode来检查bat文件是否有效(0通常意味着它工作,其他任何东西通常意味着它没有),使用Start-Process,如下所示:

$batch = Start-Process -FilePath .\aws.bat -Wait -passthru;$batch.ExitCode

And if you want the batch file to be hidden when it is run by powershell do this:

如果您希望在PowerShell运行时隐藏批处理文件,请执行以下操作:

$batch = Start-Process -FilePath .\aws.bat -Wait -passthru -WindowStyle Hidden;$batch.ExitCode