4

so I have this .bat file:

@echo off
cd C:\Users\user\Downloads
gcloud auth activate-service-account --key-file=keyFileName.json
gcloud auth print-access-token
pause

During the first gcloud command, it will suddenly crash the command prompt halfway, but when I copy and paste each line manually into command prompt in the same location as the location I am trying to cd to in the .bat file, it works... Any idea why? I am on Windows 10 by the way.

Searching on Google, I found two related issues, on Github and Stackoverflow.

Github's solution was using python which was not what I needed and Stackoverflow's one did not have anyone helping him/her...

Thanks

wolnavi
  • 183
  • 1
  • 14
  • This is a pure guess but seeing as you have an environment to test with ... try gcloud auth activate-service-account --key-file=keyFileName.json --quiet – Kolban Dec 04 '19 at 23:13
  • *"...but when I copy and paste each line manually, it works..."* copy where, into a Command Prompt window? at what path? – aschipfl Dec 04 '19 at 23:17
  • @Kolban Tried that but it stills quits halfway while trying the first command @aschipfl Yes, into a Command Prompt at the same path I am trying to `cd` to in the `.bat` file, I have edited my question to reflect that. Sorry for the confusion. – wolnavi Dec 04 '19 at 23:20
  • 1
    Here's another guess ... try the following in your bat file: cmd /c gcloud auth activate-service-account --key-file=keyFileName.json – Kolban Dec 04 '19 at 23:25
  • 2
    @Kolban Yes, thank you, it finally worked... i just have to add at the start of both of the `gcloud` command with `cmd /c` You may want to add that as a answer so I can mark this as solved, thanks – wolnavi Dec 04 '19 at 23:32

2 Answers2

7

When running some gcloud commands, they have the expectation that they may be interacting with a user. In addition, they can adversely interact with the current command line processor (CMD on Windows). This can result in the command line processor ending prematurely. One solution is to run your scripted gcloud commands in their own local / nested instance of a command line processor.

If today, your script contains:

gcloud ... some command parameters ...

replace this with:

cmd /c gcloud ... some command parameters ...

This will cause the gcloud command to run in its own nested environment which won't interfere with the top level (scripted) environment.

Kolban
  • 13,794
  • 3
  • 38
  • 60
2

You can also just add CALL before each command, for example:

CALL gcloud app deploy
CALL gcloud app describe
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126