• 根据VM的TAG开停机


    公有云一个非常大的优势,就是可以根据需求进行开停机。由于计费是按时进行的,从而实现节省成本。

    Azure上用脚本按时开停机已经有很多部署的案例。本文将介绍采用VM Tag中规定的时间进行开停机的脚本。

    Tag是Azure ARM模式中的一个资源的属性。用户可以根据需求对这个资源进行标识,以便在后期的操作中进行分组操作,比如积分信息的收集。

    具体实现如下:

    一 开关机需求:

      根据内网IP地址列表检查VM上的TAG信息,提取TAG名称是指定的键值对。这个键值对的值应该是一个时间段,在这个时间段内,VM应该处于关机状态。

    二 实现思路:

      在Windows机器里订阅任务计划,每10分钟运行一次脚本。

      脚本的内容:

      1 脚本读取csv文件,文件中包含IP地址和TAG的名称

      2 根据IP地址列表查找机器

      3 提取VM的TAG信息, CSV文件中定义了TAG的名称,将是这个名称的TAG取出,并将其值取出,这个值应该是一个时间段,前面一个是关机时间,后面一个是开机时间

      4 获取当前时间

      5 当前时间与TAG时间进行比较,根据策略进行开关机

    三 脚本和文件

      1 CSV文件

    address

    tag

    10.206.5.7

    test

    10.206.5.11

    test

      2 PowerShell脚本

    function modifyvm-basedontag{
    param(
    
    #csv文件的路径
    [Parameter(Mandatory=$true)]
    [String]$csvfilepath
    
    )
        #导入CSV文件
        $inputvalues = Import-Csv -Path $csvfilepath 
        #获取网卡信息,以便通过内网ip地址进行操作
        $nics = Get-AzureRmNetworkInterface
    
        #对CSV中的内容逐条进行处理
        foreach($input in $inputvalues){
            #获取内网IP的NIC信息
            foreach($nic in $nics){
             $tempa = $nic.IpConfigurations[0].PrivateIpAddress.ToString()
             $tempb = $input.address.ToString()
             if ($tempa -eq $tempb){
             break}
            }
            #IP地址对应的VM名称
            $vmname = $nic.VirtualMachine.Id.Split('/')[-1]
            #获取VM的TAG信息,TAG信息从CSV中获取
            $vmtag = Find-AzureRmResource -TagName $input.tag | Where-Object {$_.resourcetype -like "Microsoft.Compute/virtualMachines"} | Where-Object {$_.name -match $vmname}
            #如果没有此TAG,程序退出
            if(!$vmtag){write-host "please check the tag's name"; exit}
            #获取此VM
            $vm= get-azurermvm -ResourceGroupName $vmtag.ResourceGroupName -Name $vmtag.Name
            #VM TAG的key信息
            $tKeys = $vm.Tags| select -ExpandProperty keys 
            #VM TAG的值
            $tvalues = $vm.Tags | select -ExpandProperty values 
            #如果VM没有TAG,程序退出
            if($vm.Tags.Count -eq 0){write-host "this VM has no tag"; exit}
            #如果VM的TAG有一个,如下操作
            if($vm.Tags.Count -eq 1){
                if($tKeys -eq $input.tag){$time = $tvalues}
        
            }
            #如果VM TAG值超过一个,如下操作
            else
            {
                for($i=0;$i -lt $vm.tags.Count; $i++){
                  if($tKeys[$i] -eq $input.tag){$time = $tvalues[$i];write-host $time;break}
                }
            }
            #获取目前的时间
            $now = get-date
            write-host "Now is $now"
            write-host "VM stop time is $time"
    
            #tag的前面时间是关机时间,后面是开机时间。之间表示关机时间
            $stoptime=get-date $time.Split('-')[0]
            $starttime=get-date $time.Split('-')[1]
    
            #如果开机时间小于关机时间,说明日期需要加1
            if($starttime -le $stoptime){$starttime = $starttime.AddDays(1)}
    
            #获取VM的Power状态
            $status = get-azurermvm -ResourceGroupName $vmtag.ResourceGroupName -Name $vmtag.Name -Status
            $vmstatus=$status.Statuses[1].Code.Split('/')[-1]
    
            #在VM处于运行状态时:
            if($vmstatus -eq "running"){
    
              #小于停止时间,不做操作
              if($now -lt $stoptime){write-host "it's time to run vm, and VM is running, do nothing"}
              #大于停止时间,小于开机时间,停机
              if(($now -gt $stoptime) -and ($now -lt $starttime)){write-host "VM is running, it is time to stop"; #stop-azurermvm -ResourceGroupName $vmtag.ResourceGroupName -Name $vmtag.Name -Force
              }
              #大于开机时间,不做操作
              if($now -gt $starttime) {write-host "it's time to start the vm, and the vm is running, do nothing"}
            }
            #机器处于停机状态
            else{
              #小于停机时间,开机
              if($now -lt $stoptime){write-host "it's time to start VM, and the vm is stopped, start it"; #start-azurermvm -ResourceGroupName $vmtag.ResourceGroupName -Name $vmtag.Name
              }
              #大于开机时间,小于停机时间,不做操作
              if(($now -gt $stoptime) -and ($now -lt $starttime)){write-host "it is time to stop, and the VM is stopped, do nothing"}
              #大于开机时间,开机
              if($now -gt $starttime) {write-host "it's time to start the vm, and the vm is stopped, start it";
              #start-azurermvm -ResourceGroupName $vmtag.ResourceGroupName -Name $vmtag.Name
              }
            }
        }
    }
    
    modifyvm-basedontag -csvfilepath D:a.csv
  • 相关阅读:
    poj 2155 Matrix
    iOS之Prefix.pch
    多用派发队列,少用同步锁
    SenTestingKit.framework的报错!
    xcode4的环境变量,Build Settings参数,workspace及联编设置
    xcode4中build Settings常见参数解析
    XCode环境变量及路径设置
    Xcode添加静态库以及编译选项配置常见问题
    基于第三方微信授权登录的iOS代码分析
    理解iOS 8中的Self Sizing Cells和Dynamic Type
  • 原文地址:https://www.cnblogs.com/hengwei/p/7323653.html
Copyright © 2020-2023  润新知