Solved

Powershell / Where arrays

Posted on 2016-09-22
6
43 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
  • 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 39

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now