VMware Cloud Community
mfrycz
Contributor
Contributor
Jump to solution

Connected devices report.

Hi,

I am writing a script to report all connected CD-ROM's, Floppy's, Parallel ports, Serial ports to the VM and sent a nice email afterwards...

This is what i got:

$smtpServer = "smtp.company.com"
$MailFrom = "ConnectedDevices@company.com"
$VISRV = "vcenterServer"
$Mailto = "me@company.com"

# Add the VI-Snapin if it isn't loaded already
if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin -Name "VMware.VimAutomation.Core"
}

Connect-VIServer $VISRV

$report = @()

foreach($vm in (Get-VM | Where-Object {    ((Get-CDDrive -VM $_ | Where-Object { (($_.ConnectionState.Connected -eq $True) -or ($_.ConnectionState.StartConnected -eq $True))} ) -ne $Null)`
-and `
    ((Get-FloppyDrive -VM $_ | Where-Object { (($_.ConnectionState.Connected -eq $True) -or ($_.ConnectionState.StartConnected -eq $True))} ) -ne $Null)`
-and `
    ((Get-CDDrive -VM $_ | Where-Object { $_.ISOPath -like "*.ISO*"} ) -ne $Null)`
-and `
    ((Get-FloppyDrive -VM $_ | Where-Object { $_.FloppyImagePath -like "*.FLP*"} ) -ne $Null)`
-and `
    ((Get-View -ViewType VirtualMachine | %{
  foreach($dev in $_.Config.Hardware.Device){
    if($dev.DeviceInfo.Label -like "Serial Port*"){
      foreach($tmp in $dev.Connectable){
        write $_.Name
      }
    }
  }
  }) -ne $Null)`
-and `
    ((Get-View -ViewType VirtualMachine  | %{
  foreach($dev in $_.Config.Hardware.Device){{
    if($dev.DeviceInfo.Label -like "Parallel Port*"){
      foreach($tmp in $dev.Connectable){
        write $_.Name
      }
    }
  }
  }}) -ne $Null)
}
)){

if($report -ne $null){
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = $MailFrom
    $msg.To.Add($Mailto)
    $msg.Subject = "Connected Devices"
    $msg.Body = $report | Out-String
    $smtp.Send($msg)
}}

It does all queries that I want but it is not working for some reason.

So when script completes execution no emails are coming to the recipient...

Could anyone please have a look into the lines as there must be something that I am missing here...

Thanks,

MF

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, try this one

$smtpServer = "smtp.company.com"
$MailFrom = "ConnectedDevices@company.com"
$VISRV
= "vcenterServer"
$Mailto
= "me@company.com" # Add the VI-Snapin if it isn't loaded already
if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) {     Add-PSSnapin -Name "VMware.VimAutomation.Core"
}
Connect-VIServer $VISRV
$l1 = "VM"
$f1L
= $l1.Length + 9                # 11
$l2
= "VM state"
$f2L
= $l2.Length + 3                # 11
$l3
= "Media"
$f3L
= $l3.Length + 12               # 17
$l4
= "Connected"
$f4L
= $l4.Length + 1                # 10
$l5
= "Startup Settings"
$f5L
= $l5.Length + 1                # 17

$body
= $l1.PadRight($f1L) + $l2.PadRight($f2L) + $l3.PadRight($f3L) + $l4.PadRight($f4L) + $l5.PadRight($f5L) foreach($vm in Get-VM){     $body += Out-String -InputObject (" ")     $vmInfo = $vm.Name.PadRight($f1L) + $vm.PowerState.ToString().PadRight($f2L)     if($vm.CDDrives){         $vm.CDDrives | %{             $body += Out-String -InputObject ($vmInfo + $_.Name.PadRight($f3L) + $_.ConnectionState.Connected.ToString().PadRight($f4L) + $_.ConnectionState.StartConnected.ToString().PadRight($f5L))             $vmInfo = " " * $vmInfo.Length         }     }     if($vm.FloppyDrives){         $vm.FloppyDrives | %{             $body += Out-String -InputObject ($vmInfo + $_.Name.PadRight($f3L) + $_.ConnectionState.Connected.ToString().PadRight($f4L) + $_.ConnectionState.StartConnected.ToString().PadRight($f5L))             $vmInfo = " " * $vmInfo.Length         }     }     $serial = $vm.ExtensionData.Config.Hardware.Device |where {$_.DeviceInfo.Label -like "Serial Port*"}     if($serial){         $serial | %{             $body += Out-String -InputObject ($vmInfo + $_.DeviceInfo.Label.PadRight($f3L) + $_.Connectable.Connected.ToString().PadRight($f4L) + $_.Connectable.StartConnected.ToString().PadRight($f5L))             $vmInfo = " " * $vmInfo.Length         }     }     $parallel = $vm.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -like "Parallel Port*"}     if($parallel){         $parallel | %{             $body += Out-String -InputObject ($vmInfo + $_.DeviceInfo.Label.PadRight($f3L) + $_.Connectable.Connected.ToString().PadRight($f4L) + $_.Connectable.StartConnected.ToString().PadRight($f5L))             $vmInfo = " " * $vmInfo.Length         }     } } $msg = new-object Net.Mail.MailMessage
$smtp
= new-object Net.Mail.SmtpClient($smtpServer) $msg.From = $MailFrom
$msg
.To.Add($Mailto) $msg.Subject = "Connected Devices"
$msg
.Body = $body
$smtp
.Send($msg)

