Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge multiple PowerShell scripts with the same objective - multi-dimensional conditionals

I'm using PowerShell to configure systems after WDS deployment. Currently I have a script for each OS and use case, called automatically via an Unattend.xml file.

Now my problem is that when I want to change something general, I have to change it in every of these scripts.

So I thought, I should merge them all together and pass the mode as argument (for example customer) or read it from the system (for example when I want to run specific commands just for one os version or manufacturer).

I read a lot about switches here, but I'm not sure if this is the best practice for this case.

I would start the script defining the arguments

Param([string]$customer)
$manufacturer = (Get-CimInstance Win32_ComputerSystem).Manufacturer
$os = (Get-CimInstance Win32_OperatingSystem).version

This are parts of the scripts I pasted together:

Write-Output "runnig deskupdate..."
    Start-Process "$install\deskupdate\ducmd.exe" -ArgumentList "/WEB /DRV" -NoNewWindow -Wait

Write-Output "installing java..."
    Get-ChildItem $install\programs -Filter "jre-*" | ForEach {Start-Process $_.Fullname -ArgumentList "/s" -NoNewWindow -Wait}

Write-Output "installing 7zip..."
    Get-ChildItem $install\programs -Filter "7z*" | ForEach {Start-Process $_.Fullname -ArgumentList "/S" -NoNewWindow -Wait}
Write-Output "deactivating uac..."  
    New-ItemProperty -Path HKLM:Software\Microsoft\Windows\CurrentVersion\policies\system -Name EnableLUA -PropertyType DWord -Value 0 -Force

Now I'd like to have it so separated, that I can simply

  • run deskupdate only on fujitsu systems
  • install 7zip everywhere
  • don't install java at customer shop_a
  • deactivate uac only at customer shop_a

Sure, I could use an if-clause for every command, but I'm looking for a nice and maintainable solution. What would you recommend?

This is my first question here, please let me know, whether I have formulated it clearly enough. And sorry for spelling and grammatical mistakes, my native language is German.

like image 570
Semolor Avatar asked Jun 02 '26 06:06

Semolor


1 Answers

PowerShell's switch statement is quite flexible:

Param([string] $customer)

$manufacturer = (Get-CimInstance Win32_ComputerSystem).Manufacturer

switch (@{ manufacturer = $manufacturer; customer = $customer }) {
  { $_.manufacturer -eq 'Fujitsu' } { 'run deskupdate' }
  { $True }                         { 'install 7zip'   } # unconditional action
  { $_.customer -ne 'shop_A' }      { 'install java'   }
  { $_.customer -eq 'shop_A' }      { 'deactivate UAC' }
}

Note how the two dimensions to act on - manufacturer and customer - are passed via a hashtable (@{ ...; ... }).

The conditionals in the form of script blocks ({ ... }) can then access the hashtable as $_ and query its properties; similarly, the associated action script blocks have access to the hashtable via $_ as well.

Each matching conditional is evaluated independently, unless an associated script block executes break.

like image 181
mklement0 Avatar answered Jun 06 '26 05:06

mklement0