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,152
  • 1
  • 15
  • 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.

-1

Welcome to StackOverflow! You can use multiple ways:

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

  2. Send logs to output-stream:

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

  1. Send logs to Azure Monitor:

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

Hope this helps!

AmanGarg-MSFT
  • 1,067
  • 5
  • 10
  • 1
    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://docs.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