【问题标题】:Running a power shell script in Azure在 Azure 中运行 powershell 脚本
【发布时间】:2017-08-09 04:32:53
【问题描述】:

您好,我是在 azure 中运行 powershell 脚本并正在寻找最佳指导的新手?

我创建了一个powershell脚本来定位资源组中正在运行的机器并输出到一个文本文件,

安排此脚本在 azure 中自主运行的最佳方法是什么?

下面的脚本:

#Login-AzureRmAccount 
#$sub = Get-AzureRmSubscription | Out-GridView -PassThru | Set-AzureRmContext

$path="dcsppomsstorage.blob.core.windows.net/dcsvmpowerstate/activeserver1.txt"
function SPWservers {

[string]$ResourceGroupName = "*DCS-PP*"


$VMresourcegroups = (Get-AzureRmResourceGroup).Where({$_.ResourceGroupName -like $ResourceGroupName})
foreach($VMresourcegroup in $VMresourcegroups){
$vms=Get-AzureRmVM -ResourceGroupName $VMresourcegroup.ResourceGroupName
foreach ($vm in $vms)
{

$Status = (get-azurermvm -ResourceGroupName $VMresourcegroup.ResourceGroupName -Name $vm.Name -Status).Statuses

Write "powerstate for $($vm.Name) is $($Status[1].DisplayStatus)" | Format-Table $vm.Name, $Status[1].DisplayStatus -GroupBy $vm.name

}
}
}




SPWservers| out-file $path

【问题讨论】:

    标签: powershell azure powershell-workflow runbook


    【解决方案1】:

    正如 Bruno Faria 所说,我们可以使用 Azure 自动化来做到这一点。

    但目前,我们无法将 Runbook 作业的输出直接保存到 Azure 存储 blob。

    作为一种解决方法,我们可以在 Azure 门户中检查输出。 像这样的脚本:

    $Conn = Get-AutomationConnection -Name AzureRunAsConnection
    Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
    function SPWservers {
    [string]$ResourceGroupName = "vms"
    $VMresourcegroups = (Get-AzureRmResourceGroup).Where({$_.ResourceGroupName -like $ResourceGroupName})
    foreach($VMresourcegroup in $VMresourcegroups){
    $vms=Get-AzureRmVM -ResourceGroupName $VMresourcegroup.ResourceGroupName
    foreach ($vm in $vms)
    {
    
    $Status = (get-azurermvm -ResourceGroupName $VMresourcegroup.ResourceGroupName -Name $vm.Name -Status).Statuses
    
    Write "powerstate for $($vm.Name) is $($Status[1].DisplayStatus)" | Format-Table $vm.Name, $Status[1].DisplayStatus -GroupBy $vm.name
    
    }
    }
    }
    
    SPWservers| write-output
    

    此 Runbook 作业完成后,我们可以在此处找到输出

    关于 Azure Runbookschedules,我们可以在这里设置:

    有关在 Azure 自动化中调度 Runbook 的更多信息,请参阅此article

    【讨论】:

    • 非常有帮助,是的,非常感谢!,我现在有了工作的方法。
    【解决方案2】:

    我会推荐 Azure 自动化 中的 PowerShell runbook 作为解决问题的最佳指南。

    PowerShell 运行手册

    PowerShell Runbook 基于 Windows PowerShell。你直接编辑 使用 Azure 门户中的文本编辑器的 Runbook 代码。你 还可以使用任何离线文本编辑器并将 Runbook 导入 Azure 自动化。优势

    使用 PowerShell 代码实现所有复杂逻辑,而无需 PowerShell 工作流的额外复杂性。 Runbook 启动更快 比 PowerShell 工作流运行手册,因为它不需要编译 在运行之前。

    https://docs.microsoft.com/en-us/azure/automation/automation-runbook-types#powershell-runbooks

    您可以安排您的 PowerShell Runbook 在您希望的时间和频率上运行。

    此外,如果您使用免费层并且您的总作业运行时间每月少于 500 分钟,则 Azure 自动化的定价是免费的。如果您没有很多 VM 并且您的工作频率不是很频繁,那么您很可能能够在 Azure 中免费完成您的工作。

    这里有一个很好的教程来开始使用PowerShell runbook

    【讨论】:

    • @BenAhm 如果此答案对您有所帮助并且您感谢我的努力,请将其标记为有帮助。
    【解决方案3】:

    Azure 自动化正是您所寻找的。它易于使用并且效果很好。除此之外,您可以在免费层上运行长达 500 分钟的脚本。更多信息请点击以下链接:

    https://docs.microsoft.com/en-us/azure/automation/automation-offering-get-started

    如果您出于某种原因想要探索其他选项,还有 Azure Functions 和 Azure Web Jobs。

    Azure 函数

    https://blogs.msdn.microsoft.com/powershell/2017/02/24/using-powershell-modules-in-azure-functions/

    网络工作

    https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-create-web-jobs

    【讨论】:

    • 非常有帮助!,因为我也在尝试将脚本输出到 Azure 存储帐户/Blob 容器,你会推荐什么方法?
    • 这些都可以,但我会坚持使用 Azure 自动化。只需确保您的模块列表中有 AzureRM.Storage 即可在您的自动化运行手册中使用它。
    猜你喜欢
    • 2020-01-25
    • 2019-05-05
    • 2021-02-25
    • 2021-11-23
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    相关资源
    最近更新 更多