【问题标题】:Is there a PowerShell variable that can be checked to see if you the script is running in the Azure cloud shell?是否可以检查 PowerShell 变量以查看脚本是否在 Azure 云外壳中运行?
【发布时间】:2020-01-11 22:54:02
【问题描述】:

类似于$IsLinux$IsWindows 的东西。

【问题讨论】:

    标签: azure-cloud-shell


    【解决方案1】:

    根据您的描述,我不确定您是如何运行脚本的。如果您使用 Start-Job 将 PowerShell 脚本作为后台作业运行,则可以使用 Get-Job 查看 State

    Start-Job -FilePath ./GetDate.ps1  
    Get-Job
    

    【讨论】:

    • 为什么我每次在 Azure 控制台中运行脚本时都要执行start-job?如果我想在本地和控制台中使用相同的脚本但将数据保存在不同的位置怎么办?我不想要 hack,我想要一个可扩展、可移植和可测试的解决方案。
    【解决方案2】:

    我建议在特定于云 shell 的 try/catch 中运行命令,如果这是您正在寻找的?

        try {
            $sessioninfo = Get-CloudDrive
            if ($sessioninfo) {
                Write-Host "Running in Cloud Shell mode..." -ForegroundColor Green
                $path = ($home + "/clouddrive/" + "report" + "-$(get-date -Format yyyyddMM_hhmmtt).csv")
            }   
        }
        catch {
            Write-Host "Running in local PowerShell session mode..." -ForegroundColor Yellow
            $path = "C:\localpath\report"+"-$(get-date -Format yyyyddMM_hhmmtt).csv"
        }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      相关资源
      最近更新 更多