Donnerstag, 19. Mai 2016

Create user interface dynamically, where users can select from a choice

$patchNames = ("P1", "P2", "P3")  # You probably want to create that dynamically. This seems to be the whole point.
$patchIndex = -1
$patchOptions = @()
foreach ($patchName in $patchNames) {
      $patchIndex++
      $patchOptions += New-Object `
         System.Management.Automation.Host.ChoiceDescription `
         "${patchName}"
}
$options = [System.Management.Automation.Host.ChoiceDescription[]]$patchOptions
$patchIndex = $host.ui.PromptForChoice(
   "Patch-Auswahl",
   "Bitte den Patch anhand des Namens auswählen (z. B. Copy-Paste). " + `
   "Vorausgewählt ist der jüngste, der mit einfachem Enter-Drücken " + `
   "ausgewählt wird",
   $options,
   0
)
$patchName = $patchNames[${patchIndex}]

Zip files and directories

Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
$zipFilePath = `
   $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(
         "${scriptDirPath}\..\software-lieferung\${patchName}.zip"
   )
if (Test-Path -path "${zipFilePath}") {
      Write-Debug("Removing ${zipFilePath}")
      Remove-Item -path "${zipFilePath}" -force
}
[System.IO.Compression.ZipFile]::CreateFromDirectory(
      "${tmpPatchDirPath}",
      "${zipFilePath}",
      $compressionLevel,
      $False
)