简体   繁体   English

如何检查 powershell 脚本是否在 Azure 中运行

[英]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.我有一个 powershell 脚本,我正在本地 PC 上开发并部署到 Azure 上。 The script uses credentials, that are handled differently on PC and in Azure.该脚本使用凭据,在 PC 和 Azure 中处理方式不同。 I therefore need to reliably check whether the script is running on the PC or on Azure.因此,我需要可靠地检查脚本是在 PC 上还是在 Azure 上运行。

I cannot find any command, that tells me the answer.我找不到任何可以告诉我答案的命令。 I hope you can help.我希望你能帮忙。

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.我在 Azure 中检查了特定的环境变量。 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.我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM