Skip to content

Instantly share code, notes, and snippets.

@grenade
Last active March 19, 2018 19:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save grenade/10152444 to your computer and use it in GitHub Desktop.
Save grenade/10152444 to your computer and use it in GitHub Desktop.
create a new vm on hyper-v
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes
netsh advfirewall firewall set rule group="Network Discovery" new enable=Yes
param (
[Parameter(Mandatory=$true)]
[string] $computerName,
[Parameter(Mandatory=$true)]
[string] $sessionDomain,
[Parameter(Mandatory=$true)]
[string] $sessionUsername,
[Parameter(Mandatory=$true)]
[string] $sessionPassword,
[Parameter(Mandatory=$true)]
[string] $vmComputerName,
[string] $vmDescription = "",
[Parameter(Mandatory=$true)]
[ValidateSet("vmhost000.example.com", "vmhost001.example.com")]
[string] $hostComputerName,
[string] $cloud = "DevCloud",
[string] $iso = $null,
[string] $network = "VLAN137",
[string] $macAddress = "00:00:00:00:00:00",
[string] $vmLocation = "C:\ClusterStorage\Volume1\",
[string] $vmStartAction = "NeverAutoTurnOnVM",
[string] $vmStopAction = "SaveVM",
[string] $cpuType = "3.60 GHz Xeon (2 MB L2 cache)",
[string] $capabilityProfile = "Hyper-V with HA",
[string] $hardwareProfile = "Profile" + [guid]::NewGuid(),
[string] $operatingSystem = "64-bit edition of Windows Server 2012 Standard",
[ValidateSet("2008R2 Template", "2012 Template", "WebAppHostTemplate")]
[string] $template = "WebAppHostTemplate",
[string] $localAdminUsername = "Administrator",
[string] $localAdminPassword = $null,
[string] $workgroup = "WORKGROUP",
[string] $domainJoinDomain = $null,
[string] $domainJoinUsername = $null,
[string] $domainJoinPassword = $null
)
$cred = New-Object System.Management.Automation.PSCredential ("$sessionDomain\$sessionUsername", (ConvertTo-SecureString $sessionPassword -AsPlainText -Force))
Invoke-Command -ComputerName $computerName -Credential $cred -Script {
param (
[string] $vmComputerName,
[string] $vmDescription,
[string] $hostComputerName,
[string] $cloud,
[string] $iso,
[string] $network,
[string] $macAddress,
[string] $vmLocation,
[string] $vmStartAction,
[string] $vmStopAction,
[string] $cpuType,
[string] $capabilityProfile,
[string] $hardwareProfile,
[string] $operatingSystem,
[string] $template,
[string] $localAdminUsername,
[string] $localAdminPassword,
[string] $workgroup,
[string] $domainJoinDomain,
[string] $domainJoinUsername,
[string] $domainJoinPassword
)
Write-Host ("`$vmComputerName: $vmComputerName")
Write-Host ("`$hostComputerName: $hostComputerName")
$jobgroup = [guid]::NewGuid() #HardwareProfile
if ($iso) {
Write-Host "Import-SCLibraryPhysicalResource"
Import-SCLibraryPhysicalResource -VMMServer localhost -SourcePath ("E:\MSSCVMMLibrary\temp\{0}" -f $iso) -SharePath ("\\{0}.{1}\MSSCVMMLibrary\ISOs" -f $env:COMPUTERNAME, $env:USERDNSDOMAIN)
Write-Host "New-SCVirtualDVDDrive"
New-SCVirtualDVDDrive -VMMServer localhost -JobGroup $jobgroup -Bus 0 -LUN 1 -ISO (Get-SCISO -VMMServer localhost | where {$_.Name -eq $iso}) -Link
}
Write-Host "New-SCVirtualNetworkAdapter"
$vmNetwork = Get-SCVMNetwork -VMMServer localhost -Name $network
$vmSubnet = Get-SCVMSubnet -VMMServer localhost | where {$_.VMNetwork.ID -eq $vmNetwork.ID}
New-SCVirtualNetworkAdapter -VMMServer localhost -JobGroup $jobgroup -MACAddress $macAddress -MACAddressType Static -VLanEnabled $false -Synthetic -EnableVMNetworkOptimization $false -EnableMACAddressSpoofing $false -EnableGuestIPNetworkVirtualizationUpdates $false -IPv4AddressType Static -IPv6AddressType Dynamic -VMSubnet $vmSubnet -VMNetwork $vmNetwork
Write-Host "New-SCHardwareProfile"
New-SCHardwareProfile -VMMServer localhost -CPUType (Get-SCCPUType -VMMServer localhost | where {$_.Name -eq $cpuType}) -Name $hardwareProfile -Description "Profile used to create a VM/Template" -CPUCount 1 -MemoryMB 512 -DynamicMemoryEnabled $true -DynamicMemoryMinimumMB 512 -DynamicMemoryMaximumMB 4096 -DynamicMemoryBufferPercentage 10 -MemoryWeight 5000 -VirtualVideoAdapterEnabled $false -CPUExpectedUtilizationPercent 20 -DiskIops 0 -CPUMaximumPercent 100 -CPUReserve 0 -NumaIsolationRequired $false -NetworkUtilizationMbps 0 -CPURelativeWeight 100 -HighlyAvailable $true -HAVMPriority 2000 -DRProtectionRequired $false -NumLock $false -BootOrder "CD", "IdeHardDrive", "PxeBoot", "Floppy" -CPULimitFunctionality $false -CPULimitForMigration $false -CapabilityProfile (Get-SCCapabilityProfile -VMMServer localhost | where {$_.Name -eq $capabilityProfile}) -Generation 1 -JobGroup $jobgroup
Write-Host "New-SCVirtualDiskDrive"
New-SCVirtualDiskDrive -VMMServer localhost -IDE -Bus 0 -LUN 1 -JobGroup $jobgroup -VirtualHardDiskSizeMB 5120 -CreateDiffDisk $false -Dynamic -Filename ($vmComputerName + "_disk_1") -VolumeType None
$ttguid = [guid]::NewGuid() #Temporary Template
if ($domainJoinDomain -and $domainJoinUsername -and $domainJoinPassword) {
Write-Host "New-SCVMTemplate - Domain"
$domainJoinCredential = New-Object System.Management.Automation.PSCredential ("$domainJoinDomain\$domainJoinUsername", (ConvertTo-SecureString $domainJoinPassword -AsPlainText -Force))
New-SCVMTemplate -Name ("Temporary Template" + $ttguid) -Template (Get-SCVMTemplate | where {$_.Name -eq $template}) -HardwareProfile (Get-SCHardwareProfile | where {$_.Name -eq $hardwareProfile}) -JobGroup $jobgroup -ComputerName "*" -TimeZone 85 -LocalAdministratorCredential $null -FullName "" -OrganizationName "" -Domain $domainJoinDomain -DomainJoinCredential $domainJoinCredential -AnswerFile $null -OperatingSystem (Get-SCOperatingSystem | where {$_.Name -eq $operatingSystem})
} else {
Write-Host "New-SCVMTemplate - Workgroup"
if ($localAdminPassword) {
$localAdminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $localAdminUsername, (ConvertTo-SecureString -AsPlainText -String $localAdminPassword -Force)
} else {
$localAdminCredential = $null
}
New-SCVMTemplate -Name ("Temporary Template" + $ttguid) -Template (Get-SCVMTemplate | where {$_.Name -eq $template}) -HardwareProfile (Get-SCHardwareProfile | where {$_.Name -eq $hardwareProfile}) -JobGroup $jobgroup -ComputerName "*" -TimeZone 85 -LocalAdministratorCredential $localAdminCredential -FullName "" -OrganizationName "" -Workgroup $workgroup -AnswerFile $null -OperatingSystem (Get-SCOperatingSystem | where {$_.Name -eq $operatingSystem})
}
$vmConfig = New-SCVMConfiguration -VMTemplate (Get-SCVMTemplate -All | where { $_.Name -eq ("Temporary Template" + $ttguid) }) -Name $vmComputerName
Write-Output $vmConfig
switch($hostComputerName) {
"vmhost000.example.com" { $vmHost = Get-SCVMHost -ID "fde7ddc4-cc6b-479e-a405-11fb6e45c8cf" }
"vmhost001.example.com" { $vmHost = Get-SCVMHost -ID "efd70536-3662-4a96-a14c-2ff3b6b4b5d7" }
}
Write-Host "Set-SCVMConfiguration"
Set-SCVMConfiguration -VMConfiguration $vmConfig -VMHost $vmHost
Write-Host "Update-SCVMConfiguration"
Update-SCVMConfiguration -VMConfiguration $vmConfig
Write-Host "Set-SCVMConfiguration"
Set-SCVMConfiguration -VMConfiguration $vmConfig -VMLocation $vmLocation -PinVMLocation $true -ComputerName $vmComputerName
$AllNICConfigurations = Get-SCVirtualNetworkAdapterConfiguration -VMConfiguration $vmConfig
Write-Host "Update-SCVMConfiguration"
Update-SCVMConfiguration -VMConfiguration $vmConfig
if ($cloud) {
Write-Host "New-SCVirtualMachine - cloud"
$vm = New-SCVirtualMachine -Name $vmComputerName -VMConfiguration $vmConfig -Cloud (Get-SCCloud -Name $cloud) -Description $vmDescription -BlockDynamicOptimization $false -JobGroup "$jobgroup" -StartAction $vmStartAction -StopAction $vmStopAction
} else {
Write-Host "New-SCVirtualMachine - no cloud"
$vm = New-SCVirtualMachine -Name $vmComputerName -VMConfiguration $vmConfig -Description $vmDescription -BlockDynamicOptimization $false -JobGroup "$jobgroup" -StartAction $vmStartAction -StopAction $vmStopAction
}
} -ArgumentList $vmComputerName, $vmDescription, $hostComputerName, $cloud, $iso, $network, $macAddress, $vmLocation, $vmStartAction, $vmStopAction, $cpuType, $capabilityProfile, $hardwareProfile, $operatingSystem, $template, $localAdminUsername, $localAdminPassword, $workgroup, $domainJoinDomain, $domainJoinUsername, $domainJoinPassword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment