Invoke-WinGetCommand.ps1 (7945B)
1 filter Assert-WhiteSpaceIsNull { 2 IF ([string]::IsNullOrWhiteSpace($_)){$null} 3 ELSE {$_} 4 } 5 6 class WinGetSource 7 { 8 [string] $Name 9 [string] $Argument 10 [string] $Data 11 [string] $Identifier 12 [string] $Type 13 14 WinGetSource () 15 { } 16 17 WinGetSource ([string]$a, [string]$b, [string]$c, [string]$d, [string]$e) 18 { 19 $this.Name = $a.TrimEnd() | Assert-WhiteSpaceIsNull 20 $this.Argument = $b.TrimEnd() | Assert-WhiteSpaceIsNull 21 $this.Data = $c.TrimEnd() | Assert-WhiteSpaceIsNull 22 $this.Identifier = $d.TrimEnd() | Assert-WhiteSpaceIsNull 23 $this.Type = $e.TrimEnd() | Assert-WhiteSpaceIsNull 24 } 25 26 WinGetSource ([string[]]$a) 27 { 28 $this.name = $a[0].TrimEnd() | Assert-WhiteSpaceIsNull 29 $this.Argument = $a[1].TrimEnd() | Assert-WhiteSpaceIsNull 30 $this.Data = $a[2].TrimEnd() | Assert-WhiteSpaceIsNull 31 $this.Identifier = $a[3].TrimEnd() | Assert-WhiteSpaceIsNull 32 $this.Type = $a[4].TrimEnd() | Assert-WhiteSpaceIsNull 33 } 34 35 WinGetSource ([WinGetSource]$a) 36 { 37 $this.Name = $a.Name.TrimEnd() | Assert-WhiteSpaceIsNull 38 $this.Argument = $a.Argument.TrimEnd() | Assert-WhiteSpaceIsNull 39 $this.Data = $a.Data.TrimEnd() | Assert-WhiteSpaceIsNull 40 $this.Identifier = $a.Identifier.TrimEnd() | Assert-WhiteSpaceIsNull 41 $this.Type = $a.Type.TrimEnd() | Assert-WhiteSpaceIsNull 42 43 } 44 45 [WinGetSource[]] Add ([WinGetSource]$a) 46 { 47 $FirstValue = [WinGetSource]::New($this) 48 $SecondValue = [WinGetSource]::New($a) 49 50 [WinGetSource[]] $Combined = @([WinGetSource]::New($FirstValue), [WinGetSource]::New($SecondValue)) 51 52 Return $Combined 53 } 54 55 [WinGetSource[]] Add ([String[]]$a) 56 { 57 $FirstValue = [WinGetSource]::New($this) 58 $SecondValue = [WinGetSource]::New($a) 59 60 [WinGetSource[]] $Combined = @([WinGetSource]::New($FirstValue), [WinGetSource]::New($SecondValue)) 61 62 Return $Combined 63 } 64 } 65 66 class WinGetPackage 67 { 68 [string]$Name 69 [string]$Id 70 [string]$Version 71 [string]$Available 72 [string]$Source 73 [string]$Match 74 75 WinGetPackage ([string] $a, [string]$b, [string]$c, [string]$d, [string]$e) 76 { 77 $this.Name = $a.TrimEnd() | Assert-WhiteSpaceIsNull 78 $this.Id = $b.TrimEnd() | Assert-WhiteSpaceIsNull 79 $this.Version = $c.TrimEnd() | Assert-WhiteSpaceIsNull 80 $this.Available = $d.TrimEnd() | Assert-WhiteSpaceIsNull 81 $this.Source = $e.TrimEnd() | Assert-WhiteSpaceIsNull 82 } 83 84 WinGetPackage ([WinGetPackage] $a) { 85 $this.Name = $a.Name | Assert-WhiteSpaceIsNull 86 $this.Id = $a.Id | Assert-WhiteSpaceIsNull 87 $this.Version = $a.Version | Assert-WhiteSpaceIsNull 88 $this.Available = $a.Available | Assert-WhiteSpaceIsNull 89 $this.Source = $a.Source | Assert-WhiteSpaceIsNull 90 91 } 92 WinGetPackage ([psobject] $a) { 93 $this.Name = $a.Name | Assert-WhiteSpaceIsNull 94 $this.Id = $a.Id | Assert-WhiteSpaceIsNull 95 $this.Version = $a.Version | Assert-WhiteSpaceIsNull 96 $this.Available = $a.Available | Assert-WhiteSpaceIsNull 97 $this.Source = $a.Source | Assert-WhiteSpaceIsNull 98 } 99 100 WinGetSource ([string[]]$a) 101 { 102 $this.name = $a[0].TrimEnd() | Assert-WhiteSpaceIsNull 103 $this.Id = $a[1].TrimEnd() | Assert-WhiteSpaceIsNull 104 $this.Version = $a[2].TrimEnd() | Assert-WhiteSpaceIsNull 105 $this.Available = $a[3].TrimEnd() | Assert-WhiteSpaceIsNull 106 $this.Source = $a[4].TrimEnd() | Assert-WhiteSpaceIsNull 107 } 108 109 110 [WinGetPackage[]] Add ([WinGetPackage] $a) 111 { 112 $FirstValue = [WinGetPackage]::New($this) 113 $SecondValue = [WinGetPackage]::New($a) 114 115 [WinGetPackage[]]$Result = @([WinGetPackage]::New($FirstValue), [WinGetPackage]::New($SecondValue)) 116 117 Return $Result 118 } 119 120 [WinGetPackage[]] Add ([String[]]$a) 121 { 122 $FirstValue = [WinGetPackage]::New($this) 123 $SecondValue = [WinGetPackage]::New($a) 124 125 [WinGetPackage[]] $Combined = @([WinGetPackage]::New($FirstValue), [WinGetPackage]::New($SecondValue)) 126 127 Return $Combined 128 } 129 } 130 Function Invoke-WinGetCommand 131 { 132 PARAM( 133 [Parameter(Position=0, Mandatory=$true)] [string[]]$WinGetArgs, 134 [Parameter(Position=0, Mandatory=$true)] [string[]]$IndexTitles, 135 [Parameter()] [switch] $JSON 136 ) 137 BEGIN 138 { 139 $Index = @() 140 $Result = @() 141 $i = 0 142 $IndexTitlesCount = $IndexTitles.Count 143 $Offset = 0 144 $Found = $false 145 146 ## Remove two characters from the string length and add "..." to the end (only if there is the three below characters present). 147 [string[]]$WinGetSourceListRaw = & "WinGet" $WingetArgs | out-string -stream | foreach-object{$_ -replace ("$([char]915)$([char]199)$([char]170)", "$([char]199)")} 148 } 149 PROCESS 150 { 151 if($JSON){ 152 ## If expecting JSON content, return the object 153 return $WinGetSourceListRaw | ConvertFrom-Json 154 } 155 156 ## Gets the indexing of each title 157 $regex = $IndexTitles -join "|" 158 for ($Offset=0; $Offset -lt $WinGetSourceListRaw.Length; $Offset++) { 159 if($WinGetSourceListRaw[$Offset].Split(" ")[0].Trim() -match $regex) { 160 $Found = $true 161 break 162 } 163 } 164 if(!$Found) { 165 Write-Error -Message "No results were found." -TargetObject $WinGetSourceListRaw 166 return 167 } 168 169 foreach ($IndexTitle in $IndexTitles) { 170 ## Creates an array of titles and their string location 171 $IndexStart = $WinGetSourceListRaw[$Offset].IndexOf($IndexTitle) 172 $IndexEnds = "" 173 174 IF($IndexStart -ne "-1") { 175 $Index += [pscustomobject]@{ 176 Title = $IndexTitle 177 Start = $IndexStart 178 Ends = $IndexEnds 179 } 180 } 181 } 182 183 ## Orders the Object based on Index value 184 $Index = $Index | Sort-Object Start 185 186 ## Sets the end of string value 187 while ($i -lt $IndexTitlesCount) { 188 $i ++ 189 190 ## Sets the End of string value (if not null) 191 if($Index[$i].Start) { 192 $Index[$i-1].Ends = ($Index[$i].Start -1) - $Index[$i-1].Start 193 } 194 } 195 196 ## Builds the WinGetSource Object with contents 197 $i = $Offset + 2 198 while($i -lt $WinGetSourceListRaw.Length) { 199 $row = $WinGetSourceListRaw[$i] 200 try { 201 [bool] $TestNotTitles = $WinGetSourceListRaw[0] -ne $row 202 [bool] $TestNotHyphenLine = $WinGetSourceListRaw[1] -ne $row -and !$Row.Contains("---") 203 [bool] $TestNotNoResults = $row -ne "No package found matching input criteria." 204 } 205 catch {Wait-Debugger} 206 207 if(!$TestNotNoResults) { 208 Write-LogEntry -LogEntry "No package found matching input criteria." -Severity 1 209 } 210 211 ## If this is the first pass containing titles or the table line, skip. 212 if($TestNotTitles -and $TestNotHyphenLine -and $TestNotNoResults) { 213 $List = @{} 214 215 foreach($item in $Index) { 216 if($Item.Ends) { 217 $List[$Item.Title] = $row.SubString($item.Start,$Item.Ends) 218 } 219 else { 220 $List[$item.Title] = $row.SubString($item.Start, $row.Length - $Item.Start) 221 } 222 } 223 224 $result += [pscustomobject]$list 225 } 226 $i++ 227 } 228 } 229 END 230 { 231 return $Result 232 } 233 } 234