• No results found

Appendix B: Windows PowerShell scripting

DISCLAIMER OF WARRANTY

This document may contain the following HP or other software: XML, CLI statements, scripts, parameter files. These are provided as a courtesy, free of charge, “AS-IS” by Hewlett-Packard Company (“HP”). HP shall have no obligation to maintain or support this software. HP MAKES NO EXPRESS OR IMPLIED WARRANTY OF ANY KIND REGARDING THIS SOFTWARE INCLUDING ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE OR NON-INFRINGEMENT.

HP SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, WHETHER BASED ON CONTRACT, TORT OR ANY OTHER LEGAL THEORY, IN CONNECTION WITH OR ARISING OUT OF THE FURNISHING, PERFORMANCE OR USE OF THIS SOFTWARE.

Scripts

Create a virtual machine

# This script will read a comma-separated values (CSV) file to create VMs.

# The CSV file is specified in $CSVPath. A log file is created by this script.

# This log file is specified in $LogPath.

#

# The script expects to see a CSV file with a header row that specifies the

# column names (variables).

# Each row after that will define a new VM to create. The variables are as

# follows:

#

# VM_NAME: The name of the new VM to create

# VM_Path: Where to create the VM's files

# Disk_Type: Differencing, Fixed, or Dynamic (the default). No value = Dynamic.

# DiffParent: Used when Disk_Type = Dynamic. Specifies the parent for the

# differencing disk

# Disk_Size: Used if Disk_Type = Fixed or Dynamic (or blank). Size in GB for the

# new VHDX

# ProcessorCount: How many vCPUs the VM will have

# StartupRAM: Amount in MB that the VM will boot up with (Dynamic Memory or not)

# DynamicMemory: Yes or No (default). Do you enable DM or not?

# MinRAM: Amount in MB for Minimum RAM if DM is enabled.

# MaxRAM: Amount in MB for Maximum RAM if DM is enabled.

# MemPriority: 0-100 value if for Memory Weight DM is enabled

# MemBuffer: 5-2000 value for Memory Buffer is DM is enabled

# StaticMAC: Any non-Null value will be used as a MAC address. No error

# detection.

# AddToCluster: The new VM will be added to the specified cluster if not blank.

# Start: If set to Yes, then the VM will be started up.

38

# Clear the screen cls

$CSVPath = "\\demo-sofs1\CSV1\VMs.txt"

$LogPath = "C:\Scripts\VMsLog.txt"

# Remove the log file

