Sandeep
Sandeep

Reputation: 137

Correction in powershell script

I have written a powershell script to get OVM and KVM driver type and version.And I need both to get from one script, so that it can work both OVM and KVM machines.I am getting the output from the script ,but I need help in trimming so that the output will be correct.

$Driver = Get-CimInstance -ClassName Win32_PnPSignedDriver |
    Where-Object {$_.DeviceName -like '*VirtIO*' -or $_.DeviceName -like '*Oracle VM Virtual PCI Bus*'} | 
        Select-Object -Property DeviceName,DriverVersion

if ($Driver -contains '*Red Hat VirtIO*')

{

Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>14</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>KVM Driver Type & Version</B></td>"
    Add-Content $report "<td bgcolor= 'red'  height='30' align=left><B>$Driver</B></td>"
Add-Content $report "</tr>"

}

else

{
Add-Content $report "<tr>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=center><B>14</B></td>"
    Add-Content $report "<td bgcolor= 'White'  height='30' align=left><B>PV Driver Type & Version</B></td>"
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='30' align=left><B>$Driver</B></td>"
Add-Content $report "</tr>"

}

The current out put of the script shows like below, when it runs on OVM the out put shows as **PV Driver Type & Version @{DeviceName=Oracle VM Virtual PCI Bus; DriverVersion=3.4.2.1757}

The output should be PV Driver Type & Version Oracle VM Virtual PCI Bus 3.4.2.1757}

For KVM , I am getting below output in powershell prompt. But not getting any output in email, as the email should get only "Red Hat VirtIO Ethernet Adapter 100.74.104.13200"

DeviceName DriverVersion ---------- ------------- Red Hat VirtIO SCSI controller 100.74.104.13200 Red Hat VirtIO Ethernet Adapter 100.74.104.13200

Any help is much appreciated.

Upvotes: 3

Views: 123

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

In your example, I'd suggest using here-strings to make it easier to read. (included comment adjustment for string subexpression)

$P=@{ClassName='Win32_PnPSignedDriver'
     Filter='DeviceName LIKE "%VirtIO%" OR DeviceName LIKE "%Oracle VM Virtual PCI%"'}
$Drivers = Get-CimInstance @P | Select-Object -Property DeviceName,DriverVersion

ForEach ($Driver in $Drivers)
{
    $Label='PV Driver Type & Version'
    If ($Driver.DeviceName -like '*Red Hat VirtIO*')
    {$Label='KVM Driver Type & Version'}

    Add-Content -Path $report -Value @"
<tr>
    <td bgcolor='White' height='30' align=center><B>14</B></td>
    <td bgcolor='White' height='30' align=left><B>$Label</B></td>
    <td bgcolor='Aquamarine' height='30' align=left><B>$($Driver.DeviceName,$Driver.DriverVersion -join ' ')</B></td>
</tr>
"@
}

Upvotes: 1

Related Questions