Hyper-V Backup Powershell 备份脚本Export-VM

<#PSScriptInfo

.VERSION 4.5

.GUID c7fb05cc-1e20-4277-9986-523020060668

.AUTHOR Mike Galvin Contact: mike@gal.vin twitter.com/mikegalvin_

.COMPANYNAME Mike Galvin

.COPYRIGHT (C) Mike Galvin. All rights reserved.

.TAGS Hyper-V Virtual Machines Cluster CSV Full Backup Export Permissions Zip History

.LICENSEURI

.PROJECTURI https://gal.vin/2017/09/18/vm-backup-for-hyper-v

.ICONURI

.EXTERNALMODULEDEPENDENCIES Windows 10/Windows Server 2016/Windows 2012 R2 Hyper-V PowerShell Management Modules

.REQUIREDSCRIPTS

.EXTERNALSCRIPTDEPENDENCIES Hyper-V PowerShell Management Tools

.RELEASENOTES

#>

<#
    .SYNOPSIS
    Hyper-V Backup PowerShell Utility - Creates a full backup of running Hyper-V Virtual Machines.

    .DESCRIPTION
    This script creates a full backup of running Hyper-V Virtual Machines.

    This script will:
    
    Create a full backup of Virtual Machine(s), complete with configuration, snapshots/checkpoints, and VHD files.
    
    The -List switch should be used to specify a txt file with a list of VM names to backup. If this option is not
    configured, all running VMs will be backed up.

    If the -NoPerms switch is used, the script will shutdown the VM and copy all the files to the backup location, then start the VM.
    You should use the -NoPerms switch if Hyper-V does not have the appropriate permissions to the specified backup location to do an export.
    If the -NoPerms switch is NOT used, the script will use the built-in export function, and the VMs will continue to run.

    The -Keep switch should be used to keep the specified number of days worth of backups. For example, to keep one months worth of backups
    use -Keep 30.

    The -Compress switch should be used to generate a zip file of each VM that is backed up. The original backup folder will be deleted afterwards.

    Important note: This script should be run on a Hyper-V host. The Hyper-V PowerShell management modules should be installed.

    Please note: to send a log file using ssl and an SMTP password you must generate an encrypted
    password file. The password file is unique to both the user and machine.
    
    The command is as follows:

    $creds = Get-Credential
    $creds.Password | ConvertFrom-SecureString | Set-Content c:\foo\ps-script-pwd.txt
    
    .PARAMETER BackupTo
    The path the Virtual Machines should be backed up to.
    A folder will be created in the specified path and each VM will have it's own folder inside.

    .PARAMETER List
    Enter the path to a txt file with a list of Hyper-V VM names to backup. If this option is not configured, all running VMs will be backed up.

    .PARAMETER L
    The path to output the log file to.
    The file name will be Hyper-V-Backup-YYYY-MM-dd-HH-mm-ss.log

    .PARAMETER NoPerms
    Instructs the script to shutdown the running VM(s) to do the file-copy based backup, instead of using the Hyper-V export function.
    When multiple VMs are running, the first VM (alphabetically) will be shutdown, backed up, and then started, then the next and so on.

    .PARAMETER Keep
    Instructs the script to keep a specified number of days worth of backups. The script will delete VM backups older than the number of days specified.

    .PARAMETER Compress
    This option will create a .zip file of each Hyper-V VM backup. Available disk space should be considered when using this option.

    .PARAMETER Subject
    The email subject that the email should have. Encapulate with single or double quotes.

    .PARAMETER SendTo
    The e-mail address the log should be sent to.

    .PARAMETER From
    The e-mail address the log should be sent from.

    .PARAMETER Smtp
    The DNS name or IP address of the SMTP server.

    .PARAMETER User
    The user account to connect to the SMTP server.

    .PARAMETER Pwd
    The txt file containing the encrypted password for the user account.

    .PARAMETER UseSsl
    Configures the script to connect to the SMTP server using SSL.

    .EXAMPLE
    Hyper-V-Backup.ps1 -BackupTo \\nas\vms -List C:\scripts\vms.txt -NoPerms -Keep 30 -Compress -L C:\scripts\logs -Subject 'Server: Hyper-V Backup' -SendTo me@contoso.com -From hyperv@contoso.com -Smtp smtp.outlook.com -User user -Pwd C:\foo\pwd.txt -UseSsl
    This will shutdown all the VMs listed in the file located in C:\scripts\vms.txt, and back up their files to \\nas\vms. Each VM will have their own folder. A zip file for each VM folder will be created, and the
    folder will be deleted. Any backups older than 30 days will also be deleted. The log file will be output to C:\scripts\logs and sent via e-mail with a custom subject line.
