[玩转系统] InfoWorld:自动化实时 VM 导出
作者:精品下载站 日期:2024-12-14 07:41:43 浏览:16 分类:玩电脑
InfoWorld:自动化实时 VM 导出
我写的剧本比彼得最初设想的要复杂一些。现在,我怀疑他的目标能否通过一句俏皮话来实现。因此,由于我需要编写一个脚本,因此我花了一些时间来使其在错误处理和参数验证等方面变得健壮。我只想开发一次脚本,为什么不彻底呢?
当我根据 Peter 的要求完成脚本时,我意识到这也可以使用 PowerShell 工作流程来解决。原始脚本的限制之一是它需要在 Hyper-V 服务器上运行。我没有包含任何连接到远程服务器的规定。我还认识到导出多个虚拟机可以并行完成。尽管我的原始脚本允许使用后台作业,这有点像并行运行。但我认为工作流程版本至少可能具有教育意义。以下是 Export-MyVM 工作流程。
#requires -version 3.0
#requires -module Hyper-V
Workflow Export-MyVM {
Param(
[Parameter(Position=0,Mandatory=$True,
HelpMessage="Enter the virtual machine name or names")]
[ValidateNotNullorEmpty()]
[Alias("name")]
[string[]]$VM,
[Parameter(Position=0,Mandatory=$True,
HelpMessage="Enter the root backup path")]
[ValidateNotNullorEmpty()]
[string]$Path,
[switch]$Monthly
)
Write-Verbose -Message "Starting $workflowcommandname"
#define some variables if we are doing weekly or monthly backups
if ($monthly) {
$type = "Monthly"
$retain = 2
}
else {
$type = "Weekly"
$retain = 4
}
Write-Verbose -message "Processing $type backups. Retaining last $retain."
#manage folders
#get backup directory list
Try {
Write-Verbose -Message "Checking $path for subfolders"
#get only directories under the path that start with Weekly or Monthly
$subFolders = Get-ChildItem -Path $path$type* -Directory -ErrorAction Stop
}
Catch {
Write-Warning "Failed to enumerate folders from $path. $($_.Exception.Message)"
#bail out of the script
return
}
#check if any backup folders
if ($subFolders) {
#if found, get count
Write-Verbose -message "Found $($subfolders.count) folder(s)"
#if more than the value of $retain, delete oldest one
if ($subFolders.count -ge $retain ) {
#get oldest folder based on its CreationTime property
$oldest = $subFolders | Sort-Object -property CreationTime | Select-Object -first 1
Write-Verbose -message "Deleting oldest folder $($oldest.fullname)"
#delete it
$oldest | Remove-Item -Recurse -Force
}
} #if $subfolders
else {
#if none found, create first one
Write-Verbose -Message "No matching folders found. Creating the first folder"
}
#create the folder
#get the current date
$now = Get-Date
#name format is Type_Year_Month_Day_HourMinute
$childPath = "{0}_{1}_{2:D2}_{3:D2}_{4:D2}{5:D2}" -f $type,$now.year,$now.month,$now.day,$now.hour,$now.minute
#create a variable that represents the new folder path
$newpath = Join-Path -Path $path -ChildPath $childPath
Try {
Write-Verbose -message "Creating $newpath"
#Create the new backup folder
$BackupFolder = New-Item -Path $newpath -ItemType directory -ErrorAction Stop
}
Catch {
Write-Warning -message "Failed to create folder $newpath."
throw $_
#failed to create folder so bail out of the script
Return
}
#export VMs
if ($BackupFolder) {
#export each machine in parallel
foreach -parallel ($item in $VM) {
Write-Verbose -Message "Exporting $item"
#define a hashtable of parameters to splat to Export-VM
$exportParam = @{
Path = $newPath
Name=$item
ErrorAction="Stop"
}
Try {
Export-VM @exportParam
}
Catch {
Write-Warning "Failed to export virtual machine(s)."
Throw $_
}
} #foreach parallel
} #if backup folder exists
Write-Verbose -Message "Ending $workflowcommandname"
} #close workflow
尽管我删除了 WhatIf 参数,但大部分代码与原始脚本相同。您不能在工作流程中使用 SupportsShouldProcess,而且我没有时间完全编写自己的。唯一真正特定于工作流程的代码是:
#export each machine in parallel
foreach -parallel ($item in $VM) {
Write-Verbose -Message "Exporting $item"
#define a hashtable of parameters to splat to Export-VM
$exportParam = @{
Path = $newPath
Name=$item
ErrorAction="Stop"
}
Try {
Export-VM @exportParam
}
Catch {
Write-Warning "Failed to export virtual machine(s)."
Throw $_
}
} #foreach parallel
也许最大的优势是,通过工作流程,我可以获得对后台作业和远程处理的自动支持。现在我可以针对 Hyper-V 服务器执行工作流程。
PS C:\> export-myvm -name 'chi-client02','chi-dctest' -path d:\backup -Verbose -AsJob -PSComputerName chi-hvr2.globomantics.local
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
100 Job100 PSWorkflowJob Running True chi-hvr2.globoman... export-myvm
PS C:\> get-job 100 -IncludeChildJob
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
100 Job100 PSWorkflowJob Running True chi-hvr2.globoman... export-myvm
101 Job101 PSWorkflowJob Running True chi-hvr2.globoman... Export-MyVM
我仍然可以在我的计算机上创建 PowerShell 计划作业来运行此工作流程。
顺便说一句,我相信您知道 Altaro、Veeam 和 Unitrends 等公司(所有这些公司都为我的博客提供了支持)提供了大量 Hyper-V 备份产品。其中一些甚至有其产品的免费版本。因此,虽然您可以使用 PowerShell 导出虚拟机,但这并不意味着您应该这样做。尽管我可以看到快速而脏的备份的价值。最终,我认为有选择是一件好事。
享受。
猜你还喜欢
- 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