自动终止30天后终止Azure VM

Atf*_*Atf 6 azure azure-virtual-machine azure-powershell

在我今天的活动中,我在azure中创建测试vm然后将其删除.出于某种原因,这次我计划创建100个测试vms,我想实现一个调度机制(通过powershell或AzureRunbook),以便创建的服务器可以在30天后自动删除...

问题是我可以使用Powershell找出创建的云服务日期,但不能创建VM的创建日期.很少有云服务包含我不想删除的旧虚拟机.

我正在考虑vm的不同命名约定,以便我可以使用它来删除时间 -

if($vm.name -like "mypattern*")
{
     $out1 = $vm.Name
     $out2 = $vm.ServiceName
     Remove-AzureVM -Name $out1 -ServiceName $out2 -DeleteVHD
     sleep -Seconds 60
 } 
Run Code Online (Sandbox Code Playgroud)

我相信除此之外必须有更多的方法.什么可以替代选择.我更喜欢powershell.

Mik*_*eWo 3

根据我所了解的情况,您正在尝试查找虚拟机的创建日期。所讨论的 VM 是“经典”VM,这意味着它们部署到云服务容器,而不是通过 Azure 资源管理器创建。有些虚拟机已经创建,有些将由您的脚本继续创建。有些云服务容器已经存在了一段时间,有些可能是较新的。

我无法找到通过服务管理 API 检索 VM 创建日期的方法;然而,如果我们能够到达实际的虚拟机,那么我们还有更多工作要做。下面的脚本假设虚拟机创建日期与虚拟机上的操作系统安装日期相同(我认为这应该是一个很好的指标,我验证了它不是源映像的日期,而是源映像的日期)虚拟机创建)。为此,您需要在虚拟机上启用 PowerShell 远程端点(创建虚拟机时默认存在该端点),并且您必须在本地管理员权限下运行该脚本,因为它会与证书存储区混淆。

$remoteCreds = Get-Credential
$maxVMAgeInDays = 30
#Classic VMs
Get-AzureVM | ForEach-Object { 
    #Need to ensure we have the self-signed VM certificate installed to authenticate and secure the connection.
    $winRmCertThumbprint = $_.Vm.DefaultWinRmCertificateThumbprint 
    $certPath = "Cert:\LocalMachine\Root\$winRmCertThumbprint"
    if (!(Test-Path -Path $certPath)){
        #Cert for VM isn't found, importing.
        $winRmCert = Get-AzureCertificate -ServiceName $_.ServiceName -Thumbprint $winRmCertThumbprint -ThumbprintAlgorithm sha1

        $certTempFile = [IO.Path]::GetTempFileName()
        $winRmCert.Data | Out-File $certTempFile

        # Target The Cert That Needs To Be Imported
        $CertToImport = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $certTempFile

        $store = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine"
        $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
        $store.Add($CertToImport)
        $store.Close()

        "Imported cert: $certPath"

        #clean up temp file
        Remove-Item $certTempFile
    } 

    #Retrieve the powerShell Remote port for this machine.
    $remoteUri = Get-AzureWinRMUri -ServiceName $_.ServiceName -Name $_.Name

    $osInstallDate = Invoke-Command -ConnectionUri $remoteUri -Credential $remoteCreds -ScriptBlock { ([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate)  }

    $vmAgeInDays = (New-TimeSpan -Start $osInstallDate  -End (Get-Date)).Days
    if ($vmAgeInDays -gt $maxVMAgeInDays) {
        "$($_.Name) VM in $($_.ServiceName) cloud service is older than $maxVMAgeInDays ... you can remove it."
        #Add your code to remove the VM
    } else {
        "$($_.Name) VM in $($_.ServiceName) cloud service is only $($vmAgeInDays) old."        
        #Do soemthing else or just remove the else.
    }
}
Run Code Online (Sandbox Code Playgroud)

该脚本假定您已执行 Add-AzureAccount 并选择了要使用的订阅。它将提示您输入虚拟机的凭据,该特定脚本假定您拥有一个已设置为适用于每个虚拟机的凭据。这可以是您在创建 VM 时提供的凭据,也可以是您随后添加到每个 VM 的正确权限中的凭据。既然您说您通过脚本创建了数百个虚拟机,我的猜测是它们都将具有相同的管理凭据。对于现有的虚拟机,您可能需要手动添加帐户(这可能会很痛苦,具体取决于您拥有的虚拟机数量)。

该脚本循环遍历每个虚拟机并检查本地计算机上是否加载了 WinRM 证书。如果没有,则会将其拉下来并安装。这是保护远程 PowerShell 会话所需的。我从TechNet 上的 Michael Washam 脚本中获取了代码。

在我们知道我们拥有保护连接的证书后,他们会执行远程 PowerShell 命令来检索操作系统安装日期(我在ScioSoft 博客上找到的提示)。最后,它根据该日期检查虚拟机的使用期限,然后可以执行您想要的任何操作。根据您的情况,您可以将其删除。如果您确实想清除所有内容,您需要确保在删除虚拟机时也清理底层磁盘等。

最后,为了改进脚本,我建议您删除任何虚拟机,然后您可以通过指纹删除证书来清理您的证书存储。

这适用于经典虚拟机,听起来您已经拥有了。有人已经为您提供了一种通过标记处理基于 ARM 的虚拟机的方法,这将消除实际处理远程命令的需要。