Reputation: 487
I feel like I'm missing something obvious but my mind's gone blank.....
I have the following output
PS C:\Users\User1> $TargetComputers
ComputerName UninstallString
------------ ---------------
Server1 MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}
Server2 MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}
Server3 MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}
Server4 C:\Program Files\7-Zip\Uninstall.exe
Server5 MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}
Server6 MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}
Server7 {"D:\Program Files\7-Zip\Uninstall.exe", MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}}
Server8 MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}
Server9 MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}
How do I manipulate the UninstallString column to leave me just {GUID}, including literal path where specified, when I call $TargetComputers?
Upvotes: 0
Views: 143
Reputation: 27756
# Sample data
$TargetComputers = @(
@{ ComputerName='server1'; UninstallString='MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}' }
@{ ComputerName='server1'; UninstallString='{"D:\Program Files\7-Zip\Uninstall.exe", MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}}' }
@{ ComputerName='server1'; UninstallString='C:\Program Files\7-Zip\Uninstall.exe' }
) | % { [PSCustomObject] $_ }
$TargetComputers | ForEach-Object {
$_.UninstallString = $_.UninstallString -replace '.*/I{(.+?)}.*', '$1'
}
$TargetComputers
Output:
ComputerName UninstallString ------------ --------------- server1 A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9 server1 36648B37-EA03-4349-8C49-C26032D06C61 server1 C:\Program Files\7-Zip\Uninstall.exe
Explanation:
-replace
operator, we are using regular expression (RegEx) pattern matching. The 1st string after -replace
is the search string, the 2nd string is the substitute. If the RegEx doesn't match, the original string will be returned..*
- any character, zero or more times/I{
- literal sub string(
- starts a capture group
.+?
- any character, one or more times, as little as possible)
- ends the capture group}
- literal sub string.*
- any character, zero or more times$1
- replaces the string with the content of the 1st capture group (the GUID value)Upvotes: 2
Reputation: 128
Try with:
$TargetComputers = $TargetComputers | ForEach-Object {
$matches = [Regex]::Matches($_.UninstallString, ({.*}),"IgnoreCase")
if ( $null -ne $matches.Groups ) {
$_.UninstallString = $matches.Groups[1].Value
}
return $_
}
Upvotes: 0