Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://www.reddit.com/r/PowerShell/comments/l8nfjg/learning_ps_have_an_idea_in_mind_to_learn/
- # how to update installscript_v2.json
- # copy just the scriptblock part
- # get-clipboard | convertfrom-json | set-clipboard
- # paste and make changes
- # select all and copy
- # Get-Clipboard | out-string | ConvertTo-Json | Set-Clipboard
- # paste and only copy part between braces then paste into .json file
- #< run as admin
- if ((net session 2>&1) -match 'denied') {
- start powershell "-encodedcommand $([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($script:MyInvocation.MyCommand.ScriptBlock)))" -Verb RunAs
- exit
- }
- #>
- # import
- Add-Type -AssemblyName System.Windows.Forms
- [System.Windows.Forms.Application]::EnableVisualStyles()
- # functions
- function Expand-ZIPFile {
- param (
- [string]$file,
- [string]$destination
- )
- if (!$destination) {
- $destination = [string](Resolve-Path $file)
- $destination = $destination.Substring(0, $destination.LastIndexOf('.'))
- mkdir $destination | Out-Null
- }
- $shell = New-Object -ComObject Shell.Application
- #$shell.NameSpace($destination).CopyHere($shell.NameSpace($file).Items(), 16);
- $zip = $shell.NameSpace($file)
- foreach ($item in $zip.items()) {
- $shell.Namespace($destination).CopyHere($item)
- }
- }
- function Create-Shortcut {
- param (
- [string]$Source,
- [string]$DestinationLnk,
- [string]$Arguments
- )
- begin {
- $WshShell = New-Object -ComObject WScript.Shell
- }
- process {
- if (!$Source) {Throw 'No Source'}
- if (!$DestinationLnk) {Throw 'No DestinationLnk'}
- $Shortcut = $WshShell.CreateShortcut($DestinationLnk)
- $Shortcut.TargetPath = $Source
- if ($Arguments) {
- $Shortcut.Arguments = $Arguments
- }
- $Shortcut.Save()
- }
- end {
- function Release-Ref ($ref) {
- ([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
- [System.GC]::Collect()
- [System.GC]::WaitForPendingFinalizers()
- }
- $Shortcut, $WshShell | % {$null = Release-Ref $_}
- }
- }
- # work
- # remove existing objects
- try {
- gv frm* | rv -ErrorAction Stop
- } catch {}
- $numobjects = 0
- $numobjects++
- $frmAll = New-Object System.Windows.Forms.CheckBox
- $frmAll.text = 'All'
- $frmAll.autosize = $true
- $frmAll.location = New-Object System.Drawing.Point(5, ($numobjects * 25 + 5 - 25))
- $frmAll.Add_CheckStateChanged({
- if ($frmAll.Checked) {
- gv frm* -ValueOnly | ? {$_.AccessibilityObject.Role -eq 'CheckButton' -and $_.enabled -and $_.name} | % {$_.checked = $true}
- } else {
- gv frm* -ValueOnly | ? {$_.AccessibilityObject.Role -eq 'CheckButton' -and $_.enabled -and $_.name} | % {$_.checked = $false}
- }
- })
- # find a way to get flash drive letter
- $flashdrive = [io.driveinfo]::getdrives() | % {$_.name} | ? {Test-Path $_\e2b.ico} | select -f 1
- $flashdrive = $flashdrive.TrimEnd('\')
- # json file cannot have blank line at top
- $config = gc "$flashdrive\Documents\Windows 10\installscript_v2.json" | ConvertFrom-Json
- foreach ($obj in $config) {
- $numobjects++
- # each object must have control and name
- New-Variable -Name $obj.name -Value (iex $obj.control)
- $props = $obj | gm -mem NoteProperty | % name | ? {$_ -ne 'control'}
- foreach ($prop in $props) {
- try {
- $(Get-Variable $obj.name -ValueOnly).$prop = iex $obj.$prop
- } catch {
- try {
- $(Get-Variable $obj.name -ValueOnly).$prop = $obj.$prop
- } catch {
- Add-Member -InputObject $(Get-Variable $obj.name -ValueOnly) -MemberType NoteProperty -Name $prop -Value $obj.$prop
- }
- }
- }
- #$(Get-Variable $obj.name -ValueOnly).width = 95
- #(Get-Variable $obj.name -ValueOnly).height = 20
- $(Get-Variable $obj.name -ValueOnly).autosize = $true
- $objx = 5
- $objy = $numobjects * 25 + 5 - 25
- $(Get-Variable $obj.name -ValueOnly).location = New-Object System.Drawing.Point($objx, $objy)
- }
- Write-Host (gv frm* | Out-String)
- $numobjects++
- $frmButton1 = New-Object system.Windows.Forms.Button
- $frmButton1.text = 'Run'
- $frmButton1.width = 60
- $frmButton1.height = 20
- $frmButton1.location = New-Object System.Drawing.Point(5, ($numobjects * 25 + 5 - 25))
- $frmButton1.add_Click({
- gv frm* -ValueOnly | ? {$_.checked -and $_.scriptblock} | % {. (iex $_.scriptblock)}
- })
- $numobjects++
- $frmButton2 = New-Object system.Windows.Forms.Button
- $frmButton2.text = 'Exit'
- $frmButton2.width = 60
- $frmButton2.height = 20
- $frmButton2.location = New-Object System.Drawing.Point(5, ($numobjects * 25 + 5 - 25))
- $frmButton2.add_Click({ $Form.Close() })
- # form stuff
- #$formwidth = 5 + 5 + 95
- $formwidth = gv frm* -ValueOnly | % width | measure -Maximum | % maximum
- $formheight = $numobjects * 25 + 5
- $formsize = '' + $formwidth + ',' + $formheight
- $Form = New-Object system.Windows.Forms.Form
- $Form.ClientSize = $formsize
- $Form.text = 'Form'
- $Form.TopMost = $false
- $Form.controls.AddRange(@(gv frm* -ValueOnly))
- [void]$Form.ShowDialog()
Add Comment
Please, Sign In to add comment