Remove-Item -Path $LogPath -ErrorAction SilentlyContinue Import-Csv $CSVPath | ForEach-Object {

# Construct some paths

$Path = $_.VM_Path

$VM_NAME = $_.VM_NAME

$VHDPath = "$Path\$VM_NAME"

Add-Content $LogPath "Beginning: Creating $VM_NAME."

# Only create the virtual machine if it does not already exist if ((Get-VM $VM_NAME -ErrorAction SilentlyContinue)) {

Add-Content $LogPath "FAIL: $VM_NAME already existed."

} else {

# Create a new folder for the VM if it does not already exist if (!(Test-Path $VHDPath))

{

New-Item -Path $VHDPath -ItemType "Directory"

}

# Create a new folder for the VHD if it does not already exist if (!(Test-Path "$VHDPath\Virtual Hard Disks"))

{

$VhdDir = New-Item -Path "$VHDPath\Virtual Hard Disks" -ItemType ' "Directory"

}

# Create the VHD if it does not already exist $NewVHD = "$VhdDir\$VM_NAME-Disk0.vhd"

if (!(Test-Path $NewVHD)) {

# Have to set these variables because $_.Variables are not available # inside the switch.

$ParentDisk = $_.DiffParent

$Disk_Size = [int64]$_.Disk_Size * 1073741824 switch ($_.Disk_Type)

{

'Differencing' {New-VHD -Differencing -Path $NewVHD -ParentPath ' $ParentDisk}

'Fixed' {New-VHD -Fixed -Path $NewVHD -SizeBytes $Disk_Size}

Default {New-VHD -Dynamic -Path $NewVHD -SizeBytes $Disk_Size}

}

if (Test-Path $NewVHD) {

Add-Content $LogPath " Progress: $NewVHD was created."

} else {

Add-Content $LogPath " Error: $NewVHD was not created."

} } else {

Add-Content $LogPath " Progress: $NewVHD already existed"

}

# Create the VM

New-VM -Name $_.VM_NAME -Path $Path -SwitchName ConvergedNetSwitch -VHDPath ' $NewVHD -MemoryStartupBytes ([int64]$_.StartupRam * 1MB)

# Is the VM there and should we continue?

if ((Get-VM $VM_NAME -ErrorAction SilentlyContinue)) {

Add-Content $LogPath " Progress: The VM was created."

# Configure the processors

Set-VMProcessor $_.VM_NAME -Count $_.ProcessorCount -ErrorAction ' SilentlyContinue

If ((Get-VMProcessor $_.VM_NAME).count -eq $_.ProcessorCount) {

Add-Content $LogPath " Progress: Configured processor count."

} else {

Add-Content $LogPath " ERROR: Processor count was not configured."

}

# Configure Dynamic Memory if required If ($_.DynamicMemory -Eq "Yes") {

40

Set-VMMemory -VM_NAME $_.VM_NAME -DynamicMemoryEnabled $True ' -MaximumBytes ([int64]$_.MaxRAM * 1MB) -MinimumBytes ([int64]$_. ' MinRAM * 1MB) -Priority $_.MemPriority -Buffer $_.MemBuffer

If ((Get-VMMemory $_.VM_NAME).DynamicMemoryEnabled -eq $True) {

Add-Content $LogPath " Progress: Dynamic Memory was set."

} else {

Add-Content $LogPath " ERROR: Dynamic Memory was not set."

} }

# Is a static MAC Address required?

If ($_.StaticMAC -ne $NULL) {

Set-VMNetworkAdapter $_.VM_NAME -StaticMacAddress $_.StaticMAC ' -ErrorAction SilentlyContinue

If ((Get-VMNetworkAdapter $_.VM_NAME).MacAddress -eq $_.StaticMAC) {

Add-Content $LogPath " Progress: Static MAC address set."

} else {

Add-Content $LogPath " ERROR: Static MAC address was not set."

} }

Add the VM to the cluster?

$ClusterName = $_.AddToCluster If ($ClusterName -ne $NULL) {

If (Add-ClusterVirtualMachineRole -Cluster $_.AddToCluster -VM_NAME ' $_.VM_NAME -ErrorAction SilentlyContinue)

{

Add-Content $LogPath " Progress: Added VM to $ClusterName ' cluster."

} else {

Add-Content $LogPath " ERROR: Did not add VM to $ClusterName ' cluster."

} }

# Start the VM?

If ($_.Start -eq "Yes") {

Start-VM $_.VM_NAME -ErrorAction SilentlyContinue If ((Get-VM $_.VM_NAME).State -eq "Running") {

Add-Content $LogPath " Progress: Started the VM."

} else {

Add-Content $LogPath " ERROR: Did not start the VM."

} }

# End of "Is the VM there and should we continue?"

Add-Content $LogPath "Success: $VM_NAME was created."

} else {

Add-Content $LogPath "FAIL: $VM_NAME was created."

}

} }

Commands

Failover Cluster PowerShell commands

This first line adds the failover cluster role to the host HV-Node01.

Add-WindowsFeature Failover-Clustering –IncludeManagementTools –ComputerName HV-Node01

To create a cluster without any configured storage:

New-Cluster –Name Cluster1 –Node Blade1, Blade2, Blade3, Blade4, Blade5, Blade6, Blade7 –StaticAddress 10.0.0.20 -NoStorage

To install management tools use the parameter -IncludeManagementTools:

Add-WindowsFeature RSAT-Clustering –IncludeAllSubFeature To run the cluster validation process:

Test-Cluster –Node Host1, Host2, Host3 To add a disk to a cluster:

Add-ClusterDisk "Cluster Disk 1"

42

To get a list of available disks:

Get-ClusterAvailableDisks | fl Name Verify Cluster Shared Volumes:

Get-CLusterSharedVolume "Cluster Disk Name" | fl Live Migration PowerShell commands

To initiate Live Storage Migration by using PowerShell:

Move-VMStorage -VMName Exch-VM1 -DestinationStoragePath "\\Server Name\VM-Store\

'

Exch-VM1"

To setup Shared Storage:

MD '<LOCAL_DRIVE_PATH>\VM-Store'

New-SmbShare -Name VM-Store -Path "<LOCAL_DRIVE_PATH>\VM-Store"' -FullAccess domain\administrator, \host1$, domain\host2$, domain\host3$(Get-SmbShare -Name VM-Store).PresetPathAcl | Set-Acl.

Related documents