Optimize-Windows.ps1
#requires -RunAsAdministrator $PresetPath=(Join-Path $PSScriptRoot "presets") Function Optimize-Windows() { [CmdletBinding()] param( [Parameter(Mandatory = $True)] [ValidateScript( { $_ -in (Get-ChildItem -path $PresetPath *.ps1).basename } #, ErrorMessage = 'Please specify a valid preset, i.e. "Minimalist"' #only works in PS 7+ )] [ArgumentCompleter( { param($cmd, $param, $wordToComplete) [array] $validValues = (Get-ChildItem -path $PresetPath *.ps1).basename $validValues -like "$wordToComplete*" } )] [String]$Preset) . (Join-Path $PresetPath "$Preset.ps1") #get the list of tasks and inputs Write-Host "Do you want to execute the following ${tasks.count} Tasks?" $Tasks | ForEach-Object { $Synopsis=Select-Command $_| Get-Synopsis Write-Host "☐ $Synopsis" } Wait-Keypress Write-Information "Reading Inputs" $Inputs.GetEnumerator() | ForEach-Object { #todo: handle secret with Read-Password # if not interactive and has a default if (-not [Environment]::UserInteractive -and $_.Value.Default) { $Value=$_.Value.Default }elseif( -not [Environment]::UserInteractive -and -not $_.Value.Default) { Write-Error "Missing mandatory input $($_.Key)" } if($_.Secret -eq $true){ $Value=Read-Password -Prompt $_.Value.Title }else{ $Value = Read-Host -Prompt $_.Value.Title } Write-Host $Value New-Variable -Name $_.Key -Value $Value -Force -Scope Global } Write-Information "Processing ${Tasks.count} Tasks" $Tasks | ForEach-Object { $Synopsis=Select-Command $_| Get-Synopsis if ($Synopsis -eq $null) { $Synopsis="$($_.ToString())" } Write-Progress -Activity "Optimize Windows" -Status "$Synopsis" -PercentComplete ($_.Value.count / $Tasks.count * 100) Write-Host $Synopsis Invoke-Command $_ | out-null } } |