简体   繁体   中英

How do I execute a script from Windows Powershell?

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

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?

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.

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.

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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