#>

## Set up command line switches and what variables they map to.
[CmdletBinding()]
Param(
    [parameter(Mandatory=$True)]
    [alias("BackupTo")]
    $Backup,
    [alias("Keep")]
    $History,
    [alias("List")]
    [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
    $VmList,
    [alias("L")]
    [ValidateScript({Test-Path $_ -PathType 'Container'})]
    $LogPath,
    [alias("Subject")]
    $MailSubject,
    [alias("SendTo")]
    $MailTo,
    [alias("From")]
    $MailFrom,
    [alias("Smtp")]
    $SmtpServer,
    [alias("User")]
    $SmtpUser,
    [alias("Pwd")]
    [ValidateScript({Test-Path -Path $_ -PathType Leaf})]
    $SmtpPwd,
    [switch]$Compress,
    [switch]$UseSsl,
    [switch]$NoPerms)

## If logging is configured, start logging.
If ($LogPath)
{
    $LogFile = ("Hyper-V-Backup-{0:yyyy-MM-dd-HH-mm-ss}.log" -f (Get-Date))
    $Log = "$LogPath\$LogFile"

    ## If the log file already exists, clear it.
    $LogT = Test-Path -Path $Log

    If ($LogT)
    {
        Clear-Content -Path $Log
    }

    Add-Content -Path $Log -Value "****************************************"
    Add-Content -Path $Log -Value "$(Get-Date -Format G) Log started"
    Add-Content -Path $Log -Value ""
}

## Set a variable for computer name of the HyperV server.
$Vs = $Env:ComputerName

## If a VM list file is configured, get the content of the file.
If ($VmList)
{
    $Vms = Get-Content $VmList
}

## If a VM list file is not configured, just get the running VMs.
Else
{
    $Vms = Get-VM | Where-Object {$_.State -eq 'Running'} | Select-Object -ExpandProperty Name
}

## Check to see if there are any running VMs.
If ($Vms.count -ne 0)
{
    ## For logging if enabled.
    If ($LogPath)
    {
        Add-Content -Path $Log -Value "$(Get-Date -Format G) This virtual host is: $Vs"
        Add-Content -Path $Log -Value "$(Get-Date -Format G) The following VMs will be backed up:"

        ForEach ($Vm in $Vms)
        {
            Add-Content -Path $Log -Value "$Vm"
        }
    }

    Write-Host "$(Get-Date -Format G) This virtual host is: $Vs"
    Write-Host "$(Get-Date -Format G) The following VMs will be backed up:"

    ForEach ($Vm in $Vms)
    {
        Write-Host "$Vm"
    }

    ## Check to see if the NoPerms switch is set.
    If ($NoPerms) 
    {
        ## For each VM do the following.
        ForEach ($Vm in $Vms)
        {
            $VmInfo = Get-VM -name $Vm

            ## Test for the existence of a previous VM export. If it exists, delete it.
            $VmExportBackupTest = Test-Path "$Backup\$Vm"
            If ($VmExportBackupTest -eq $True)
            {
                Remove-Item "$Backup\$Vm" -Recurse -Force
            }

            ## Create directories for the VM export.
            New-Item "$Backup\$Vm" -ItemType Directory -Force
            New-Item "$Backup\$Vm\Virtual Machines" -ItemType Directory -Force
            New-Item "$Backup\$Vm\VHD" -ItemType Directory -Force
            New-Item "$Backup\$Vm\Snapshots" -ItemType Directory -Force
            
            ## For logging, test for creation of backup folders. Log if the have or haven't.
            $VmFolderTest = Test-Path "$Backup\$Vm\Virtual Machines"
            If ($VmFolderTest -eq $True)
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created backup folder $Backup\$Vm\Virtual Machines"
                }

                Write-Host "$(Get-Date -Format G) Successfully created backup folder $Backup\$Vm\Virtual Machines"
            }

            Else
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem creating folder $Backup\$Vm\Virtual Machines"
                }

                Write-Error "$(Get-Date -Format G) ERROR: There was a problem creating folder $Backup\$Vm\Virtual Machines"
            }

            $VmVHDTest = Test-Path "$Backup\$Vm\VHD"
            If ($VmVHDTest -eq $True)
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created backup folder $Backup\$Vm\VHD"
                }

                Write-Host "$(Get-Date -Format G) Successfully created backup folder $Backup\$Vm\VHD"
            }

            Else
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem creating folder $Backup\$Vm\VHD"
                }

                Write-Error "$(Get-Date -Format G) ERROR: There was a problem creating folder $Backup\$Vm\VHD"                
            }
            
            $VmSnapTest = Test-Path "$Backup\$Vm\Snapshots"
            If ($VmSnapTest -eq $True)
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created backup folder $Backup\$Vm\Snapshots"
                }

                Write-Host "$(Get-Date -Format G) Successfully created backup folder $Backup\$Vm\Snapshots"
            }

            Else
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem creating folder $Backup\$Vm\Snapshots"
                }

                Write-Error "$(Get-Date -Format G) ERROR: There was a problem creating folder $Backup\$Vm\Snapshots"
            }

            ## Stop the VM.
            Stop-VM $Vm

            ## For logging.
            If ($LogPath)
            {
                Add-Content -Path $Log -Value "$(Get-Date -Format G) Stopping VM: $Vm"
            }

            Write-Host "$(Get-Date -Format G) Stopping VM: $Vm"

            ## Wait for 5 seconds before continuing just to be safe.
            Start-Sleep -S 5

            ## Copy the config files and folders.
            Copy-Item "$($VmInfo.ConfigurationLocation)\Virtual Machines\$($VmInfo.id)" "$Backup\$Vm\Virtual Machines\" -Recurse -Force
            Copy-Item "$($VmInfo.ConfigurationLocation)\Virtual Machines\$($VmInfo.id).*" "$Backup\$Vm\Virtual Machines\" -Recurse -Force

            $VmConfigTest = Test-Path "$Backup\$Vm\Virtual Machines\*"
            If ($VmConfigTest -eq $True)
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully copied $Vm configuration to $Backup\$Vm\Virtual Machines"
                }

                Write-Host "$(Get-Date -Format G) Successfully copied $Vm configuration to $Backup\$Vm\Virtual Machines"
            }

            Else
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem copying the configuration for $Vm"
                }
                
                Write-Error "$(Get-Date -Format G) ERROR: There was a problem copying the configuration for $Vm"
            }

            ## Copy the VHD(s).
            Copy-Item $VmInfo.HardDrives.Path -Destination "$Backup\$Vm\VHD\" -Recurse -Force

            $VmVHDCopyTest = Test-Path "$Backup\$Vm\VHD\*"
            If ($VmVHDCopyTest -eq $True)
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully copied $Vm VHDs to $Backup\$Vm\VHD"
                }

                Write-Host "$(Get-Date -Format G) Successfully copied $Vm VHDs to $Backup\$Vm\VHD"
            }

            Else
            {
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem copying the VHDs for $Vm"
                }
                
                Write-Error "$(Get-Date -Format G) ERROR: There was a problem copying the VHDs for $Vm"
            }

            ## Get the VM snapshots/checkpoints.
            $Snaps = Get-VMSnapshot $Vm

            ## For each snapshot do the following.
            ForEach ($Snap in $Snaps)
            {
                ## Copy the snapshot config files and folders.
                Copy-Item "$($VmInfo.ConfigurationLocation)\Snapshots\$($Snap.id)" "$Backup\$Vm\Snapshots\" -Recurse -Force
                Copy-Item "$($VmInfo.ConfigurationLocation)\Snapshots\$($Snap.id).*" "$Backup\$Vm\Snapshots\" -Recurse -Force

                $VmSnapCopyTest = Test-Path "$Backup\$Vm\Snapshots\*"
                If ($VmSnapCopyTest -eq $True)
                {
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully copied checkpoint configuration for $Backup\$Vm\Snapshots"
                    }
                        
                    Write-Host "$(Get-Date -Format G) Successfully copied checkpoint configuration for $Backup\$Vm\Snapshots"
                }

                Else
                {
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem copying the checkpoint configuration for $Vm"
                    }
                    
                    Write-Error "$(Get-Date -Format G) ERROR: There was a problem copying the checkpoint configuration for $Vm"
                }

                ## Copy the snapshot root VHD.
                Copy-Item $Snap.HardDrives.Path -Destination "$Backup\$Vm\VHD\" -Recurse -Force

                ## For logging.
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully copied checkpoint VHDs for $Vm to $Backup\$Vm\VHD"
                }

                Write-Host "$(Get-Date -Format G) Successfully copied checkpoint VHDs for $Vm to $Backup\$Vm\VHD"
            }

            ## Start the VM.
            Start-VM $Vm

            ## For logging.
            If ($LogPath)
            {
                Add-Content -Path $Log -Value "$(Get-Date -Format G) Starting VM: $Vm"
            }

            Write-Host "$(Get-Date -Format G) Starting VM: $Vm"

            ## Wait for 30 seconds before continuing just to be safe.
            Start-Sleep -S 30

            ## Check to see if the keep command line switch is not configured.
            If ($Null -eq $History)
            {
                ## Check to see if the compress command line switch is not configured.
                If ($Compress -eq $False)
                {
                    ## Remove all previous backup folders.
                    Get-ChildItem -Path $Backup -Filter "$Vm-*-*-*-*-*-*" -Directory | Remove-Item -Recurse -Force

                    ## For logging.
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing previous backup folders."
                    }

                    Write-Host "$(Get-Date -Format G) Removing previous backup folders."
                }
            }

            ## If the keep command line switch is configured...
            Else
            {
                ## ...and if the compress command line switch is not configured.
                If ($Compress -eq $False)
                {
                    ## Remove all previous backup folders that are older than the configured number of days.
                    Get-ChildItem -Path $Backup -Filter "$Vm-*-*-*-*-*-*" -Directory | Where-Object CreationTime –lt (Get-Date).AddDays(-$History) | Remove-Item -Recurse -Force

                    ## For logging.
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing backup folders older than: $History days"
                    }

                    Write-Host "$(Get-Date -Format G) Removing backup folders older than: $History days"
                }
            }

            ## Check to see if the compress command line switch is configured...
            If ($Compress)
            {
                ## ...and if the keep command line switch is not configured.
                If ($Null -eq $History)
                {
                    ## Remove all previous compressed backups.
                    Remove-Item "$Backup\$Vm-*-*-*-*-*-*.zip" -Force

                    ## For logging.
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing previous compressed backups."
                    }

                    Write-Host "$(Get-Date -Format G) Removing previous compressed backups."
                }

                ## If the keep command line switch is configured.
                Else
                {
                    ## Remove previous compressed backups that are older than the configured number of days.
                    Get-ChildItem -Path "$Backup\$Vm-*-*-*-*-*-*.zip" | Where-Object CreationTime –lt (Get-Date).AddDays(-$History) | Remove-Item -Force

                    ## For logging.
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing compressed backups older than: $History days"
                    }

                    Write-Host "$(Get-Date -Format G) Removing compressed backups older than: $History days"
                }

                ## Compress the VM backup folder into a zip, and delete the VM export folder.
                Add-Type -AssemblyName "system.io.compression.filesystem"
                [io.compression.zipfile]::CreateFromDirectory("$Backup\$Vm", "$Backup\$Vm-{0:yyyy-MM-dd-HH-mm-ss}.zip" -f (Get-Date))
                Get-ChildItem -Path $Backup -Filter "$Vm" -Directory | Remove-Item -Recurse -Force

                ## For logging.
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created compressed backup of $Vm"
                }

                Write-Host "$(Get-Date -Format G) Successfully created compressed backup of $Vm"
            }
        
            ## If the compress command line switch is not configured.
            Else
            {
                ## Rename the export of each VM to include the date.
                Get-ChildItem -Path $Backup -Filter $Vm -Directory | Rename-Item -NewName ("$Backup\$Vm-{0:yyyy-MM-dd-HH-mm-ss}" -f (Get-Date))
            }

            ## Wait for 30 seconds before continuing just to be safe.
            Start-Sleep -S 30
        }
    }

    ## If the NoPerms command line option is not set.
    Else
    {
        ForEach ($Vm in $Vms)
        {
            ## Test for the existence of a previous VM export. If it exists then delete it, otherwise the export will fail.
            $VmExportBackupTest = Test-Path "$Backup\$Vm"
            If ($VmExportBackupTest -eq $True)
            {
                Remove-Item "$Backup\$Vm" -Recurse -Force
            }
        }

        ## Do a regular Hyper-V export of the VMs.
        $Vms | Export-VM -Path "$Backup"

        $VmExportTest = Test-Path "$Backup\*"
        If ($VmExportTest -eq $True)
        {
            If ($LogPath)
            {
                Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully exported specified VMs to $Backup"
            }

            Write-Host "$(Get-Date -Format G) Successfully exported specified VMs to $Backup"
        }

        Else
        {
            If ($LogPath)
            {
                Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem exporting the specified VMs to $Backup"
            }

            Write-Error "$(Get-Date -Format G) ERROR: There was a problem exporting the specified VMs to $Backup"
        }

        ## Loop through the VMs do perform operations for the keep and compress options, if configured.
        ForEach ($Vm in $Vms)
        {
            ## Check to see iif the keep option is not configured...
            If ($Null -eq $History)
            {
                ## ...and if the compress option is not configured.
                If ($Compress -eq $False)
                {
                    ## Remove all previous backup folders.
                    Get-ChildItem -Path $Backup -Filter "$Vm-*-*-*-*-*-*" -Directory | Remove-Item -Recurse -Force

                    ## For logging.
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing previous backup folders"
                    }

                    Write-Host "$(Get-Date -Format G) Removing previous backup folders"
                }
            }

            ## If the keep option is configured...
            Else
            {
                ## ...and if the compress option is not configured.
                If ($Compress -eq $False)
                {
                    ## Remove previous backup folders older than the configured number of days.
                    Get-ChildItem -Path $Backup -Filter "$Vm-*-*-*-*-*-*" -Directory | Where-Object CreationTime –lt (Get-Date).AddDays(-$History) | Remove-Item -Recurse -Force

                    ## For logging.
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing backup folders older than: $History days"
                    }

                    Write-Host "$(Get-Date -Format G) Removing backup folders older than: $History days"
                }
            }

            ## Check to see if the compress option is enabled...
            If ($Compress)
            {
                ## ...and if the keep option is not configured.
                If ($Null -eq $History)
                {
                    ## Remove all previous compressed backups.
                    Remove-Item "$Backup\$Vm-*-*-*-*-*-*.zip" -Force

                    ## For logging.
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing previous compressed backups"
                    }

                    Write-Host "$(Get-Date -Format G) Removing previous compressed backups"
                }

                ## If the keep option is configured.
                Else
                {
                    ## Remove previous compressed backups older than the configured number of days.
                    Get-ChildItem -Path "$Backup\$Vm-*-*-*-*-*-*.zip" | Where-Object CreationTime –lt (Get-Date).AddDays(-$History) | Remove-Item -Force

                    ## For logging.
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing compressed backups older than: $History days"
                    }

                    Write-Host "$(Get-Date -Format G) Removing compressed backups older than: $History days"
                }

                ## Compress the VM export folder into a zip, and delete the VM export folder.
                Add-Type -AssemblyName "system.io.compression.filesystem"
                [io.compression.zipfile]::CreateFromDirectory("$Backup\$Vm", "$Backup\$Vm-{0:yyyy-MM-dd-HH-mm-ss}.zip" -f (Get-Date))
                Get-ChildItem -Path $Backup -Filter "$Vm" -Directory | Remove-Item -Recurse -Force

                ## For logging.
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created compressed backup of $Vm"
                }

                Write-Host -Path $Log -Value "$(Get-Date -Format G) Successfully created compressed backup of $Vm"
            }
        
            ## If the compress option is not enabled.
            Else
            {
                ## Rename the export of each VM to include the date.
                Get-ChildItem -Path $Backup -Filter $Vm -Directory | Rename-Item -NewName ("$Backup\$Vm-{0:yyyy-MM-dd-HH-mm-ss}" -f (Get-Date))
            }
        }
    }
}

