当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 为 Hyper-V VM Notes 添加一些功能

作者:精品下载站 日期:2024-12-14 07:47:39 浏览:14 分类:玩电脑

为 Hyper-V VM Notes 添加一些功能


由于我在家工作,因此非常依赖 Hyper-V 环境。我假设如果您在工作中使用 Hyper-V,您的情况也是如此。由于我进行了大量测试,有时很难记住给定虚拟机上正在运行的内容。我是否将该框更新为 PowerShell v5?该虚拟机运行的是 Windows Server 2016 TP 3 还是 TP4?

Hyper-V 虚拟机有一个可以记笔记的设置,这似乎是存储系统信息的理想位置。由于我的大多数Windows机器都在我的公共网络上,并且虚拟机名称与计算机名称相同,因此我可以轻松地使用PowerShell远程连接到每个虚拟机,获取一些系统信息,并更新相应的注释。

我可以运行这样的命令来获取我需要的系统信息。

Get-WmiObject win32_Operatingsystem | 
Select @{Name="OperatingSystem";Expression={$_.Caption}},
@{Name="ServicePack";Expression={$_.CSDVersion}},
@{Name="PSVersion";Expression= {$psversiontable.PSVersion}},
@{Name="Hostname";Expression={
    If (Get-Command Resolve-DNSName -ErrorAction SilentlyContinue) {
    (Resolve-DnsName -Name $env:computername -Type A).Name
    }
    else {
[system.net.dns]::Resolve($env:computername).hostname
}
}}

我得到这样的结果:

[玩转系统] 为 Hyper-V VM Notes 添加一些功能

在我的代码中,我想包含计算机名称,以防它不同。如果服务器具有 Resolve-DNSName cmdlet,我将调用它,否则我将使用 .NET Framework 来解析名称。

要设置 Notes 属性,我可以使用 Set-VM。

$VM = get-vm chi-dc02
Set-VM $VM -Notes $newNote

请注意,此行为将替换任何现有注释。在我的最终代码中,我考虑到了这一点,并且默认情况下我附加了系统信息。但如果您愿意,有一个参数可以替换注释内容。

我的最终代码还包含一个参数来使用虚拟机的 IP 地址而不是其名称。我有一些虚拟机不属于我的测试域,但我在 DNS 中注册了它们的名称。

这是完整的脚本。

#requires -version 4.0
#requires -module Hyper-V


<#
Update the Hyper-V VM Note with system information

The script will make a PowerShell remoting connection to each virtual machine using the 
VM name as the computer name. If that is not the case, you can use the detected IP address
and then connect to the resolved host name. Alternate credentials are supported for 
the remoting connection.

The default behavior is to append the information unless you use -Replace.

The default is all virtual machines on a given server, but you can specify an
individual VM or use a wild card.

You can only update virtual machines that are currently running.

Usage:
c:\scripts\Update-VMNote chi* -computername chi-hvr2 -credential globomantics\administrator -replace
#>

[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(Position=0)]
[ValidateNotNullorEmpty()]
[Alias("name")]
[string]$VMName = "*",
[Alias("CN")]
[string]$Computername = $env:COMPUTERNAME,
[Alias("RunAs")]
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty,
[Switch]$ResolveIP,
[Switch]$Replace
)


Write-Verbose "Starting: $($MyInvocation.Mycommand)"

Write-Verbose "Getting running VMs from $($Computername.ToUpper())"

$VMs = (Get-VM -Name $VMName -computername $computername).Where({$_.state -eq 'running'})

if ($VMs) {
    foreach ($VM in $VMs) {
    Write-Host "Processing $($VM.Name)" -ForegroundColor Green
    if ($ResolveIP) {
        #get IP Address
        $IP = (Get-VMNetworkAdapter $VM).IPAddresses | where {$_ -match "\d{1,3}\." } | select -first 1

        #resolve IP address to name
        $named = (Resolve-DnsName -Name $IP).NameHost
    }
    else {
        #use VMname
        $named = $VM.name
    }
    
    #get PSVersion
    #get Operating System and service pack
    #resolving hostname locally using .NET because not all machines
    #may have proper cmdlets
    $sb = { 
        Get-WmiObject win32_Operatingsystem | 
        Select @{Name="OperatingSystem";Expression={$_.Caption}},
        @{Name="ServicePack";Expression={$_.CSDVersion}},
        @{Name="PSVersion";Expression= {$psversiontable.PSVersion}},
        @{Name="Hostname";Expression={
            If (Get-Command Resolve-DNSName -ErrorAction SilentlyContinue) {
            (Resolve-DnsName -Name $env:computername -Type A).Name
            }
            else {
        [system.net.dns]::Resolve($env:computername).hostname
        }
        }}
      } #close scriptblock
    
    #create a hashtable of parameters to splat to Invoke-Command
    $icmHash = @{
        ErrorAction = "Stop"
        Computername = $Named
        Scriptblock = $sb
    }
    
    #add credential if specified
    if ($Credential.username) {
        $icmHash.Add("Credential",$Credential)
    }
    Try {
        #run remoting command
        Write-Verbose "Getting remote information"
        $Info = Invoke-Command @icmHash  | Select * -ExcludeProperty RunspaceID
        #update Note
        Write-Verbose "`n$(($info | out-string).Trim())"
        if ($Replace) {
            Write-Verbose "Replacing VM Note"
            $newNote = ($info | out-string).Trim()
        }
        else {
            Write-Verbose "Appending VM Note"
            $current = $VM.Notes
            $newNote = $Current + "`n" + ($info | out-string).Trim()    
        }
        Set-VM $VM -Notes $newNote
        #reset variable
        Remove-Variable Info
    } #try

    Catch {
        Write-Warning "[$($VM.Name)] Failed to get guest information. $($_.exception.message)"
    } #catch

    } #foreach VM
} #if running VMs found
else {
    Write-Warning "Failed to find any matching running virtual machines on $Computername"
}

Write-Verbose "Ending: $($MyInvocation.Mycommand)"

请注意,这是一个脚本而不是函数。我现在可以轻松更新我的虚拟机。

C:\scripts\Update-VMNote.ps1 chi* -Computername chi-hvr2 -Credential globomantics\administrator -replace

[玩转系统] 为 Hyper-V VM Notes 添加一些功能

结果如下:

[玩转系统] 为 Hyper-V VM Notes 添加一些功能

当然,您可以修改脚本以在注释中包含您想要的任何信息。

如果您觉得这有用,我希望您能让我知道。

享受!

2016 年 1 月 11 日更新

我已经更新了脚本并将其变成了一个函数。您现在可以通过管道将虚拟机引入到该函数中。我还包含了一个 Passthru 参数,以便您可以看到注释信息。该函数托管在 GitHub 上:https://gist.github.com/jdhitsolutions/6f17c1d901ff870ff7a3。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