Functions/Backup-VM.ps1
function Backup-VM { [CmdletBinding()] param ( [Parameter()] [string[]] $VMName, [Parameter()] [string] $Path = "D:\HV_backup", [Parameter()] [switch] $All, [Parameter()] [string[]] $ExcludeVM ) if ($VMName) { $vm = Get-VM $VMName } elseif ($All) { $vm = Get-VM } else { throw "No VM specified, use parameter VMName or -All" } $i = 0 foreach ($v in $vm) { $i++ if ($ExcludeVM -contains $v.Name) { Write-Warning "$($v.Name) is excluded, skipping... ($($i) of $(($vm).count))" } else { $date = Get-Date $DateTimeString = "$($date.Year)$($date.Month)$($date.Day)_$($date.Hour)$($date.Minute)$($date.Second)" $FullBackupPath = Join-Path $Path "$($DateTimeString)_$($v.Name)" Write-Verbose "Exporting $($v.Name) to path $($FullBackupPath) ($($i) of $(($vm).count))" -Verbose Export-VM -VM $v -Path $FullBackupPath } } } $scriptblock = { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) $o = Get-VM | Where-Object { $_.Name -like "$wordToComplete*" } $o.Name | ForEach-Object { if ($_ -match " ") { "'$_'" } else { $_ } } } Register-ArgumentCompleter -CommandName Backup-VM -ParameterName VMName -ScriptBlock $scriptblock Register-ArgumentCompleter -CommandName Backup-VM -ParameterName ExcludeVM -ScriptBlock $scriptblock |