## If there are no VMs, then do nothing.
Else
{
    ## For Logging.
    If ($LogPath)
    {
        Add-Content -Path $Log -Value "$(Get-Date -Format G) There are no VMs running to backup"
    }

    Write-Host "$(Get-Date -Format G) There are no VMs running to backup"
}

## If log was configured stop the log.
If ($LogPath)
{
    Add-Content -Path $Log -Value ""
    Add-Content -Path $Log -Value "$(Get-Date -Format G) Log finished"
    Add-Content -Path $Log -Value "****************************************"

    ## If email was configured, set the variables for the email subject and body.
    If ($SmtpServer)
    {
        # If no subject is set, use the string below
        If ($Null -eq $MailSubject)
        {
            $MailSubject = "Hyper-V Backup"
        }

        $MailBody = Get-Content -Path $Log | Out-String

        ## If an email password was configured, create a variable with the username and password.
        If ($SmtpPwd)
        {
            $SmtpPwdEncrypt = Get-Content $SmtpPwd | ConvertTo-SecureString
            $SmtpCreds = New-Object System.Management.Automation.PSCredential -ArgumentList ($SmtpUser, $SmtpPwdEncrypt)

            ## If ssl was configured, send the email with ssl.
            If ($UseSsl)
            {
                Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -UseSsl -Credential $SmtpCreds
            }

            ## If ssl wasn't configured, send the email without ssl.
            Else
            {
                Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -Credential $SmtpCreds
            }
        }

        ## If an email username and password were not configured, send the email without authentication.
        Else
        {
            Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer
        }
    }
}

