1

I'd like to make Powershell script which get specific string from the line. This string might be on different lines. So text file looks like this - 1st file -

[Bootstrap]
buildid=400m3(Build:9702)
ProductBuildid=9702
ProductCode={55E61709-D7D4-43C0-B45D-BFAF5C09A02D}
UpgradeCode={7C35B9AB-2CE3-4C18-BE7C-5B97EA089EB3}

2nd file -

[Bootstrap]
ProductCode={2BB8FBB4-CFF9-434E-AA0A-40F5379C1602}

I need to get MSI code after ProductCode=

$openofficeSetup = "C:\Program Files (x86)\openoffice*\program\setup.ini"
if (Test-Path $openofficeSetup)
{
$openofficeMSI = Select-String "ProductCode=*" $openofficeSetup
$openofficeMSI = $openofficeMSI -Replace "*ProductCode=", ""
msiexec.exe /x $openofficeMSI /qn

Line 5 with -Replace is wrong. i have no idea how to remove everything before.

PS N:\> echo $openofficeMSI
C:\Program Files (x86)\OpenOffice 4\program\setup.ini:4:ProductCode={55E61709-D7D4-43C0-B45D-BFAF5C09A02D}

How can I get rid of everything but MSI code?

Offtopic: Other way would searching for MSI code in registry HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\ but using setup.ini looks easier.

2
  • MSI code has 38 characters. Maybe simply I should grab last 38 characters then... Commented Oct 8, 2013 at 11:27
  • Paths can't contain asterisks. Commented Oct 8, 2013 at 14:27

1 Answer 1

1

I would use [regex]::Match instead of select-string:

$openofficeSetup = Resolve-Path "C:\Program Files (x86)\openoffice*\program\setup.ini" 
if (Test-Path $openofficeSetup){
  $txt=[IO.File]::ReadAllText($openofficeSetup )
  $openofficeMSI = [regex]::Match($txt,'ProductCode=(.*)').Groups[1].Value
  msiexec.exe /x $openofficeMSI /qn
}
Sign up to request clarification or add additional context in comments.

3 Comments

I got an error, sorry it is in German: Ausnahme beim Aufrufen von "ReadAllText" mit 1 Argument(en): "Illegales Zeichen im Pfad." Bei Zeile:3 Zeichen:28 + $txt=[IO.File]::ReadAllText <<<< ($openofficeSetup) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
"Illegales Zeichen im Pfad" means "illegal character in path," i.e., the asterisk.
I missed the asterisk in the path. Yes, ReadAllText doesn't like wildcards. Resolve-Path to the rescue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.