VMware Cloud Community
pfuhli
Enthusiast
Enthusiast
Jump to solution

stuck on getting connected state of VMs CDROM

Hello,

I tried to iterate through my VMs for getting the ConnectionState of the CDROM attached to each VM. Unfortunately I'm a bloody beginner with Power Shell 😕 Maybe someone of you can guide me through?

Here my starting point which only works for one VM:

$server = Get-VIServer -Server <serverIP> -User <username> -Password <mypassword>

$vm = Get-VM -Name <vmname>

($vm | Get-CDDrive).ConnectionState | ft { $vm.Name },Connected

How do I have to wrap this to get the data for all my VMs?

Regards,

daniel

Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

To get the names of VMs with connected CD drives you could use something like:

get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" } } | select Name

View solution in original post

Reply
0 Kudos
5 Replies
glynnd1
Expert
Expert
Jump to solution

get-vm | % { get-view $_.ID } | select Name, @{ Name="ToolsVersion"; Expression={$_.config.tools.toolsVersion}}

get-vm | % {$name = $_.name + ".csv"; $_ | get-stat -stat disk.write.average -Realtime -MaxSamples 180 | select timestamp,value,metricId | export-csv $name }

Using the above two lines as examples I think the line below is what you want. The % is a foreach loop. Haven't tested it, and I'm a complete newbie - or at least was on Monday Smiley Happy

get-vm | % Get-CDDrive.ConnectionState | ft {$vm.Name},Connected

halr9000
Commander
Commander
Jump to solution

I tried to iterate through my VMs for getting the ConnectionState of the CDROM attached to each VM. Unfortunately I'm a bloody beginner with Power Shell

($vm | Get-CDDrive).ConnectionState | ft { $vm.Name },Connected

The reason this does not work is because accessing a property e.g. ".ConnectionState" only works on a single object, not a collection, and not a pipeline. The best way to get that value is by piping it through foreach-object.

$vm | get-cddrive | % { $_.ConnectionState.Connected }

Unfortunately, there is not a link back to the VM with a CD-Drive object, so if you want the VM name in the table you'll have to do something like this:

$vm | ft name, { (Get-CDDrive $_).ConnectionState.Connected }

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
pfuhli
Enthusiast
Enthusiast
Jump to solution

I found another way which is working - nevertheless it flaws out the status of all CDROMs though I wanted to have only the connected. Also I was able to send it via mail 🐵

$SmtpClient = new-object system.net.mail.smtpClient
$SmtpServer = "<server>"
$From = "<sendermailaddi>"
$To = "<myaddi>"
$Title = "VIToolkit:connected CDROMs "
$file = "c:\test.txt"
 
 
 
$server = Get-VIServer -Server <ip> -User <unsername>-Password <password>
 
 
 
$Body = "See attached file for information!!"
 
 
 
Get-VM | ft name,@{label="CDConnectionState"; Expr = { ($_ | get-cddrive).ConnectionState.Connected} } | Out-File -FilePath $file -Force
 
 
 
$msg = new-object System.Net.Mail.MailMessage $From, $To, $Title, $Body
$attachment = new-object System.Net.Mail.Attachment $file
$msg.Attachments.Add($attachment)
$client = new-object System.Net.Mail.SmtpClient $SmtpServer
$client.Send($msg)

Any idea how to sort out the ones which are not connected?

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

To get the names of VMs with connected CD drives you could use something like:

get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" } } | select Name

Reply
0 Kudos
DanAndryszak
Contributor
Contributor
Jump to solution

Thanks.

Couldn't get the VM names in the output of the script I created (I'm a "novice" PowerShell scripter).

Your script is straight to point.

Reply
0 Kudos