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
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
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
get-vm | % Get-CDDrive.ConnectionState | ft {$vm.Name},Connected
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)
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?
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
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.