It is possible that you have to adjust the width of some of the columns, depending for example on the namelength of your guests.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Not exactly sure what you want to see in the report, so I just list the device name.

Try this version

$smtpServer = "smtp.company.com" 
$MailFrom
= "ConnectedDevices@company.com"
$VISRV
= "vcenterServer"
$Mailto
= "me@company.com" # Add the VI-Snapin if it isn't loaded already
if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) {     Add-PSSnapin -Name "VMware.VimAutomation.Core"
} Connect-VIServer $VISRV $report = @() foreach($vm in Get-VM mmmstv12*){     $devs = @()     if($vm.CDDrives){         $devs = ($vm.CDDrives | `
            where {($_.ConnectionState.Connected -eq $True -or $_.ConnectionState.StartConnected -eq $True) -and`
            $_.ISOPath -like "*.ISO*" } | %{$_.Name})     }     if($vm.FloppyDrives){         $devs += (Get-FloppyDrive -VM $vm | `
        where {($_.ConnectionState.Connected -eq $True -or $_.ConnectionState.StartConnected -eq $True) -and`
           
$_.FloppyImagePath -like "*.FLP*" } | %{$_.Name})     }     $devs += ($vm.ExtensionData.Config.Hardware.Device | `
    where {$_.DeviceInfo.Label -like "Serial Port*"} | %{$_.DeviceInfo.Label})     $devs += ($vm.ExtensionData.Config.Hardware.Device | `
   
where {$_.DeviceInfo.Label -like "Parallel Port*"} | %{$_.DeviceInfo.Label})         if($devs){         $report += ($vm.Name + " " + $devs)     } } if($report -ne $null){     $msg = new-object Net.Mail.MailMessage
   
$smtp = new-object Net.Mail.SmtpClient($smtpServer)     $msg.From = $MailFrom
   
$msg.To.Add($Mailto)     $msg.Subject = "Connected Devices"
   
$msg.Body = $report | Out-String
   
$smtp.Send($msg) }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

mfrycz
Contributor
Contributor
Jump to solution

Hi Luc,

All looks good with your changes.

Report apart of name should show all queries in the script ie:

VM            VM state       media                  Status                 Startup Settings

testvm1      on                 CDROM               Connected            disconnected

testvm2      off                 FloppyDrive           Connected            connected

something like that...

Not sure how this will behave if vm have more than one "media" connected... as now only with

if($devs){
        $report += ($vm.Name + " " + $devs)
               
    }

this is the output as per your changes:

===================================================

testvm1 Serial port 1 Serial port 2 Serial port 3 testvm2 Serial port 1

testvm3 Serial port 1Parallel port 1

testvm4 Parallel port 1

testvm5 Parallel port 1

testvm6 Serial port 1

testvm7 CD/DVD Drive 1

testvm8 CD/DVD Drive 1

===================================================

PS.Will have to spend more study on formating this outputs Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just to make sure, you want a line per connected device in the report ?

A VM with multiple devices connected will thus have multiple lines.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mfrycz
Contributor
Contributor
Jump to solution

Yes if report could be formatted in this way:

VM            VM state       media                  Status                 Startup Settings

testvm1      on                CDROM               Connected            disconnected

                                     FloppyDrive 1        Connected            connected

                                     FloppyDrive 1        Disconnected        connected

testvm2      off                 FloppyDrive           Connected            connected

so you could have a space line between VM's that would be great.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, try this one

$smtpServer = "smtp.company.com"
$MailFrom = "ConnectedDevices@company.com"
$VISRV
= "vcenterServer"
$Mailto
= "me@company.com" # Add the VI-Snapin if it isn't loaded already
if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null ) {     Add-PSSnapin -Name "VMware.VimAutomation.Core"
}
Connect-VIServer $VISRV
$l1 = "VM"
$f1L
= $l1.Length + 9                # 11
$l2
= "VM state"
$f2L
= $l2.Length + 3                # 11
$l3
= "Media"
$f3L
= $l3.Length + 12               # 17
$l4
= "Connected"
$f4L
= $l4.Length + 1                # 10
$l5
= "Startup Settings"
$f5L
= $l5.Length + 1                # 17

