Hello,
I have this script to find VMs using shared disks:
"_________________________________________________________________________"
"The VMs below have shared disks and reside on hosts which must be patched. Must be scheduled."
Get-VM -PipelineVariable vm | Get-ScsiController | Where-Object {$_.BusSharingMode -in 'Virtual','Physical'} | Select Parent,BusSharingMode,@{N='VMHost';E={$vm.VMHost.Name}},@{N='Version';E={$vm.VMHost.version}} | Out-Default
"
However I now realize that many VMs actually share disks by setting the actual disk to mult-writer rather than via the SCSI controller. Is there an easy way to re-write this script so that it finds shared disk regardless of how which method they're using?
It is probably a lot easier to do that in a separate line of code, but keep the same output properties (although BusSharingMode is technically not correct for disks marked as MultiWriter).
Get-VM -PipelineVariable vm |
Get-HardDisk |
Where-Object {$_.ExtensionData.Backing.Sharing -ne 'sharingNone'} |
Select Parent, @{N='BusSharingMode';E={'MultiWriter'}},
@{N = 'VMHost'; E = { $vm.VMHost.Name } },
@{N = 'Version'; E = { $vm.VMHost.version } }
Combined that could be something like this
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
Get-ScsiController -VM $vm |
Where-Object { $_.BusSharingMode -in 'Virtual', 'Physical' } |
Select-Object Parent, BusSharingMode,
@{N = 'VMHost'; E = { $vm.VMHost.Name } },
@{N = 'Version'; E = { $vm.VMHost.version } }
Get-HardDisk -VM $vm |
Where-Object {$_.ExtensionData.Backing.Sharing -ne 'sharingNone'} |
Select Parent, @{N='BusSharingMode';E={'MultiWriter'}},
@{N = 'VMHost'; E = { $vm.VMHost.Name } },
@{N = 'Version'; E = { $vm.VMHost.version } }
} | Out-Default
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It is probably a lot easier to do that in a separate line of code, but keep the same output properties (although BusSharingMode is technically not correct for disks marked as MultiWriter).
Get-VM -PipelineVariable vm |
Get-HardDisk |
Where-Object {$_.ExtensionData.Backing.Sharing -ne 'sharingNone'} |
Select Parent, @{N='BusSharingMode';E={'MultiWriter'}},
@{N = 'VMHost'; E = { $vm.VMHost.Name } },
@{N = 'Version'; E = { $vm.VMHost.version } }
Combined that could be something like this
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
Get-ScsiController -VM $vm |
Where-Object { $_.BusSharingMode -in 'Virtual', 'Physical' } |
Select-Object Parent, BusSharingMode,
@{N = 'VMHost'; E = { $vm.VMHost.Name } },
@{N = 'Version'; E = { $vm.VMHost.version } }
Get-HardDisk -VM $vm |
Where-Object {$_.ExtensionData.Backing.Sharing -ne 'sharingNone'} |
Select Parent, @{N='BusSharingMode';E={'MultiWriter'}},
@{N = 'VMHost'; E = { $vm.VMHost.Name } },
@{N = 'Version'; E = { $vm.VMHost.version } }
} | Out-Default
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I added a little bit to make it a little more specific to my purpose - I want to identify all hosts which need to be upgraded however contain VMs which cannot be motioned, like shared-disks. For some reason if I try to add the "folder" field nothing comes out:
$cluhosts = get-vmhost | Where-Object {$_.version -notmatch "7.0.3" -and $_.parent -notmatch "xyz" -and $_.Name -notmatch "xyz"}
foreach ($cluhost in $cluhosts) {
get-vmhost -Name $cluhost | Get-VM -PipelineVariable vm |
ForEach-Object -Process {
Get-ScsiController -VM $vm |
Where-Object { $_.BusSharingMode -in 'Virtual', 'Physical' } |
Select-Object Parent, Folder, BusSharingMode,
@{N = 'VMHost'; E = { $vm.VMHost.Name } },
@{N = 'Version'; E = { $vm.VMHost.version } }
Get-HardDisk -VM $vm |
Where-Object {$_.ExtensionData.Backing.Sharing -ne 'sharingNone'} |
Select Parent, Folder, @{N='BusSharingMode';E={'MultiWriter'}},
@{N = 'VMHost'; E = { $vm.VMHost.Name } },
@{N = 'Version'; E = { $vm.VMHost.version } }
}| Out-Default
}
Any ideas?
Instead of Folder use the calculated property (the object in the pipeline is not a VM)
Select-Object ..., @{N='Folder';E={$vm.Folder.Name}}, ...
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD! Can I export all this to csv without losing the formating?
Sure, add a pipeline symbol followed by Export-Csv after the last curly brace
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference