[玩转系统] 为 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
}
}}
我得到这样的结果:
在我的代码中,我想包含计算机名称,以防它不同。如果服务器具有 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
结果如下:
当然,您可以修改脚本以在注释中包含您想要的任何信息。
如果您觉得这有用,我希望您能让我知道。
享受!
2016 年 1 月 11 日更新
我已经更新了脚本并将其变成了一个函数。您现在可以通过管道将虚拟机引入到该函数中。我还包含了一个 Passthru 参数,以便您可以看到注释信息。该函数托管在 GitHub 上:https://gist.github.com/jdhitsolutions/6f17c1d901ff870ff7a3。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag