A script that detects cdroms that are connected or marked connect at power on and are mapped to non-shared storage.
This statement defines the VMs at risk for CDRom states that "block or may block if powered on" vMotion in HA or DRS.
The scripts below have been used to ID these risks but none match the statement perfectly and allow direct forwarding to VM Admins for correction of true risk.
Some ID all connected CDRoms, but if connected to shared storage there is no issue.
Some ID .ISO mappings but ignore connection state or when "connect on power on" is selected or not; or whether CDRom is connected; or if other non-shared storage is mapped.
So we need a script that lists "cdroms that are connected or marked connect at power on and are mapped to non-shared storage".
Any help appreciated and all points will be awarded.
+++++++
Get-VM | Where-Object {$_ | Get-CDDrive | Where-Object {$_.ConnectionState.Connected}} | select name, host, guest
+++++++
get-vm | where { $_ | get-cddrive | where { $_.ISOPath -like "*.ISO*"} } | select Name, @{Name=".ISOPath";Expression={( Get-CDDrive $_).isopath }} | ft –AutoSize
+++++++
HealthCheck 6.15 plugin
# Start of Settings
# VMs with CD drives not to report on
$CDFloppyConnectedOK ="APP*"
# End of Settings
$Result = @($FullVM | ?{$_.runtime.powerState -eq "PoweredOn" -And $_.Name -notmatch $CDFloppyConnectedOK} | ?{$_.config.hardware.device | ?{$_ -is [VMware.Vim.VirtualCdrom] -And $_.connectable.connected}} | Select Name)
$Result
$Title = "CD-Roms Connected"
$Header = "VM: CD-ROM Connected - vMotion Violation: $(@($Result).Count)"
$Comments = "The following VMs have a CD-ROM connected, this may cause issues if this machine needs to be migrated to a different host"
$Display = "Table"
$Author = "Alan Renouf, Frederic Martin"
$PluginVersion = 1.2
$PluginCategory = "vSphere"
+++++++
#########################################################################################
# The script lists information about CD-ROM, Floppy, Parallel Ports and Seriel Ports #
# #
# Created by: Anders Mikkelsen, 2009 #
#########################################################################################
Add-PSSnapin VMware.VimAutomation.Core
$server = "VIServerName or IP"
$user = "UserName"
$pwd = "Password"
Connect-VIServer $server -User $user -Password $pwd
$vms = Get-VM
write "VM's with CD-ROM 'Connected' :"
foreach ($vm in $vms | where { $_ | Get-CDDrive | where { $_.ConnectionState.Connected -eq "true"}}) {
write $vm.name
}
write "`nVM's with CD-ROM set to 'Connect at power on' :"
foreach ($vm in $vms | where { $_ | Get-CDDrive | where { $_.ConnectionState.StartConnected -eq "true"}}) {
write $vm.name
}
write "`nVM's with CD-ROM device type set to 'Client Device' :"
foreach ($vm in $vms | where { $_ | Get-CDDrive | where { $_.RemoteDevice.Length -ge 0}}) {
write $vm.name
}
write "`nVM's with CD-ROM device type set to 'Datastore ISO file' :"
foreach ($vm in $vms | where { $_ | Get-CDDrive | where { $_.ISOPath -like "*.ISO*"}}) {
write $vm.name
}
write "`nVM's with Floppy 'Connected' :"
foreach ($vm in $vms | where { $_ | Get-FloppyDrive | where { $_.ConnectionState.Connected -eq "true"}}) {
write $vm.name
}
write "`nVM's with Floppy 'Connect at power on' :"
foreach ($vm in $vms | where { $_ | Get-FloppyDrive | where { $_.ConnectionState.StartConnected -eq "true"}}) {
write $vm.name
}
write "`nVM's with Floppy device type set to 'Client Device' :"
foreach ($vm in $vms | where { $_ | Get-FloppyDrive | where { $_.RemoteDevice.Length -ge 0}}) {
write $vm.name
}
write "`nVM's with Floppy device type set to 'Use excisting floppy image in datastore' :"
foreach ($vm in $vms | where { $_ | Get-FloppyDrive | where { $_.FloppyImagePath -like "*.FLP*"}}) {
write $vm.name
}
write "`nVM's with Serial Ports attached :"
$vms | Get-View | %{
foreach($dev in $_.Config.Hardware.Device){
if($dev.DeviceInfo.Label -like "Serial Port*"){
foreach($tmp in $dev.Connectable){
write $_.Name
#write $tmp
}
}
}
}
write "`nVM's with Parallel Ports attached :"
$vms | Get-View | %{
foreach($dev in $_.Config.Hardware.Device){
if($dev.DeviceInfo.Label -like "Parallel Port*"){
foreach($tmp in $dev.Connectable){
write $_.Name
#write $tmp
}
}
}
}
Try something like this
Get-VM | Where {Get-CDDrive -VM $_ | Where {($_.ConnectionState.StartConnected -eq "true" -or
$_.ConnectionState.Connected -eq "true") -and
($_.IsoPath -and !((Get-Datastore -Name ($_.IsoPath.Split(']')[0]).TrimStart('[')).ExtensionData.Summary.MultipleHostAccess))}} |
Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc - thanks for quick reply. Your book and specifically the Get-VMHostPNic function came in handy last week. Get-datastore works fine. Datastore names vary alpha-numeric, 7 to 17 characters including underscores and dashes. Error on proposed is: Get-Datastore : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again. At C:\Utilities\tempScripts\CDReport.ps1:26 char:57 + ($_.IsoPath -and !((Get-Datastore -Name ($_.IsoPath.Split(']')[0 ... + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Get-Datastore], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatast ore
Thanks.
Do you get the correct datastorename when you do
Get-VM | Get-CDDrive | where {$_.IsoPath} |
Select @{N="DSname";E={($_.IsoPath.Split(']')[0]).TrimStart('[')}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So working from your query ... using one liner below ... I get output that should allow filtering for desired results.
So. select for ConnectionState of "connected or StartConnected" and does not have a DSname entry.
A connected or connect on power on cdrom mapped on non-shared storage. (the iso path in output is bonus)
What does Guest Control or No Guest Control indicate?
PowerCLI C:\> Get-VM | Get-CDDrive | where {$_.IsoPath} |
Select Parent, connectionstate, @{N="DSname";E={($_.IsoPath.Split(']')
[0]).TrimStart('[')}} | ft -autosize
Parent | ConnectionState | DSname | ||
xxSNRq01Pxx | NotConnected, | GuestControl, | StartConnected | D_UOX11 |
abcqHQ01Dxx | NotConnected, | GuestControl, | NoStartConnected | RRAGG_UOX11 |
abxxISYN02Pxx | Connected, | GuestControl, | StartConnected | RRASG_UOX11 |
ababcBISYN01Pxx | Connected, | GuestControl, | StartConnected | RRASG_UOX11 |
xxvqAPxxAD30Pxx | Connected, | GuestControl, | StartConnected | RRASG_UOX11 |
xxvqAPxxC30Pxx | Connected, | GuestControl, | StartConnected | RRASG_UOX11 |
abcqA01Sxx | NotConnected, | GuestControl, | NoStartConnected | RRPCISG_0000-120 |
ababcBAPg01Qxx | NotConnected, | GuestControl, | StartConnected | RRWKSSG_11-7 |
ababcBq04Pxx | Connected, | NoGuestControl, | StartConnected | XWR_UOX11 |
xxqdexx01 | Connected, | GuestControl, | StartConnected | XWR_UOX11 |
abxxGq01Pxx | Connected, | GuestControl, | StartConnected | XWR_UOX11 |
xxapqi02dxx | NotConnected, | GuestControl, | NoStartConnected | XWR_UOX11 |
xxXCCq01Dxx | NotConnected, | GuestControl, | StartConnected | XWR_UOX11 |
xxXqI01Dxx | NotConnected, | GuestControl, | StartConnected | XWR_UOX11 |
abcqExx1 | Connected, | GuestControl, | NoStartConnected | |
xxvqDBq30Pxx | Connected, | GuestControl, | NoStartConnected | |
abcDExx01 | NotConnected, | GuestControl, | NoStartConnected | |
xxfsnfs01sxx | NotConnected, | GuestControl, | NoStartConnected | |
abcnxdcid01 | NotConnected, | NoGuestControl, | NoStartConnected | |
xx11 | NotConnected, | GuestControl, | NoStartConnected |
According to the SDK, in VirtualDeviceConnectInfo, it means "Enables guest control over whether the connectable device is connected"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
A connected or connect on power on cdrom mapped on non-shared storage. (the iso path in output is bonus)
I am still having problem with syntax to get above report.
output to include vmname, vmhostname, connectionstate, actual iso path - but not to include cdroms connected to shared storage
No luck. Tired and must have tried every syntax combo. Can't sleep until this works.
Modified requirement to "Any cdrom that is connected to a non-shared storage LUN ISO".
Where-Object : Cannot bind argument to parameter 'FilterScript' because it is null.
At C:\Utilities\tempScripts\CDReport2.ps1:13 char:16
+ Get-VM | where ( $_ | Get-CDDrive |
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Where-Object], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.WhereObjec
tCommand
************
Get-VM | where ( $_ | Get-CDDrive |
where (($_.ConnectionState.Connected -eq "true" -and $_.ISOPath -like "*.ISO*") -and ( $_.ISOPath -inotcontains "]" ))) |
select parent,connectionstate,ISOPath,@{N="DSname";E={($_.IsoPath.Split(']')[0]).TrimStart('[')}} |
export-csv $filelocation -NoTypeInformation
Correct answer points still available.
I'm not sure I understand your "non-shared LUN ISO".
You do a test on the IsoPath with -inotcontains ']', but I would guess that an IsoPath to an ISO on a datastore always has these square brackets.
That is the reason why I checked if the datastore was shared via the MultiHostAcces property.
In the output of my one-liner, you have 6 IsoPaths that are not on datastore.
Where are these located then ?
Can you give an example of such an IsoPath ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Get-Datastore : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that
is not null or empty and then try the command again.
At C:\Utilities\tempScripts\LucD_CD_032014.ps1:22 char:181
+ ... atastore -Name ($_.IsoPath.Split(']')[0]).TrimStart('[')).ExtensionData.Summary. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Datastore], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatast
ore
I get 5 of these errors and no output on:
Get-VM | Where {Get-CDDrive -VM $_ | Where {($_.ConnectionState.StartConnected -eq "true" -or $_.ConnectionState.Connected -eq "true") -and ($_.IsoPath -and !((Get-Datastore -Name ($_.IsoPath.Split(']')[0]).TrimStart('[')).ExtensionData.Summary.MultipleHostAccess))}} |
select parent,connectionstate,ISOPath,@{N="DSname";E={($_.IsoPath.Split(']')[0]).TrimStart('[')}} |
export-csv $filelocation -NoTypeInformation
You can see output received in previous post on the variant of one liner. I'm running script inside PowerGUI where I test all my scripts. Powercli 5.5, Powershell 3
The attempt of mine using bracket was just one try of many different variants including some get-view attempts.
Modified requirement: VMs with connected CDRoms with ISOs on unshared storage
As a heads up of vMotion issues to VM Admins.
Can you run the following, I would like to see what those 6 last IsoPath properties hold.
Get-VM | Get-CDDrive | where {$_.IsoPath} | Select Parent,ConnectionState,IsoPath
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc - thanks for helping.
So, the single bracket is a no go, but the double bracket does separate the local from the shared storage. I left enough data in to see all the different combinations.
Looks like " CDs with connectionState of connected or start connected and ISOpath with "[]"" would be target output. "
However; we also have to get by that error regarding the null named DS. I don't see a pattern on 5 out of 93 total returned with this query.
Parent | ConnectionState | IsoPath |
p0lp0730s01730 | Connected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
p0lp0730s02730 | Connected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
p0lp0ngt01dv | Connected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
p0lwCRMp01 | Connected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
9geWDB66630730 | Connected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/windows.iso |
p06630GDS01730 | Connected, GuestControl, StartConnected | [l5_sto001] iso/en_windows_server_2012_x64_dvd_915478.iso |
p0sqp001 | Connected, GuestControl, StartConnected | [l5_sto001] Windows2003STD-x32SP2.iso |
9ge6630VAD30730 | Connected, GuestControl, StartConnected | [mker_sto001] iso/Gold Image/LiteTouchPE_x64.iso |
9ge6630Vkrf30730 | Connected, GuestControl, StartConnected | [mker_sto001] iso/Gold Image/LiteTouchPE_x64.iso |
p06630ISYN02730 | Connected, GuestControl, StartConnected | [mker_sto001] iso/Gold Image/LiteTouchPE_x64.iso |
p0WWBISYN01730 | Connected, GuestControl, StartConnected | [mker_sto001] iso/Gold Image/LiteTouchPE_x64.iso |
p0WWB66604730 | Connected, NoGuestControl, StartConnected | [l5_sto001] iso/Win2KSESP4.iso |
p0lwCp011 | NotConnected, GuestControl, NoStartConnected | [] |
p0lwe663db11 | NotConnected, GuestControl, NoStartConnected | [] |
p0lwE663p0P11 | NotConnected, GuestControl, NoStartConnected | [] |
p0lwE663p0P12 | NotConnected, GuestControl, NoStartConnected | [] |
p0lwOSPp0P11 | NotConnected, GuestControl, NoStartConnected | [] |
p0lwOSPp0P12 | NotConnected, GuestControl, NoStartConnected | [] |
CONkrf2 | NotConnected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
p011 | NotConnected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
p0ldb730s01730 | NotConnected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
p0lwMQS2 | NotConnected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
p0p0gpm11 | NotConnected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
p0sjs66602730 | NotConnected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/solaris.iso |
p0WBBI01730 | NotConnected, GuestControl, NoStartConnected | [] /vmimages/tools-isoimages/windows.iso |
p0wTestVM01QV | NotConnected, GuestControl, NoStartConnected | [] /vmimages/tools-isoimages/windows.iso |
p0lp0cctui02dv | NotConnected, GuestControl, NoStartConnected | [l5_sto001] iso/RHEL3.7/rhel-3-u7-i386-es-disc1.iso |
p0WDSHQ01DV | NotConnected, GuestControl, NoStartConnected | [mkAGG_sto001] WIn2003SysprepTools.iso |
p0WDSHQ01730 | NotConnected, GuestControl, NoStartConnected | [mkPCISG_5656-120] w2k3_R2_Enterprise_x64_SP2_Disk1.iso |
p0WDSHRC01730 | NotConnected, GuestControl, NoStartConnected | [mkPCISG_5656-120] w2k3_R2_Enterprise_x64_SP2_Disk1.iso |
p0WDSNA01730 | NotConnected, GuestControl, NoStartConnected | [mkPCISG_5656-120] w2k3_R2_Enterprise_x64_SP2_Disk1.iso |
p0WDSAkrf1730 | NotConnected, GuestControl, NoStartConnected | [PCI_sto001] iso/NETFRAMEWORK.ISO |
p0WDSHOT01730 | NotConnected, GuestControl, NoStartConnected | [PCI_sto001] iso/w2k3_R2_Standard_x64_SP2_Disk1.iso |
p0LSNR66601730 | NotConnected, GuestControl, StartConnected | [DMZ_sto001] ISO/erServer-v6.1.1-0-130618-x64.iso |
p0lwCCTBp001DV | NotConnected, GuestControl, StartConnected | [l5_sto001] iso/RHEL3.7/rhel-3-u7-i386-es-disc1.iso |
p0lwCCTUI01DV | NotConnected, GuestControl, StartConnected | [l5_sto001] iso/RHEL3.7/rhel-3-u7-i386-es-disc1.iso |
p0WWBp0p0P01QV | NotConnected, GuestControl, StartConnected | [mkWKSSG_1751-782] SourceFiles.iso |
p0lwdcikrf1 | NotConnected, NoGuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
Ok, try this revised version
Get-VM | Where {Get-CDDrive -VM $_ | Where {($_.ConnectionState.StartConnected -eq "true" -or $_.ConnectionState.Connected -eq "true") -and
$_.IsoPath -and
$_.IsoPath -ne "[]" -and
$_.IsoPath -notmatch "^/usr/lib/vmware/isoimages|^/vmimages/tools-isoimages" -and
!((Get-Datastore -Name ($_.IsoPath.Split(']')[0]).TrimStart('[')).ExtensionData.Summary.MultipleHostAccess)}} |
Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That did same as previous script. 5 of the 'Name" errors and no output.
Why are you using "$_.IsoPath -notmatch "^/usr/lib/vmware/isoimages|^/vmimages/tools-isoimages" -and" - doesn't this eliminate the local iso mounts from output?
The empty double brackets should filter any shared storage.
Shouldn't we filter for -eq [] and not use the the "get-datastore -name"?
I don't know how the iso path can contain just " [] " brackets. This may be a remnant of past Converter or migration operations. It is not permitted and prompts error in normal operations. The fact is that we have a number of Linux clients with that exact scenario. The CDRoms are NotConnected and NotStartConnected and the ISO is selected and mapped to " [] ". It doesn't seem pertinent to my goal of providing VM Admins with risks of Connected CDRoms or StartConnected CDRoms with local storage (non-shared datastores) as ISOPath.
Although this one liner produces the required data, It does not filter connected state for "only Connected or StartConnected" or ISOPath of "local, non-shared storage" with output of VMname, connectionstate, and ISOpath.
Get-VM | Get-CDDrive | where {$_.IsoPath} | Select Parent,ConnectionState,IsoPath
Parent | ConnectionState | IsoPath |
p0lp0730s01730 | Connected, GuestControl, NoStartConnected | [] /usr/lib/vmware/isoimages/linux.iso |
Output would include all VMs with ConnectionState:
Connected, GuestControl, NoStartConnected
Connected, NoGuestControl, NoStartConnected
Connected, GuestControl, StartConnected
NotConnected, NoGuestControl, StartConnected
NotConnected, GuestControl, StartConnected
And ISOPaths with local, non-shared storage (determining factor? [] ? MultiHostAccess? )
The filter of:
-and !((Get-Datastore -Name ($_.IsoPath.Split(']')[0]).TrimStart('[')).ExtensionData.Summary.MultipleHostAccess)}} |
results in zero output and 5 errors of:
Get-Datastore : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that
is not null or empty and then try the command again.
At C:\Utilities\tempScripts\LucD_CD_032014.ps1:22 char:181
+ ... atastore -Name ($_.IsoPath.Split(']')[0]).TrimStart('[')).ExtensionData.Summary. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Datastore], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatast
ore
A working parameter to filter via "where" for the [] has eluded me so far.
Perhaps an additional function is required to get to desired output.
Goal is to provide VM Admins with specific VMs that are configured such as to pose a true vMotion and/or HA failure without having to manually filter output via Excel for daily reports.
You could try a
Where {$_.IsoPath -match '[]'}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
At C:\Utilities\tempScripts\LucD_CD_032014.ps1:22 char:31
+ Get-VM | Get-CDDrive | where {$_.IsoPath -match '[]'} | Select Parent,Connection ...
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
May need some character exception syntax.
It looks as if the IsoPath is not really containing the string '[]'.
Do these VMs get selected when you do
Where {!$_.IsoPath}
In other words when the IsoPath property contains the empty string or $null ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
where {$_.IsoPath)
IsoPath : []
IsoPath : [] /usr/lib/vmware/isoimages/linux.iso
IsoPath : [SHAREDSTORAGE_LUN001] iso/RHEL3.7/rhel-3-u7-i386-es-disc1.iso
----------
Where {$_.IsoPath -match '[]'}
errors
----------
Where {$_.IsoPath -match '``[``]'}
or
Where {$_.IsoPath -match '````[````]*'}
no output
----------
{!$_.IsoPath}
does not contain the anomaly "[]" in output
anomaly does appear in where {$_.IsoPath)
so it is "mounted" via this query
---------
Please note that I am not concerned with anomaly being in or out of output. It shows up as mounted in isopath query but those anamolies are also "NotConnected" and "NotStartConnected" and should not show up in final filtered output anyways.
p0lwE663p0P12 NotConnected, GuestControl, NoStartConnected []
-------------
Thanks for any suggestions.
I'm checking out Hal's TFM chap 4 Get-VMCDReport.ps1 now