Windows/Powershell/Hyper V
From r00tedvw.com wiki
(Difference between revisions)
(→scvmm install vm) |
|||
(9 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
=Install= | =Install= | ||
− | To Install the Hyper-V Powershell Module | + | To Install the Hyper-V Powershell Module (Done on Windows 10 client) |
<nowiki>PS> Get-WindowsOptionalFeature -Online -FeatureName *hyper-v* | select DisplayName, FeatureName | <nowiki>PS> Get-WindowsOptionalFeature -Online -FeatureName *hyper-v* | select DisplayName, FeatureName | ||
Line 21: | Line 21: | ||
Online : True | Online : True | ||
RestartNeeded : False</nowiki> | RestartNeeded : False</nowiki> | ||
+ | |||
+ | |||
+ | =Scripts= | ||
+ | ==Clone VM== | ||
+ | Quick script to clone a VM based on an existing "master".<br> | ||
+ | issues with this method will be duplication of VM values, such as hostname, sid, etc. If using an Evaluation copy of windows, when the master expires, the clones wont work anymore, so rebuilding the master every 6 months is needed.<br> | ||
+ | Also, this uses VHD differencing, so the master cannot be powered on or it will break the rest of the vms created off it. | ||
+ | <nowiki>param( | ||
+ | [parameter(ParameterSetName="createvm")][int64]$Epoch = [long](Get-Date -Date ((Get-Date).ToUniversalTime()) -UFormat %s), | ||
+ | [parameter(ParameterSetName="createvm")][ValidateSet('Win2k16-Master','Win2k12r2-Master','Win2k16GVEUpgrade-Master','Win2k12r2GVEUpgrade-Master','Linux-Master')][string]$BaseImage = "Linux-Master", | ||
+ | [parameter(ParameterSetName="createvm")][string]$ComputerName = "win-45mo0eqvg4g", | ||
+ | [parameter(ParameterSetName="createvm")][string]$Name = -join($BaseImage, "-", $Epoch), | ||
+ | [parameter(ParameterSetName="createvm")][string]$Path = "C:\ProgramData\Microsoft\Windows\Hyper-V\", | ||
+ | [parameter(ParameterSetName="createvm")][string]$NewVHDPath = "C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\$Name.vhdx", | ||
+ | [parameter(ParameterSetName="createvm")][string]$NewVHDSizeBytes = 20GB, | ||
+ | [parameter(ParameterSetName="createvm")][int]$Generation = 1, | ||
+ | [parameter(ParameterSetName="createvm")][string]$MemoryStartupBytes = 1GB, | ||
+ | [parameter(ParameterSetName="createvm")][string]$SwitchName = "External_Virtual_Switch_01", | ||
+ | [parameter(ParameterSetName="createvm")][int]$ProcessorCount = 1, | ||
+ | [parameter(ParameterSetName="createvm")][int64]$MemoryMinimumBytes = 1073741824, | ||
+ | [parameter(ParameterSetName="createvm")][int64]$MemoryMaximumBytes = 2147483648, | ||
+ | [parameter(ParameterSetName="createvm")][string]$AutomaticStartAction = "Nothing", | ||
+ | [parameter(ParameterSetName="createvm")][int]$AutomaticStartDelay = 1, | ||
+ | [parameter(ParameterSetName="createvm")][string]$AutomaticStopAction = "Shutdown", | ||
+ | [parameter(ParameterSetName="createvm")][string]$Date = (Get-Date), | ||
+ | [parameter(ParameterSetName="createvm")][string]$Notes = "Created $Date", | ||
+ | [parameter(ParameterSetName="createvm")][string]$LogDir = "C:\logs" | ||
+ | ) | ||
+ | |||
+ | #Creates a new Virtual Machine | ||
+ | function CreateVM { | ||
+ | New-VM -ComputerName $ComputerName ` | ||
+ | -Name $Name ` | ||
+ | -Path $Path ` | ||
+ | -VHDPath $newVHDPath ` | ||
+ | -Generation $Generation ` | ||
+ | -MemoryStartupBytes $MemoryStartupBytes ` | ||
+ | -SwitchName $SwitchName | ||
+ | } | ||
+ | |||
+ | #Configures the new Virtual Machine | ||
+ | function ConfigureVM { | ||
+ | Set-VM -ComputerName $ComputerName ` | ||
+ | -Name $Name ` | ||
+ | -ProcessorCount $ProcessorCount ` | ||
+ | -DynamicMemory ` | ||
+ | -MemoryMinimumBytes $MemoryMinimumBytes ` | ||
+ | -MemoryMaximumBytes $MemoryMaximumBytes ` | ||
+ | -AutomaticStartAction $AutomaticStartAction ` | ||
+ | -AutomaticStartDelay $AutomaticStartDelay ` | ||
+ | -AutomaticStopAction $AutomaticStopAction ` | ||
+ | -Notes $Notes | ||
+ | } | ||
+ | |||
+ | #Deletes the new Virtual Machine (only used when Configuration fails) | ||
+ | function DeleteVM { | ||
+ | $disk = Get-VMHardDiskDrive -ComputerName $ComputerName -VMName $Name | Select-Object -ExpandProperty Path | ||
+ | Remove-Item $disk | ||
+ | Remove-VM -ComputerName $ComputerName -Name $Name -Force | ||
+ | } | ||
+ | |||
+ | #Verify that the desired Computer Name does not already exist | ||
+ | function ValidateName { | ||
+ | Get-VM -ComputerName $ComputerName | ForEach-Object { | ||
+ | if ($_.VMName -eq $Name) { | ||
+ | $script:LogOut = "$Date - Failed to create the VM with the name: $Name. A VM already exists with that name." | ||
+ | exit | ||
+ | } | ||
+ | else { | ||
+ | return | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #Clone the $BaseImage VHD using differencing | ||
+ | function CloneVHD { | ||
+ | #$srcVHD = -join((Split-Path -Path $NewVHDPath), "\", $BaseImage, ".vhdx") | ||
+ | $srcVHD = Get-VMHardDiskDrive -ComputerName $ComputerName -VMName $BaseImage | Select-Object -ExpandProperty Path | ||
+ | $newVHD = -join((Split-Path -Path $NewVHDPath), "\", $BaseImage, "-", $Epoch, ".vhdx") | ||
+ | New-VHD -Differencing -Path $newVHD -ParentPath $srcVHD | ||
+ | } | ||
+ | |||
+ | #Clear the Variables used | ||
+ | function ClearVariables { | ||
+ | #$Variables = Get-Variable | Where-Object {($_.Attributes -like "System.Management.Automation.ArgumentTypeConverterAttribute") -and ($_.Name -notin "ConfirmPreference","DebugPreference","ErrorActionPreference","InformationPreference","OutputEncoding","ProgressPreference","PSDefaultParameterValues","VerbosePreference","WarningPreference")} | Select-Object -ExpandProperty Name | ||
+ | $Variables = ("Epoch", "BaseImage", "ComputerName", "Name", "Path", "NewVHDPath", "NewVHDSizeBytes" , "Generation", "MemoryStartupBytes", "SwitchName", "ProcessorCount", "MemoryMinimumBytes", "MemoryMaximumBytes", "AutomaticStartAction", "AutomaticStartDelay", "AutomaticStopAction", "Date", "Notes") | ||
+ | foreach ($Variable in $Variables) { | ||
+ | Get-Variable $Variable | Clear-Variable -Force -ErrorAction SilentlyContinue | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #Verify Log directory exists and create if not | ||
+ | function VerifyLogDir { | ||
+ | if( -Not (Test-Path -Path $LogDir ) ) { | ||
+ | New-Item -ItemType directory -Path $LogDir -ErrorAction Stop | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #Set-PSDebug -Off | ||
+ | #Set-PSDebug -Trace 2 | ||
+ | |||
+ | try { | ||
+ | ValidateName -ErrorAction Stop | ||
+ | CloneVHD -ErrorAction stop | ||
+ | CreateVM -ErrorAction Stop | ||
+ | $script:LogOut = "$Date - Created VM $Name" | ||
+ | } | ||
+ | catch { | ||
+ | $ErrorMessage = $_.Exception.Message | ||
+ | $script:LogOut = "$Date - Failed to create the VM $Name. The error message was $ErrorMessage" | ||
+ | Break | ||
+ | } | ||
+ | finally { | ||
+ | echo $script:LogOut | Out-File $LogDir\vm-script.log -Append | ||
+ | } | ||
+ | |||
+ | try { | ||
+ | ConfigureVM -ErrorAction Stop | ||
+ | Start-VM -ComputerName $ComputerName -Name $Name -ErrorAction Stop | ||
+ | $script:LogOut = "$Date - Configured VM $Name" | ||
+ | } | ||
+ | catch { | ||
+ | $ErrorMessage = $_.Exception.Message | ||
+ | $script:LogOut = "$Date - Failed to configure the VM $Name. The error message was $ErrorMessage ---- Deleting VM $Name due to configuration failure." | ||
+ | DeleteVM | ||
+ | Break | ||
+ | } | ||
+ | finally { | ||
+ | echo $script:LogOut | Out-File $LogDir\vm-script.log -Append | ||
+ | } | ||
+ | |||
+ | #Get-Variable | ForEach-Object { $_.Name } | ||
+ | ClearVariables</nowiki> | ||
+ | |||
+ | ==scvmm create vm from template== | ||
+ | WIP, script to create virtual machines on a SCVMM host using templates. | ||
+ | <nowiki>param( | ||
+ | [parameter(ParameterSetName="createvm")][int64]$Epoch = [long](Get-Date -Date ((Get-Date).ToUniversalTime()) -UFormat %s), | ||
+ | [parameter(ParameterSetName="createvm")][ValidateSet('RNC-LAB-ENG Template','RNC-LAB-ENGINEERING TEMPLATE','RNCENG-LAB-CENTOS7','RNCENG-LAB-WIN2K16')][string]$TemplateName = "RNCENG-LAB-WIN2K16", | ||
+ | [parameter(ParameterSetName="createvm")][string]$VMName = -join($TemplateName, "-", $Epoch), | ||
+ | [parameter(ParameterSetName="createvm")][string]$Description = "GVE Test VM $BaseImage created on $( Get-Date )", | ||
+ | [parameter(ParameterSetName="createvm")][ValidateSet('AlwaysAutoTurnOnVM','NeverAutoTurnOnVM','TurnOnVMIfRunningWhenVSStopped')][string]$StartAction = "AlwaysAutoTurnOnVM", | ||
+ | [parameter(ParameterSetName="createvm")][ValidateSet('SaveVM','ShutdownGuestOS','TurnOffVM')][string]$StopAction = "TurnOffVM", | ||
+ | [parameter(ParameterSetName="createvm")][string]$SCVMMServer = "rnc-lab-scvmm1.r00tedvw.com", | ||
+ | [parameter(ParameterSetName="createvm")][string]$sccloud = "RNC-LAB-ENG", | ||
+ | [parameter(ParameterSetName="createvm")][string]$LogDir = "C:\logs", | ||
+ | [parameter(ParameterSetName="createvm")][string]$UserRoleName = "ENG Users" | ||
+ | ) | ||
+ | |||
+ | #Verify Connectivity to SCVMM Server | ||
+ | function VerifySCVMMServerConnectivity { | ||
+ | $scvmmport = 8100 | ||
+ | do { | ||
+ | $v ++ | ||
+ | if ($(Test-Connection -ComputerName $SCVMMserver -Count 1)) { | ||
+ | if($(Get-SCVMMServer -ComputerName $SCVMMserver -TCPPort $scvmmport | Where-Object { ($_.IsConnected -match "True") } )){ | ||
+ | $v=6 | ||
+ | $script:LogOut += "$(Get-Date) - Connected to scvmm server: $SCVMMserver" | ||
+ | } else { | ||
+ | Get-SCVMMServer -ComputerName $SCVMMserver -TCPPort $scvmmport | ||
+ | } | ||
+ | } else { | ||
+ | $script:LogOut += "$(Get-Date) - Unable to connect to scvmm server: $SCVMMserver" | ||
+ | } | ||
+ | } | ||
+ | Until ($v -gt 4) | ||
+ | } | ||
+ | |||
+ | #Verify Log directory exists and create if not | ||
+ | function VerifyVMMmodule { | ||
+ | if( -Not (Get-Module -ListAvailable -Name virtualmachinemanager) ) { | ||
+ | Import-Module virtualmachinemanager | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #Creates a new Virtual Machine from a Template | ||
+ | function CreateVM { | ||
+ | $template = (Get-SCVMTemplate -VMMServer $SCVMMServer | Where-Object {$_.Name -eq $TemplateName}) | ||
+ | $Cloud = (Get-sccloud | Where-Object {$_.Name -eq $sccloud}) | ||
+ | $virtualmachineconfiguration = (New-SCVMConfiguration -Cloud $Cloud -VMTemplate $Template -Name $VMName) | ||
+ | if ($VMName -like "*WIN2K16*") { | ||
+ | $OperatingSystem = "Windows Server 2016 Standard" | ||
+ | } elseif ($VMName -like "*WIN2K12*") { | ||
+ | $OperatingSystem = "Windows Server 2012 Standard" | ||
+ | } elseif ($VMName -like "*centos*") { | ||
+ | $OperatingSystem = "CentOS Linux 7 (64 bit)" | ||
+ | } else { | ||
+ | $OperatingSystem = "Other (64 bit)" | ||
+ | } | ||
+ | |||
+ | New-SCVirtualMachine -Name $VMName ` | ||
+ | -VMConfiguration $VirtualMachineConfiguration ` | ||
+ | -Cloud $Cloud ` | ||
+ | -Description $Description ` | ||
+ | -StartAction $StartAction ` | ||
+ | -StopAction $StopAction ` | ||
+ | -OperatingSystem $OperatingSystem ` | ||
+ | -ReturnImmediately ` | ||
+ | -RunAsynchronously ` | ||
+ | -JobVariable "CreateVMJob" ` | ||
+ | -ErrorAction Stop | ||
+ | } | ||
+ | |||
+ | #Verify that the desired Computer Name does not already exist | ||
+ | function ValidateName { | ||
+ | Get-SCVirtualMachine -VMMServer $SCVMMServer -ErrorAction Stop | ForEach-Object { | ||
+ | if ($_.VMName -eq $VMName) { | ||
+ | $script:LogOut += "$(Get-Date) - Failed to create the VM with the name: $VMName. A VM already exists with that name." | ||
+ | exit | ||
+ | } | ||
+ | else { | ||
+ | return | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #Verify Log directory exists and create if not | ||
+ | function VerifyLogDir { | ||
+ | if( -Not (Test-Path -Path $LogDir ) ) { | ||
+ | New-Item -ItemType directory -Path $LogDir -ErrorAction Stop | ||
+ | } | ||
+ | } | ||
+ | |||
+ | #Grant Role to vm | ||
+ | function GrantRole { | ||
+ | Grant-SCResource -VMMServer $SCVMMServer -UserRoleName $UserRoleName -Resource $(Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName) -ErrorAction Stop | ||
+ | } | ||
+ | |||
+ | #Delete VM | ||
+ | function DeleteVM { | ||
+ | Remove-SCVirtualMachine -VM $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName ) -ErrorAction Stop | ||
+ | } | ||
+ | |||
+ | #Set-PSDebug -Off | ||
+ | #Set-PSDebug -Trace 2 | ||
+ | |||
+ | #Validate the Module, Name, and Log Directory, then Create the VM | ||
+ | try { | ||
+ | $script:LogOut = @() | ||
+ | VerifyVMMmodule | ||
+ | VerifySCVMMServerConnectivity | ||
+ | ValidateName | ||
+ | VerifyLogDir | ||
+ | CreateVM | Out-Null | ||
+ | $script:LogOut += "$(Get-Date) - Created VM $VMName" | ||
+ | } | ||
+ | catch { | ||
+ | $ErrorMessage = $_.Exception.Message | ||
+ | $script:LogOut += "$(Get-Date) - Failed to create the VM $VMName. The error message was $ErrorMessage" | ||
+ | Break | ||
+ | } | ||
+ | finally { | ||
+ | Write-Output "`n--------------------------------------------------" | Out-File $LogDir\vm-script.log -Append | ||
+ | foreach ($element in $script:LogOut){ | ||
+ | Write-Output $element | Out-File $LogDir\vm-script.log -Append | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | #Wait until vm creation is complete, then Configure VM and Start | ||
+ | try { | ||
+ | $script:LogOut = @() | ||
+ | do { | ||
+ | Write-Progress -Activity "Creating Virtual Machine" | ||
+ | Start-Sleep 3 | ||
+ | } Until ($(Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName | Where-Object { ($_.Status -notmatch "UnderCreation") } )) | ||
+ | GrantRole | Out-Null | ||
+ | Start-SCVirtualMachine -VM $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName ) | Out-Null | ||
+ | #Waiting 10 seconds and refreshing vm details so that IP info is populated. | ||
+ | Start-Sleep 10 | ||
+ | Read-SCVirtualMachine -VM $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName ) | Out-Null | ||
+ | $script:LogOut += "$(Get-Date) - Granted access for $UserRoleName to $VMName `n$(Get-Date) - IP Address for $VMName is $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName | Select-Object -ExpandProperty "VirtualNetworkAdapters" | Select-Object -ExpandProperty IPv4Addresses )" | ||
+ | } | ||
+ | catch { | ||
+ | $ErrorMessage = $_.Exception.Message | ||
+ | $script:LogOut += "$(Get-Date) - Failed to configure the VM $VMName, deleting. The error message was $ErrorMessage" | ||
+ | do { | ||
+ | Write-Progress -Activity "Failed to configure virtual machine, waiting to delete" | ||
+ | Start-Sleep 3 | ||
+ | } Until ($(Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName | Where-Object { ($_.Status -notmatch "UnderCreation") } )) | ||
+ | Stop-SCVirtualMachine -VM $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName ) -ErrorAction Ignore | ||
+ | DeleteVM | ||
+ | } | ||
+ | finally { | ||
+ | foreach ($element in $script:LogOut){ | ||
+ | Write-Output $element | Out-File $LogDir\vm-script.log -Append | ||
+ | } | ||
+ | }</nowiki> |
Latest revision as of 14:59, 7 February 2019
Contents |
[edit] Install
To Install the Hyper-V Powershell Module (Done on Windows 10 client)
PS> Get-WindowsOptionalFeature -Online -FeatureName *hyper-v* | select DisplayName, FeatureName DisplayName FeatureName ----------- ----------- Hyper-V Microsoft-Hyper-V-All Hyper-V Platform Microsoft-Hyper-V Hyper-V Management Tools Microsoft-Hyper-V-Tools-All Hyper-V Module for Windows PowerShell Microsoft-Hyper-V-Management-PowerShell Hyper-V Hypervisor Microsoft-Hyper-V-Hypervisor Hyper-V Services Microsoft-Hyper-V-Services Hyper-V GUI Management Tools Microsoft-Hyper-V-Management-Clients PS> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Management-PowerShell Path : Online : True RestartNeeded : False
[edit] Scripts
[edit] Clone VM
Quick script to clone a VM based on an existing "master".
issues with this method will be duplication of VM values, such as hostname, sid, etc. If using an Evaluation copy of windows, when the master expires, the clones wont work anymore, so rebuilding the master every 6 months is needed.
Also, this uses VHD differencing, so the master cannot be powered on or it will break the rest of the vms created off it.
param( [parameter(ParameterSetName="createvm")][int64]$Epoch = [long](Get-Date -Date ((Get-Date).ToUniversalTime()) -UFormat %s), [parameter(ParameterSetName="createvm")][ValidateSet('Win2k16-Master','Win2k12r2-Master','Win2k16GVEUpgrade-Master','Win2k12r2GVEUpgrade-Master','Linux-Master')][string]$BaseImage = "Linux-Master", [parameter(ParameterSetName="createvm")][string]$ComputerName = "win-45mo0eqvg4g", [parameter(ParameterSetName="createvm")][string]$Name = -join($BaseImage, "-", $Epoch), [parameter(ParameterSetName="createvm")][string]$Path = "C:\ProgramData\Microsoft\Windows\Hyper-V\", [parameter(ParameterSetName="createvm")][string]$NewVHDPath = "C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\$Name.vhdx", [parameter(ParameterSetName="createvm")][string]$NewVHDSizeBytes = 20GB, [parameter(ParameterSetName="createvm")][int]$Generation = 1, [parameter(ParameterSetName="createvm")][string]$MemoryStartupBytes = 1GB, [parameter(ParameterSetName="createvm")][string]$SwitchName = "External_Virtual_Switch_01", [parameter(ParameterSetName="createvm")][int]$ProcessorCount = 1, [parameter(ParameterSetName="createvm")][int64]$MemoryMinimumBytes = 1073741824, [parameter(ParameterSetName="createvm")][int64]$MemoryMaximumBytes = 2147483648, [parameter(ParameterSetName="createvm")][string]$AutomaticStartAction = "Nothing", [parameter(ParameterSetName="createvm")][int]$AutomaticStartDelay = 1, [parameter(ParameterSetName="createvm")][string]$AutomaticStopAction = "Shutdown", [parameter(ParameterSetName="createvm")][string]$Date = (Get-Date), [parameter(ParameterSetName="createvm")][string]$Notes = "Created $Date", [parameter(ParameterSetName="createvm")][string]$LogDir = "C:\logs" ) #Creates a new Virtual Machine function CreateVM { New-VM -ComputerName $ComputerName ` -Name $Name ` -Path $Path ` -VHDPath $newVHDPath ` -Generation $Generation ` -MemoryStartupBytes $MemoryStartupBytes ` -SwitchName $SwitchName } #Configures the new Virtual Machine function ConfigureVM { Set-VM -ComputerName $ComputerName ` -Name $Name ` -ProcessorCount $ProcessorCount ` -DynamicMemory ` -MemoryMinimumBytes $MemoryMinimumBytes ` -MemoryMaximumBytes $MemoryMaximumBytes ` -AutomaticStartAction $AutomaticStartAction ` -AutomaticStartDelay $AutomaticStartDelay ` -AutomaticStopAction $AutomaticStopAction ` -Notes $Notes } #Deletes the new Virtual Machine (only used when Configuration fails) function DeleteVM { $disk = Get-VMHardDiskDrive -ComputerName $ComputerName -VMName $Name | Select-Object -ExpandProperty Path Remove-Item $disk Remove-VM -ComputerName $ComputerName -Name $Name -Force } #Verify that the desired Computer Name does not already exist function ValidateName { Get-VM -ComputerName $ComputerName | ForEach-Object { if ($_.VMName -eq $Name) { $script:LogOut = "$Date - Failed to create the VM with the name: $Name. A VM already exists with that name." exit } else { return } } } #Clone the $BaseImage VHD using differencing function CloneVHD { #$srcVHD = -join((Split-Path -Path $NewVHDPath), "\", $BaseImage, ".vhdx") $srcVHD = Get-VMHardDiskDrive -ComputerName $ComputerName -VMName $BaseImage | Select-Object -ExpandProperty Path $newVHD = -join((Split-Path -Path $NewVHDPath), "\", $BaseImage, "-", $Epoch, ".vhdx") New-VHD -Differencing -Path $newVHD -ParentPath $srcVHD } #Clear the Variables used function ClearVariables { #$Variables = Get-Variable | Where-Object {($_.Attributes -like "System.Management.Automation.ArgumentTypeConverterAttribute") -and ($_.Name -notin "ConfirmPreference","DebugPreference","ErrorActionPreference","InformationPreference","OutputEncoding","ProgressPreference","PSDefaultParameterValues","VerbosePreference","WarningPreference")} | Select-Object -ExpandProperty Name $Variables = ("Epoch", "BaseImage", "ComputerName", "Name", "Path", "NewVHDPath", "NewVHDSizeBytes" , "Generation", "MemoryStartupBytes", "SwitchName", "ProcessorCount", "MemoryMinimumBytes", "MemoryMaximumBytes", "AutomaticStartAction", "AutomaticStartDelay", "AutomaticStopAction", "Date", "Notes") foreach ($Variable in $Variables) { Get-Variable $Variable | Clear-Variable -Force -ErrorAction SilentlyContinue } } #Verify Log directory exists and create if not function VerifyLogDir { if( -Not (Test-Path -Path $LogDir ) ) { New-Item -ItemType directory -Path $LogDir -ErrorAction Stop } } #Set-PSDebug -Off #Set-PSDebug -Trace 2 try { ValidateName -ErrorAction Stop CloneVHD -ErrorAction stop CreateVM -ErrorAction Stop $script:LogOut = "$Date - Created VM $Name" } catch { $ErrorMessage = $_.Exception.Message $script:LogOut = "$Date - Failed to create the VM $Name. The error message was $ErrorMessage" Break } finally { echo $script:LogOut | Out-File $LogDir\vm-script.log -Append } try { ConfigureVM -ErrorAction Stop Start-VM -ComputerName $ComputerName -Name $Name -ErrorAction Stop $script:LogOut = "$Date - Configured VM $Name" } catch { $ErrorMessage = $_.Exception.Message $script:LogOut = "$Date - Failed to configure the VM $Name. The error message was $ErrorMessage ---- Deleting VM $Name due to configuration failure." DeleteVM Break } finally { echo $script:LogOut | Out-File $LogDir\vm-script.log -Append } #Get-Variable | ForEach-Object { $_.Name } ClearVariables
[edit] scvmm create vm from template
WIP, script to create virtual machines on a SCVMM host using templates.
param( [parameter(ParameterSetName="createvm")][int64]$Epoch = [long](Get-Date -Date ((Get-Date).ToUniversalTime()) -UFormat %s), [parameter(ParameterSetName="createvm")][ValidateSet('RNC-LAB-ENG Template','RNC-LAB-ENGINEERING TEMPLATE','RNCENG-LAB-CENTOS7','RNCENG-LAB-WIN2K16')][string]$TemplateName = "RNCENG-LAB-WIN2K16", [parameter(ParameterSetName="createvm")][string]$VMName = -join($TemplateName, "-", $Epoch), [parameter(ParameterSetName="createvm")][string]$Description = "GVE Test VM $BaseImage created on $( Get-Date )", [parameter(ParameterSetName="createvm")][ValidateSet('AlwaysAutoTurnOnVM','NeverAutoTurnOnVM','TurnOnVMIfRunningWhenVSStopped')][string]$StartAction = "AlwaysAutoTurnOnVM", [parameter(ParameterSetName="createvm")][ValidateSet('SaveVM','ShutdownGuestOS','TurnOffVM')][string]$StopAction = "TurnOffVM", [parameter(ParameterSetName="createvm")][string]$SCVMMServer = "rnc-lab-scvmm1.r00tedvw.com", [parameter(ParameterSetName="createvm")][string]$sccloud = "RNC-LAB-ENG", [parameter(ParameterSetName="createvm")][string]$LogDir = "C:\logs", [parameter(ParameterSetName="createvm")][string]$UserRoleName = "ENG Users" ) #Verify Connectivity to SCVMM Server function VerifySCVMMServerConnectivity { $scvmmport = 8100 do { $v ++ if ($(Test-Connection -ComputerName $SCVMMserver -Count 1)) { if($(Get-SCVMMServer -ComputerName $SCVMMserver -TCPPort $scvmmport | Where-Object { ($_.IsConnected -match "True") } )){ $v=6 $script:LogOut += "$(Get-Date) - Connected to scvmm server: $SCVMMserver" } else { Get-SCVMMServer -ComputerName $SCVMMserver -TCPPort $scvmmport } } else { $script:LogOut += "$(Get-Date) - Unable to connect to scvmm server: $SCVMMserver" } } Until ($v -gt 4) } #Verify Log directory exists and create if not function VerifyVMMmodule { if( -Not (Get-Module -ListAvailable -Name virtualmachinemanager) ) { Import-Module virtualmachinemanager } } #Creates a new Virtual Machine from a Template function CreateVM { $template = (Get-SCVMTemplate -VMMServer $SCVMMServer | Where-Object {$_.Name -eq $TemplateName}) $Cloud = (Get-sccloud | Where-Object {$_.Name -eq $sccloud}) $virtualmachineconfiguration = (New-SCVMConfiguration -Cloud $Cloud -VMTemplate $Template -Name $VMName) if ($VMName -like "*WIN2K16*") { $OperatingSystem = "Windows Server 2016 Standard" } elseif ($VMName -like "*WIN2K12*") { $OperatingSystem = "Windows Server 2012 Standard" } elseif ($VMName -like "*centos*") { $OperatingSystem = "CentOS Linux 7 (64 bit)" } else { $OperatingSystem = "Other (64 bit)" } New-SCVirtualMachine -Name $VMName ` -VMConfiguration $VirtualMachineConfiguration ` -Cloud $Cloud ` -Description $Description ` -StartAction $StartAction ` -StopAction $StopAction ` -OperatingSystem $OperatingSystem ` -ReturnImmediately ` -RunAsynchronously ` -JobVariable "CreateVMJob" ` -ErrorAction Stop } #Verify that the desired Computer Name does not already exist function ValidateName { Get-SCVirtualMachine -VMMServer $SCVMMServer -ErrorAction Stop | ForEach-Object { if ($_.VMName -eq $VMName) { $script:LogOut += "$(Get-Date) - Failed to create the VM with the name: $VMName. A VM already exists with that name." exit } else { return } } } #Verify Log directory exists and create if not function VerifyLogDir { if( -Not (Test-Path -Path $LogDir ) ) { New-Item -ItemType directory -Path $LogDir -ErrorAction Stop } } #Grant Role to vm function GrantRole { Grant-SCResource -VMMServer $SCVMMServer -UserRoleName $UserRoleName -Resource $(Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName) -ErrorAction Stop } #Delete VM function DeleteVM { Remove-SCVirtualMachine -VM $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName ) -ErrorAction Stop } #Set-PSDebug -Off #Set-PSDebug -Trace 2 #Validate the Module, Name, and Log Directory, then Create the VM try { $script:LogOut = @() VerifyVMMmodule VerifySCVMMServerConnectivity ValidateName VerifyLogDir CreateVM | Out-Null $script:LogOut += "$(Get-Date) - Created VM $VMName" } catch { $ErrorMessage = $_.Exception.Message $script:LogOut += "$(Get-Date) - Failed to create the VM $VMName. The error message was $ErrorMessage" Break } finally { Write-Output "`n--------------------------------------------------" | Out-File $LogDir\vm-script.log -Append foreach ($element in $script:LogOut){ Write-Output $element | Out-File $LogDir\vm-script.log -Append } } #Wait until vm creation is complete, then Configure VM and Start try { $script:LogOut = @() do { Write-Progress -Activity "Creating Virtual Machine" Start-Sleep 3 } Until ($(Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName | Where-Object { ($_.Status -notmatch "UnderCreation") } )) GrantRole | Out-Null Start-SCVirtualMachine -VM $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName ) | Out-Null #Waiting 10 seconds and refreshing vm details so that IP info is populated. Start-Sleep 10 Read-SCVirtualMachine -VM $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName ) | Out-Null $script:LogOut += "$(Get-Date) - Granted access for $UserRoleName to $VMName `n$(Get-Date) - IP Address for $VMName is $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName | Select-Object -ExpandProperty "VirtualNetworkAdapters" | Select-Object -ExpandProperty IPv4Addresses )" } catch { $ErrorMessage = $_.Exception.Message $script:LogOut += "$(Get-Date) - Failed to configure the VM $VMName, deleting. The error message was $ErrorMessage" do { Write-Progress -Activity "Failed to configure virtual machine, waiting to delete" Start-Sleep 3 } Until ($(Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName | Where-Object { ($_.Status -notmatch "UnderCreation") } )) Stop-SCVirtualMachine -VM $( Get-SCVirtualMachine -VMMServer $SCVMMServer -Name $VMName ) -ErrorAction Ignore DeleteVM } finally { foreach ($element in $script:LogOut){ Write-Output $element | Out-File $LogDir\vm-script.log -Append } }