Solved

Powershell / Where arrays

Posted on 2016-09-22
6
46 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A brief introduction to what I consider to be the best editor for PowerShell.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

929 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

20 Experts available now in Live!

Get 1:1 Help Now