Hi Danilo, I have adjusted the script but it does not work yet. This is now the script:
#Variables
$hostName = "xenserver host name"
$Username = "admin_account"
$Password = "admin_password"
$vmPrefix = "VDIPRD"
$storageRepoName = "Local storage"
$diskSize = 10GB # 10 GB disk
$diskLabel = "Persistent-disk"
# 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
# We use the -Name switch to fetch the SR using the value of its name_label field. This also works for other `Get-Xen...` calls!
# We then fetch its opaque_ref, which is what's needed by the New-XenVDI call further down
$storageRepository = Get-XenSR -Name $diskLabel | Select-Object opaque_ref, uuid, name_label
if ($null -eq $storageRepository) {
Write-Output "Storage repository not found."
return
}
# Show saved SR reference, UUID, and Name
$storageRepository
foreach ($vm in $vms) {
# Create a new virtual disk (VDI)
$vdi = New-XenVDI -NameLabel $DiskLabel -VirtualSize $diskSize -SR $storageRepository.opaque_ref -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
Could it be that in line 46, instead of $diskLabel, there should be $storageRepoName? (this is a variable at the beginning of the script). It should retrieve the 'Local Storage' repository opaque_ref, right?
If I make it $StorageRepoName I get an error:
Failed to create VDI for VM: VDIPRD2005
At D:\Temp\ADD disk to XenServer VMs on XenServer host.ps1:61 char:13
+ throw "Failed to create VDI for VM: $($vm.name_label)"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Failed to create VDI for VM: VDIPRD2005:String) [], RuntimeException
+ FullyQualifiedErrorId : Failed to create VDI for VM: VDIPRD2005
Do you have any suggestions?
Thank you in advance!