azurepowershell

How-to check that powershell script is running in Azure


I have a powershell script, that I am developing on local PC and deploying onto Azure. The script uses credentials, that are handled differently on PC and in Azure. I therefore need to reliably check whether the script is running on the PC or on Azure.

I cannot find any command, that tells me the answer. I hope you can help.


Solution

  • I know this is old but if the goal is to just be able to run the script locally for development then this should work.

    if ($env:computername -ne "<EnterYourComputerNameHere>") {
        # Script is running in Azure
        # use Azure Automation credentials
    } else {
        # Script is running locally
        # us Local credential process
    }
    enter code here
    
    #do the remainder of your work
    

    I checked in Azure for specific environment variables. Looks like this would be more appropriate in this case:

    if ($env:AUTOMATION_ASSET_ACCOUNTID) {
        "Running in Azure Automation"
    } else {
        "Running outside of Azure Automation"
    }
    

    I hope that helps a little bit more.