## End

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Windows 11上添加Hyper-V脚本,可以按照以下步骤操作: 1. 首先,打开Windows PowerShell。可以通过在开始菜单中搜索"PowerShell"并单击打开。 2. 在PowerShell窗口中,作为管理员运行该窗口以获取管理员权限。可以通过右键单击PowerShell图标并选择"以管理员身份运行"来实现。 3. 接下来,输入以下命令并按下Enter键: ``` Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All ``` 此命令将启用Hyper-V功能。它将在计算机上进行必要的更改以支持虚拟化。 4. 完成上述步骤后,您将需要重新启动计算机才能使更改生效。输入以下命令以执行计算机重启: ``` Restart-Computer ``` 确保您保存并关闭所有打开的应用程序和文件,然后按下Enter键。 5. 计算机将重新启动,并在重启后,Hyper-V将成功添加到您的Windows 11系统中。 请注意,要执行这些操作,您需要具备管理员权限。如果您没有管理员权限,则需要与系统管理员联系,或者通过其他方式获取管理员权限才能成功添加Hyper-V。 ### 回答2: 若要在Windows 11中添加Hyper-V脚本,可以按照以下步骤进行操作: 1. 打开PowerShell:在Windows 11中按下Win + X键,在弹出的菜单中选择“Windows PowerShell”。 2. 以管理员身份运行PowerShell:在弹出的菜单中选择“以管理员身份运行”。 3. 输入命令并运行脚本:在PowerShell窗口中,输入以下命令并按Enter键运行脚本。 ```powershell Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All ``` 4. 等待脚本执行完成:脚本执行期间,系统会进行Hyper-V组件的安装和配置,这可能需要一些时间。请耐心等待脚本执行完成。 5. 重启计算机:脚本执行完成后,系统可能要求您重新启动计算机以使更改生效。如果收到此提示,请按照指示重新启动计算机。 6. 检查Hyper-V是否已添加成功:重新启动计算机后,可以打开Hyper-V管理器或使用Powershell命令来验证Hyper-V是否已成功添加到Windows 11中。 通过以上步骤,您可以成功添加Hyper-V脚本到Windows 11中,并开始使用Hyper-V虚拟化功能。请注意,这个过程需要管理员权限才能执行。 ### 回答3: 要在Windows 11上添加Hyper-V脚本,您可以按照以下步骤进行操作: 1. 打开开始菜单并搜索“PowerShell”。右键单击Windows PowerShell图标,并选择“以管理员身份运行”选项,以确保具有管理员权限。 2. 在打开的Windows PowerShell窗口中,输入以下命令来启用Hyper-V: ```powershell Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All ``` 按下回车键执行该命令。 3. 等待一段时间,让Windows 11安装Hyper-V相关组件。完成后,你将看到一条成功的消息。 4. 重新启动计算机以使更改生效。您可以使用以下命令来重新启动计算机: ```powershell Restart-Computer ``` 进一步确认您的选择并重新启动计算机。 5. 在重新启动后,您可以通过使用以下命令来验证Hyper-V是否成功安装: ```powershell Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V ``` 如果安装已成功完成,您将看到“状态”列中显示为“已启用”。 6. 现在,您已经成功添加了Hyper-V脚本,并且可以使用Hyper-V管理工具来创建和管理虚拟机。 请注意,以上命令执行需要管理员权限,而且在一些特定的硬件配置上,可能需要启用BIOS中的虚拟化功能。确保您的计算机符合Hyper-V的要求并按照上述步骤进行操作,以确保成功添加Hyper-V

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值