简体   繁体   中英

Azure CLI in python

I'm running some Azure CLI commands from a python program.

process = subprocess.Popen('powershell.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE)

command_list = "Connect-AzAccount \n" + "az keyvault secret show --vault-name " + vault_name + " --name " + secret_name
out, err = process.communicate(command_list.encode('utf-8'))

Is it possbile from this to see if something went wrong? like the user didn't logg in or the vault does not exist? It seems to be printing these error to the terminal but I would like to catch them in my program.

You're actually using a mixture of CLI and Azure PowerShell module.

I've tested on my machine thinking that it shouldn't matter, but it actually does. If you login with PowerShell but execute a command with az.exe then you get:

Please run 'az login' to setup account.

...which is exactly what you need to do to use Az. So you have a choice. You can either use PowerShell only (Az Module) and replace az keyvault secret show --vault-name xxxx with Get-AzKeyVaultSecret -VaultName xxxx , az.exe only by replacing Connect-AzAccount with az login , or you could use both but you would need to log in to both. My recommendation, since you're using pipes for interprocess communication, would be to use az.exe only. But it really depends on the entirety of what you're looking to achieve.

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