Solved

Powershell / Where arrays

Posted on 2016-09-22
6
81 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 40

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
Independent Software Vendors: 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!

 
LVL 40

Accepted Solution

by:
Subsun earned 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

738 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