gangstanthony

installscriptv2

Jan 30th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://www.reddit.com/r/PowerShell/comments/l8nfjg/learning_ps_have_an_idea_in_mind_to_learn/
  2.  
  3. # how to update installscript_v2.json
  4. # copy just the scriptblock part
  5. # get-clipboard | convertfrom-json | set-clipboard
  6. # paste and make changes
  7. # select all and copy
  8. # Get-Clipboard | out-string | ConvertTo-Json | Set-Clipboard
  9. # paste and only copy part between braces then paste into .json file
  10.  
  11. #< run as admin
  12. if ((net session 2>&1) -match 'denied') {
  13.     start powershell "-encodedcommand $([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($script:MyInvocation.MyCommand.ScriptBlock)))" -Verb RunAs
  14.     exit
  15. }
  16. #>
  17.  
  18. # import
  19.  
  20. Add-Type -AssemblyName System.Windows.Forms
  21. [System.Windows.Forms.Application]::EnableVisualStyles()
  22.  
  23. # functions
  24.  
  25. function Expand-ZIPFile {
  26.     param (
  27.         [string]$file,
  28.         [string]$destination
  29.     )
  30.  
  31.     if (!$destination) {
  32.         $destination = [string](Resolve-Path $file)
  33.         $destination = $destination.Substring(0, $destination.LastIndexOf('.'))
  34.         mkdir $destination | Out-Null
  35.     }
  36.     $shell = New-Object -ComObject Shell.Application
  37.     #$shell.NameSpace($destination).CopyHere($shell.NameSpace($file).Items(), 16);
  38.     $zip = $shell.NameSpace($file)
  39.     foreach ($item in $zip.items()) {
  40.         $shell.Namespace($destination).CopyHere($item)
  41.     }
  42. }
  43.  
  44. function Create-Shortcut {
  45.     param (
  46.         [string]$Source,
  47.         [string]$DestinationLnk,
  48.         [string]$Arguments
  49.     )
  50.  
  51.     begin {
  52.         $WshShell = New-Object -ComObject WScript.Shell
  53.     }
  54.  
  55.     process {
  56.         if (!$Source) {Throw 'No Source'}
  57.         if (!$DestinationLnk) {Throw 'No DestinationLnk'}
  58.  
  59.         $Shortcut = $WshShell.CreateShortcut($DestinationLnk)
  60.         $Shortcut.TargetPath = $Source
  61.         if ($Arguments) {
  62.             $Shortcut.Arguments = $Arguments
  63.         }
  64.         $Shortcut.Save()
  65.     }
  66.  
  67.     end {
  68.         function Release-Ref ($ref) {
  69.             ([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
  70.             [System.GC]::Collect()
  71.             [System.GC]::WaitForPendingFinalizers()
  72.         }
  73.         $Shortcut, $WshShell | % {$null = Release-Ref $_}
  74.     }
  75. }
  76.  
  77. # work
  78.  
  79. # remove existing objects
  80. try {
  81.     gv frm* | rv -ErrorAction Stop
  82. } catch {}
  83.  
  84. $numobjects = 0
  85.  
  86. $numobjects++
  87. $frmAll                         = New-Object System.Windows.Forms.CheckBox
  88. $frmAll.text                    = 'All'
  89. $frmAll.autosize                = $true
  90. $frmAll.location                = New-Object System.Drawing.Point(5, ($numobjects * 25 + 5 - 25))
  91. $frmAll.Add_CheckStateChanged({
  92.     if ($frmAll.Checked) {
  93.         gv frm* -ValueOnly | ? {$_.AccessibilityObject.Role -eq 'CheckButton' -and $_.enabled -and $_.name} | % {$_.checked = $true}
  94.     } else {
  95.         gv frm* -ValueOnly | ? {$_.AccessibilityObject.Role -eq 'CheckButton' -and $_.enabled -and $_.name} | % {$_.checked = $false}
  96.     }
  97. })
  98.  
  99. # find a way to get flash drive letter
  100. $flashdrive = [io.driveinfo]::getdrives() | % {$_.name} | ? {Test-Path $_\e2b.ico} | select -f 1
  101. $flashdrive = $flashdrive.TrimEnd('\')
  102. # json file cannot have blank line at top
  103. $config = gc "$flashdrive\Documents\Windows 10\installscript_v2.json" | ConvertFrom-Json
  104.  
  105. foreach ($obj in $config) {
  106.     $numobjects++
  107.  
  108.     # each object must have control and name
  109.     New-Variable -Name $obj.name -Value (iex $obj.control)
  110.    
  111.     $props = $obj | gm -mem NoteProperty | % name | ? {$_ -ne 'control'}
  112.  
  113.     foreach ($prop in $props) {
  114.         try {
  115.             $(Get-Variable $obj.name -ValueOnly).$prop = iex $obj.$prop
  116.         } catch {
  117.             try {
  118.                 $(Get-Variable $obj.name -ValueOnly).$prop = $obj.$prop
  119.             } catch {
  120.                 Add-Member -InputObject $(Get-Variable $obj.name -ValueOnly) -MemberType NoteProperty -Name $prop -Value $obj.$prop
  121.             }
  122.         }
  123.     }
  124.    
  125.     #$(Get-Variable $obj.name -ValueOnly).width = 95
  126.     #(Get-Variable $obj.name -ValueOnly).height = 20
  127.     $(Get-Variable $obj.name -ValueOnly).autosize = $true
  128.  
  129.     $objx = 5
  130.     $objy = $numobjects * 25 + 5 - 25
  131.  
  132.     $(Get-Variable $obj.name -ValueOnly).location = New-Object System.Drawing.Point($objx, $objy)
  133. }
  134.  
  135. Write-Host (gv frm* | Out-String)
  136.  
  137. $numobjects++
  138. $frmButton1                         = New-Object system.Windows.Forms.Button
  139. $frmButton1.text                    = 'Run'
  140. $frmButton1.width                   = 60
  141. $frmButton1.height                  = 20
  142. $frmButton1.location                = New-Object System.Drawing.Point(5, ($numobjects * 25 + 5 - 25))
  143. $frmButton1.add_Click({
  144.     gv frm* -ValueOnly | ? {$_.checked -and $_.scriptblock} | % {. (iex $_.scriptblock)}
  145. })
  146.  
  147. $numobjects++
  148. $frmButton2                         = New-Object system.Windows.Forms.Button
  149. $frmButton2.text                    = 'Exit'
  150. $frmButton2.width                   = 60
  151. $frmButton2.height                  = 20
  152. $frmButton2.location                = New-Object System.Drawing.Point(5, ($numobjects * 25 + 5 - 25))
  153. $frmButton2.add_Click({ $Form.Close() })
  154.  
  155. # form stuff
  156.  
  157. #$formwidth = 5 + 5 + 95
  158. $formwidth = gv frm* -ValueOnly | % width | measure -Maximum | % maximum
  159. $formheight = $numobjects * 25 + 5
  160. $formsize = '' + $formwidth + ',' + $formheight
  161.  
  162. $Form                            = New-Object system.Windows.Forms.Form
  163. $Form.ClientSize                 = $formsize
  164. $Form.text                       = 'Form'
  165. $Form.TopMost                    = $false
  166.  
  167. $Form.controls.AddRange(@(gv frm* -ValueOnly))
  168.  
  169. [void]$Form.ShowDialog()
  170.  
  171.  
Add Comment
Please, Sign In to add comment