【问题标题】:Utilizing Azure Custom Script Extensions to maintain a custom PowerShell module across multiple servers利用 Azure 自定义脚本扩展跨多个服务器维护自定义 PowerShell 模块
【发布时间】:2021-01-22 14:16:20
【问题描述】:

我正在尝试自动化工作中的一些任务,有人告诉我,我们可以利用 Azure 自定义脚本扩展来跨多个服务器维护自定义 PowerShell 模块。

该脚本位于我们的 Azure DevOps 上,但我假设我可以创建一个管道,在拉取请求完成后将其移动到存储帐户。

但是,我的问题是 - 自定义脚本扩展首先是如何工作的,它是否适合我的需求,即在多台服务器上都有最新的 PowerShell 模块可用?

【问题讨论】:

    标签: azure


    【解决方案1】:

    你能找到问题的答案吗?

    据我所知,安装自定义脚本扩展后,脚本将执行一次。

    下面的代码 sn-p 显示了如何在多个 VM 上部署自定义脚本扩展:

    $CustomerScriptExtension = "yourCSEName"
    $AllVMs = Get-AzureRMVM  #get all VMs
    $CntOfVMs = 0;
    foreach($vm in $AllVMs)
    {
        try
        {
            $CntOfVMs = $CntOfVMs + 1
            Write-Host "VMCount:" $CntOfVMs
            #Get the VM details of that instance
            $vmDetails = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name             vm.Name
            #Check if already an extension exists of type Custome Script Extension
            $cseExtension = $vmDetails.Extensions | where { $_.Publisher -eq 
    "Microsoft.Compute" -and $_.VirtualMachineExtensionType -eq "CustomScriptExtension" }
     
            # If Extension already exists remove the extension
            if ($cseExtension)
            {
                #Remove the extension
                Write-Host "Removing existing extension on VirtualMachine:" $vm.Name
                Remove-AzureRmVMCustomScriptExtension -ResourceGroupName 
      $vm.ResourceGroupName -VMName $vm.Name -Name $cseExtension.Name
                   Write-Host "Completed Removal of existing extension on VirtualMachine:" 
      $vm.Name
            }
           
            try
            {
                Write-Host "Installing extenstion on VirtualMachine :" $vm.Name
                Set-AzureRmVMCustomScriptExtension -ResourceGroupName 
      $vm.ResourceGroupName -VMName $vm.Name -Location $vm.Location -FileUri 
      PathToyourPowerShellCodeToUpdateVMGoesThere.ps1 -Run "InstalluptodatePowerShell.ps1" -name 
      $CustomerScriptExtension
                Write-Host "completed Extension installation on VirtualMachine  :" 
      $vm.Name 
            }
            catch
            {
                Write-Host "Error installing extension on  VirtualMachine  :" $vm.Name
    
       
            }
        }
           
        catch
        {
             Write-Host "Error in setting extension on VM:" $vm.Name 
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 1970-01-01
      • 2020-03-09
      • 2023-02-16
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多