Experts,
In my solution i have some (over a 100) VMs that i need to change their harddisk type from IDE to SCSI. In order to do that, i need to go into every VM vmdk file and change ddb.adapter = “ide.” to “lsilogic.”. Then remove the old IDE harddisk and add a new SCSI harddisk. In order to do that, i will need to login into every host navigate to the VM vmdk file and manually replace the attribute and save the file. Is there a fast way to do that using powerCli that can change/replace the adapter type with a script on the fly without having to download/update all the vmdk files locally on my PC? I have been reading a lot of posts here but none seems to accomplish what i'm looking for. This was the closest but the change is not on the fly (What is the best way to change the Virtual Device Node of a HardDisk from IDE to SCSI? ) .. help is appreciated.
Try something like this
$vmName = 'MyVM'
$hdName = 'Hard disk 1'
$tempFolder = 'C:\Temp'
$vm = Get-VM -Name $vmName
if($vm.PowerState -ne 'PoweredOff'){
Throw "VM $($vmName) needs to be powered off"
}
$hd = Get-HardDisk -VM $vm -Name $hdName
Remove-HardDisk -HardDisk $hd -Confirm:$false
$dsName,$path = $hd.Filename.Split(' ')
$vmdkFile = $path.Split('/')[1]
$dsName = $dsName.Trim('[]')
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root '\' > $null
Copy-DatastoreItem -Item "DS:/$($path)" -Destination $tempFolder
(Get-Content -Path "$($tempFolder)\$($vmdkFile)" | %{
if($_ -match 'ddb.adapterType = "ide"'){
$_ -replace 'ide','lsilogic'
}
else{
$_
}
}) | Set-Content -Path "$($tempFolder)\$($vmdkFile)" -Confirm:$false
Copy-DatastoreItem -Item "$($tempFolder)\$($vmdkFile)" -Destination "DS:/$($path)" -Confirm:$false
Remove-PSDrive -Name DS
$hdNew = New-HardDisk -VM $vm -DiskPath $hd.Filename -Confirm:$false
New-ScsiController -Type VirtualLsiLogic -HardDisk $hdNew
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
$vmName = 'MyVM'
$hdName = 'Hard disk 1'
$tempFolder = 'C:\Temp'
$vm = Get-VM -Name $vmName
if($vm.PowerState -ne 'PoweredOff'){
Throw "VM $($vmName) needs to be powered off"
}
$hd = Get-HardDisk -VM $vm -Name $hdName
Remove-HardDisk -HardDisk $hd -Confirm:$false
$dsName,$path = $hd.Filename.Split(' ')
$vmdkFile = $path.Split('/')[1]
$dsName = $dsName.Trim('[]')
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root '\' > $null
Copy-DatastoreItem -Item "DS:/$($path)" -Destination $tempFolder
(Get-Content -Path "$($tempFolder)\$($vmdkFile)" | %{
if($_ -match 'ddb.adapterType = "ide"'){
$_ -replace 'ide','lsilogic'
}
else{
$_
}
}) | Set-Content -Path "$($tempFolder)\$($vmdkFile)" -Confirm:$false
Copy-DatastoreItem -Item "$($tempFolder)\$($vmdkFile)" -Destination "DS:/$($path)" -Confirm:$false
Remove-PSDrive -Name DS
$hdNew = New-HardDisk -VM $vm -DiskPath $hd.Filename -Confirm:$false
New-ScsiController -Type VirtualLsiLogic -HardDisk $hdNew
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a lot LucD .. it worked perfectly for me except for the new scsi controller that i did not include in my script .. one question why did you include this line to add a new SCSI controller of Type VirtualLsiLogic? The current controller type shows as LSI logic parallel .. is there any need to change it to another type or is there a need to re-create the controller? Thanks
You can select whatever type you want.
You can also use a SCSI controller that is already present.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a mil LucD