• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 148
  • Last Modified:

Powershell / Where arrays

Hi everybody!

I'm trying to get a script to get the version (32 or 64 bits) of different INF drivers and then install the one corresponding to the version of the system.

# GET THE PATH OF THE INF FILES IN A FOLDER
$INF_PATH = gci $PSScriptRoot -Recurse -Filter "*.inf" | ? {$_.Attributes -ne "Directory"} | % {$_.FullName}

# GET THE OS VERSION IN THE INF (64 or 86)
$INF_OS_VERSION = ([regex]::matches((Get-Content $INF_PATH | Select-String -Pattern '^"Models, NT' | % {$_.Line.split(",")[1]}) , '\d{2}') | % {$_.value})

# GET THE OS VERSION OF THE SYSTEM (64 or 86)
$SYS_OS_VERSION = ((Get-WmiObject Win32_OperatingSystem).OSArchitecture).Substring(0,2)

$RESULT =  $INF_PATH | ? {($INF_OS_VERSION -eq $SYS_OS_VERSION)}

$RESULT

Open in new window


In this case I've got two INF, one 64 bits, one 32 bits, but instead of returning only one path of the INF (in 64 bits in my case), it returns both!

Can you see what I did wrong?

Thank you in advance,
0
Johann Buve
Asked:
Johann Buve
  • 3
  • 2
1 Solution
 
SubsunCommented:
Can you post the sample line which you trying to match in the inf file?
0
 
footechCommented:
I don't see any correlation between $INF_PATH and $INF_OS_VERSION, meaning there's nothing that binds them together, and so your Where-Object comparison has no meaning.

Easiest way to troubleshoot would be if you could provide both .INFs.
0
 
Johann BuveAuthor Commented:
Hi,

I attached the *.inf files (I just changed the extension to *.txt or I couldn't upload them).
As you will see, there are actually more datas extracted from the *.inf  of the print drivers (the OS, the driver name, the description language)

These files are just for the PS versions of the drivers, but (of course) there are more.

# SET THE PRINT DESCRIPTION LANGUAGE (PCL, PCL6, PS)
$MFP_DESC = "PS"

# GET THE PATH OF THE INF FILES IN A FOLDER
$INF_PATH = gci $PSScriptRoot -Recurse -Filter "*.inf" | ? {$_.Attributes -ne "Directory"} | % {$_.FullName}

# GET THE OS VERSION IN THE INF (64 or 86)
$INF_OS_VERSION = ([regex]::matches((Get-Content $INF_PATH | Select-String -Pattern '^"Xerox"=Models, NT' | % {$_.Line.split(",")[1]}) , '\d{2}') | % {$_.value})

# GET THE OS VERSION OF THE SYSTEM (64 or 86)
$SYS_OS_VERSION = ((Get-WmiObject Win32_OperatingSystem).OSArchitecture).Substring(0,2)

# GET THE PRINT DESCRIPTION LANGUAGE (PCL, PCL6, PS)
$INF_DRIVER = ([regex]::matches((Get-Content $INF_PATH |  Select-String -Pattern "^Xerox_UPD_1="), '\"(.+?)\"') | % {$_.value.replace('"','')})
$INF_DESC = $INF_DRIVER | % {$_.split()[2]}

# GET THE PATH DEPENDING ON THE OS VERSION (64 IN MY CASE) AND THE LANGUAGE DESCRIPTION (PS IN MY CASE)
$RESULT =  $INF_PATH | ? {($INF_OS_VERSION -eq $SYS_OS_VERSION) -and ($INF_DESC -eq $MFP_DESC)}

$RESULT

Open in new window


Thank you again for your help,

Johann
x2UNIVP32.txt
x2UNIVP64.txt
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
SubsunCommented:
$INF_PATH is nowhere dependent on $INF_OS_VERSION or $INF_DESC in the script. The variables are created using the $INF_PATH but not connected to it in Script.

Basically $INF_PATH contains only the list of file names, you can  filter based on the multiple properties of the object or even based on the file content...

In following example, I created a PowerShell  custom object to connect the file name, OS architecture and Language and finally filtered it based on your requirement.. Try it and see if it works for you.
# SET THE PRINT DESCRIPTION LANGUAGE (PCL, PCL6, PS)
$MFP_DESC = "PS"

# GET THE PATH OF THE INF FILES IN A FOLDER
$INF_PATH = gci $PSScriptRoot -Recurse -Filter "*.inf" | ? {$_.Attributes -ne "Directory"} | % {$_.FullName}

# GET THE OS VERSION and language IN THE INF (64 or 86)
$INF = $INF_PATH |%{
	$Path = $_
         #Creating custom powershell object to link file name, OS architecture and Language 
	New-Object PSObject -Property @{
	INF = $Path
	OS = [regex]::matches((Get-Content $Path | Select-String -Pattern '^"Xerox"=Models, NT' | % {$_.Line.split(",")[1]}) , '\d{2}') | %{$_.value}
	language = [regex]::matches((Get-Content $Path |  Select-String -Pattern "^Xerox_UPD_1="), '\"(.+?)\"') | %{($_.value.replace('"','')).split()[2]}
	}
}
# GET THE OS VERSION OF THE SYSTEM (64 or 86)
$SYS_OS_VERSION = ((Get-WmiObject Win32_OperatingSystem).OSArchitecture).Substring(0,2)

# GET THE PATH DEPENDING ON THE OS VERSION (64 IN MY CASE) AND THE LANGUAGE DESCRIPTION (PS IN MY CASE)
$RESULT =  $INF | ? {$_.OS -eq $SYS_OS_VERSION -and $_.language -eq $MFP_DESC} | Select -ExpandProperty INF

$RESULT

Open in new window

There are better ways to do it, but I have not changed the code much, so it'll be easy for you to find difference..
0
 
Johann BuveAuthor Commented:
I tried and it works perfectly!
Thank you a lot Subsun!
0
 
SubsunCommented:
You're Welcome!.. hope you are clear what was wrong with your code. If you don't have further questions about the script.. the don't forget to close the question :-)
0

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now