Home > Back-end >  Advice on how to compare string in a text file
Advice on how to compare string in a text file

Time:08-20

Could anyone offer any advice how to compare string from a text file to the info pulled from wmi.

$GCard = (Get-WmiObject "Win32_Videocontroller").Name
$DriverModel = Get-Content -Path "C:\temp\Quadro473.81.txt"

foreach ($Line in $DriverModel)
{

if($Line -like "*$MyCard*")
    {
        write-host "Quadro" 
    }
}

The text file (Quadro473.81.txt) is as following.

UDA Package name: QuadroWeb Public Disk1

{"NSD":{"State":"0"},"DCH":{"State":"1"}}


nv_disp.inf:
    DEV_0FF3 "NVIDIA Quadro K420"
    DEV_0FF9 "NVIDIA Quadro K2000D"
    DEV_0FFA "NVIDIA Quadro K600"
    DEV_0FFD "NVIDIA NVS 510"
    DEV_0FFE "NVIDIA Quadro K2000"
    DEV_0FFF "NVIDIA Quadro 410"
    DEV_1021 "NVIDIA Tesla K20Xm"
    DEV_1022 "NVIDIA Tesla K20c"
    DEV_1023 "NVIDIA Tesla K40m"
    DEV_1024 "NVIDIA Tesla K40c"
    DEV_1026 "NVIDIA Tesla K20s"
    DEV_1027 "NVIDIA Tesla K40st"
    DEV_1028 "NVIDIA Tesla K20m"
    DEV_1029 "NVIDIA Tesla K40s"

The card in the machine report back as NVIDIA Quadro K420

I am trying to work out how to match the string from the video controller name to the list from the drivermodels and have it output the word quadro if it matches. I would just need to match the model that appears in quotes.

Any help would be appriciated.

CodePudding user response:

You can use -match to search through the array of strings returned by Get-Content and have it output "Quadro" when found inside your if statement.

$GCard       = (Get-WmiObject "Win32_Videocontroller").Name
$DriverModel = Get-Content -Path "C:\temp\Quadro473.81.txt"
foreach ($GCName in $GCard)
{
    if ($DriverModel -match $GCName) 
    {
        Write-Host -Object "Quadro"
    }
}

Few things to cover when using -match comparison operator.

  1. When used against an array, it will act as a filter and return the line that was matched against $GCard.
  2. It's underlying search pattern it uses are regular expressions.
    • So, if your call to (Get-WmiObject "Win32_Videocontroller").Name returned a value that has special characters, they would have to be escaped.

Since it returns the line it matches, it will evaluate the condition to "true" executing your block of code.

EDIT: Given that there could be multiple return values in $GCard, using a loop you can compare each line with $DriverModel.

  • Related