Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

PowerShell: Manipulate existing variable field values

I feel like I'm missing something obvious but my mind's gone blank.....

I have the following output

PS C:UsersUser1> $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 Files7-ZipUninstall.exe                                                          
Server5        MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}                                          
Server6        MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}                                          
Server7        {"D:Program Files7-ZipUninstall.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?

question from:https://stackoverflow.com/questions/65943568/powershell-manipulate-existing-variable-field-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
# Sample data
$TargetComputers = @(
    @{ ComputerName='server1'; UninstallString='MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}' }
    @{ ComputerName='server1'; UninstallString='{"D:Program Files7-ZipUninstall.exe", MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}}' }
    @{ ComputerName='server1'; UninstallString='C:Program Files7-ZipUninstall.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 Files7-ZipUninstall.exe

Explanation:

  • By using the -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.
  • RegEx search string breakdown:
    • .* - 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
  • RegEx substitute:
    • $1 - replaces the string with the content of the 1st capture group (the GUID value)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...