Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell / Where arrays

Posted on 2016-09-22
6
Medium Priority
?
143 Views
Last Modified: 2016-09-27
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
Comment
Question by:Johann Buve
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 41811096
Can you post the sample line which you trying to match in the inf file?
0
 
LVL 41

Expert Comment

by:footech
ID: 41811117
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
 

Author Comment

by:Johann Buve
ID: 41812100
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 41812267
$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
 

Author Comment

by:Johann Buve
ID: 41812277
I tried and it works perfectly!
Thank you a lot Subsun!
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41812299
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question