$body
= $l1.PadRight($f1L) + $l2.PadRight($f2L) + $l3.PadRight($f3L) + $l4.PadRight($f4L) + $l5.PadRight($f5L) foreach($vm in Get-VM){     $body += Out-String -InputObject (" ")     $vmInfo = $vm.Name.PadRight($f1L) + $vm.PowerState.ToString().PadRight($f2L)     if($vm.CDDrives){         $vm.CDDrives | %{             $body += Out-String -InputObject ($vmInfo + $_.Name.PadRight($f3L) + $_.ConnectionState.Connected.ToString().PadRight($f4L) + $_.ConnectionState.StartConnected.ToString().PadRight($f5L))             $vmInfo = " " * $vmInfo.Length         }     }     if($vm.FloppyDrives){         $vm.FloppyDrives | %{             $body += Out-String -InputObject ($vmInfo + $_.Name.PadRight($f3L) + $_.ConnectionState.Connected.ToString().PadRight($f4L) + $_.ConnectionState.StartConnected.ToString().PadRight($f5L))             $vmInfo = " " * $vmInfo.Length         }     }     $serial = $vm.ExtensionData.Config.Hardware.Device |where {$_.DeviceInfo.Label -like "Serial Port*"}     if($serial){         $serial | %{             $body += Out-String -InputObject ($vmInfo + $_.DeviceInfo.Label.PadRight($f3L) + $_.Connectable.Connected.ToString().PadRight($f4L) + $_.Connectable.StartConnected.ToString().PadRight($f5L))             $vmInfo = " " * $vmInfo.Length         }     }     $parallel = $vm.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -like "Parallel Port*"}     if($parallel){         $parallel | %{             $body += Out-String -InputObject ($vmInfo + $_.DeviceInfo.Label.PadRight($f3L) + $_.Connectable.Connected.ToString().PadRight($f4L) + $_.Connectable.StartConnected.ToString().PadRight($f5L))             $vmInfo = " " * $vmInfo.Length         }     } } $msg = new-object Net.Mail.MailMessage
$smtp
= new-object Net.Mail.SmtpClient($smtpServer) $msg.From = $MailFrom
$msg
.To.Add($Mailto) $msg.Subject = "Connected Devices"
$msg
.Body = $body
$smtp
.Send($msg)

It is possible that you have to adjust the width of some of the columns, depending for example on the namelength of your guests.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mfrycz
Contributor
Contributor
Jump to solution

Luc,

This works great.

I will do some changes to it (cosmetic ones) but the code is exactly what i was looking for...

BTW. going through your book and I have to admit it is a great read Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks.

Should you have any book-related questions we do have a website and forum set up.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
edelldieguez
Contributor
Contributor
Jump to solution

Hi Guys,

LucD,

Anyway that i can get this report in a .csv format??

Also i got this warning when i run the script:

Warning: 'CDDrives" property is obsolete. Use 'Get-CDDrives cmdlet instead

Warning: 'FloppyDrives" property is obsolete. Use  'Get-FloppyDrives' cmdlet instead

Anyway to fix this??

Thanks in advance.

0 Kudos
R0Ge
Contributor
Contributor
Jump to solution

Hi,

Thanks to LucD info above, I created a csv report.

Hope this helps you furter

Regards,

Robert

$Report = @()

$outputfile = "d:\vmwithdevices.csv"

foreach

($vm in Get-VM){

$serial = $vm.ExtensionData.Config.Hardware.Device |where {$_.DeviceInfo.Label -like "Serial Port*"} | %{$_.DeviceInfo.Label}

$parallel = $vm.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -like "Parallel Port*"} | %{$_.DeviceInfo.Label}

$usb = $vm.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -like "USB controller*"} | %{$_.DeviceInfo.Label}

$floppy = ( ($vm.FloppyDrives) | where {($_.ConnectionState.Connected -eq $True -or $_.ConnectionState.StartConnected -eq $True) -and $_.FloppyImagePath -like "*.FLP*" } | %{$_.Name})

$CDrom = ( ($vm.CDDrives) |where {($_.ConnectionState.Connected -eq $True -or $_.ConnectionState.StartConnected -eq $True) -and $_.ISOPath -like "*.ISO*" } | %{$_.Name})

if ($serial -or $parallel -or $usb -or $floppy -or $CDrom) {

$row = "" | Select-Object -Property VMname,Serial,Parallel,USB,Floppy,CDrom

$row.VMname = $VM.Name

$row.Serial = [string]$serial

$row.Parallel = [string]$parallel

$row.USB = [string]$usb

$row.Floppy = [string]$floppy

$row.CDrom = [string]$CDrom

$report += $row

}

}

$report | Export-Csv $outputfile -NoTypeInformation -UseCulture

0 Kudos