I want to use a PowerShell script, to each VM whose name starts with W10PRD, to add a 10 Gb disk that will be used as a persistent disk in an MCS scenario and expand the RAM to 16 Gb and the vCPU to 4.
I created the script below for this purpose. This script works fine until creating the disks. So things start to go wrong from line 76 onwards.
I then get this error: Error occurred while processing VM W10PRD1003: Cannot bind parameter 'SR'. Cannot convert the "@{uuid=<UUID of Local Storage SR>; name_label=Local storage}" value of type "Selected.XenAPI.SR" to type "XenAPI.XenRef`1[XenAPI.SR]".
foreach ($vm in $vms) {
# Create a new virtual disk (VDI)
try {
$vdi = New-XenVDI -NameLabel $diskLabel -VirtualSize $diskSize -SR $storageRepoUUID -ReadOnly $false
if ($vdi -eq $null) {
throw "Failed to create VDI for VM: $($vm.name_label)"
}
# Create a virtual disk drive (VBD) connecting the VDI to the VM
$vbd = New-XenVBD -VM $vm.uuid -VDI $vdi.uuid -Device 2 -Bootable $false -Mode RW -Type Disk
if ($vbd -ne $null) {
Write-Output "Added a 2GB disk to VM: $($vm.name_label) on repository: $storageRepoName"
} else {
Write-Output "Failed to attach VDI to VM: $($vm.name_label)"
}
} catch {
Write-Output "Error occurred while processing VM $($vm.name_label): $($_.Exception.Message)"
}
}
# Close XenServer Connection
Disconnect-XenServer
Who knows what is wrong with this script, or knows a better option for creating 400 disks on 400 VMs?
Question
Marcel Zunnebeld1709163163
Hello,
I want to use a PowerShell script, to each VM whose name starts with W10PRD, to add a 10 Gb disk that will be used as a persistent disk in an MCS scenario and expand the RAM to 16 Gb and the vCPU to 4.
I created the script below for this purpose. This script works fine until creating the disks. So things start to go wrong from line 76 onwards.
I then get this error:
Error occurred while processing VM W10PRD1003: Cannot bind parameter 'SR'. Cannot convert the "@{uuid=<UUID of Local Storage SR>; name_label=Local storage}" value of type "Selected.XenAPI.SR" to type "XenAPI.XenRef`1[XenAPI.SR]".
# Variables
$hostName = "xenserver host name"
$Username = "admin_account"
$Password = "admin_password"
$vmPrefix = "W10PRD"
$storageRepoName = "Local storage"
$diskSize = 10GB # 10 GB disk
$diskLabel = "Persistent-disk"
$RAMinGB = 16
$RAMinBytes = $RAMinGB*1073741824
$vCPU = 4
$vCPUatStartup = 4
$vCPUMax = 4
# Import XenServer module and connect
Import-Module XenServerPSModule
Try {
$Session = Connect-XenServer -Url https://$hostName -UserName $username -Password $password -NoWarnCertificates -SetDefaultSession
} Catch [XenAPI.Failure] {
[string]$PoolMaster = $_.Exception.ErrorDescription[1]
Write-Host -ForegroundColor Red "$($Pools.$Pool) is slave, Master was identified as $PoolMaster, trying to connect"
$Pools.Pool = $PoolMaster
$Session = Connect-XenServer -url "https://$PoolMaster" -UserName $username -Password $password -NoWarnCertificates -SetDefaultSession
}
# Search for XenServer host with specified name
$xenHost = Get-XenHost | Where-Object {
$_.name_label -eq $hostName
} | Select-Object uuid, name_label
# Show saved Host UUID and name
$xenHost
if ($host -eq $null) {
Write-Output "XenServer host not found."
return
}
# Get all VM's on speciefied host that starts with the VMPrefix
$vms = Get-XenVM | Where-Object {
$_.is_a_template -eq $False -and
$_.is_a_snapshot -eq $False -and
$_.name_label -like "$vmPrefix*"
} | Select-Object uuid, name_label
# Show saved VM UUIDs and Names
$vms
# Get the storage repository by name
$storageRepoUUID = Get-XenSR | Where-Object {
$_.name_label -like "$storageRepoName"
} | Select-Object uuid, name_label -First 1
if ($storageRepoUUID -eq $null) {
Write-Output "Storage repository not found."
return
}
# Show saved SR UUID and Name
$storageRepoUUID
foreach ($vm in $vms) {
$dynamicMin = $RAMinBytes
$dynamicMax = $RAMinBytes
$staticMin = $RAMinBytes
$staticMax = $RAMinBytes
Set-XenVM -Name $vm.name_label -Memory $RAMinBytes
}
foreach ($vm in $vms) {
Set-XenVM -Name $vm.name_label -VCPUsAtStartup $vCPUatStartup -VCPUsMax $vCPUMax
}
foreach ($vm in $vms) {
# Create a new virtual disk (VDI)
try {
$vdi = New-XenVDI -NameLabel $diskLabel -VirtualSize $diskSize -SR $storageRepoUUID -ReadOnly $false
if ($vdi -eq $null) {
throw "Failed to create VDI for VM: $($vm.name_label)"
}
# Create a virtual disk drive (VBD) connecting the VDI to the VM
$vbd = New-XenVBD -VM $vm.uuid -VDI $vdi.uuid -Device 2 -Bootable $false -Mode RW -Type Disk
if ($vbd -ne $null) {
Write-Output "Added a 2GB disk to VM: $($vm.name_label) on repository: $storageRepoName"
} else {
Write-Output "Failed to attach VDI to VM: $($vm.name_label)"
}
} catch {
Write-Output "Error occurred while processing VM $($vm.name_label): $($_.Exception.Message)"
}
}
# Close XenServer Connection
Disconnect-XenServer
Who knows what is wrong with this script, or knows a better option for creating 400 disks on 400 VMs?
Link to comment
6 answers to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now