1

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.

Hasta Tamang
  • 2,205
  • 1
  • 18
  • 17

2 Answers2

1

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.

  • Thanks a lot. This solved my problem !! I had hoped for a more versatile solution, but without generality, one may work with less :-) Thanks alot. – Jørgen Lindskov Knudsen Apr 02 '20 at 12:43
  • `$env:AUTOMATION_ASSET_ACCOUNTID` doesn't exist on Azure. – Ajay Jun 25 '20 at 08:01
  • I've just been informed that Azure is migrating the Automation runtime from something called the "SQLPAL" sandbox to Azure Containerized Instances, and the `AUTOMATION_ASSET_ACCOUNTID` is going away. This has been causing my runbooks to fail for a week. Finally got a straight answer from Azure Support. – Larry Silverman Aug 28 '23 at 19:27
-2

Welcome to StackOverflow! You can use multiple ways:

  1. https://learn.microsoft.com/en-us/azure/automation/automation-runbook-execution#viewing-job-status-from-the-azure-portal

  2. Send logs to output-stream:

https://learn.microsoft.com/en-us/azure/automation/automation-runbook-output-and-messages#output-stream

  1. Send logs to Azure Monitor:

https://learn.microsoft.com/en-us/azure/automation/automation-manage-send-joblogs-log-analytics

Hope this helps!

AmanGarg-MSFT
  • 1,123
  • 6
  • 10
  • 2
    Sorry, but these suggestions does not solve the problem. I need a wey to write an if-statement, where the condition is true, only when the script is run in an azure runbook. Initially, I was under the assumption that the cmd [System.Environment]::OSVersion.Platform would give the answer, but it returns ‘Win32NT’ both on the pc and in the azure runbook. – Jørgen Lindskov Knudsen Oct 27 '19 at 14:35
  • I misunderstood the question. Here is a way you can use to figure out if it is a VM or local - https://stackoverflow.com/a/28161983/10571855 – AmanGarg-MSFT Oct 27 '19 at 16:32
  • 1
    Thanks again, but this does not work either. dmidecode reports, that /dev/mem does not exist. My guess is, that the example relies on a Linux (judged from the use of the grep command). – Jørgen Lindskov Knudsen Oct 27 '19 at 21:48
  • To check if it is a windows VM check if a process named WindowsAzureGuestAgent.exe is running or not -- https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/agent-windows#manual-detection. To check if a process is running on windows you can use `tasklist` -- https://stackoverflow.com/questions/162291/how-to-check-if-a-process-is-running-via-a-batch-script – AmanGarg-MSFT Oct 28 '19 at 20:13