• Home
  • /
  • 2023
  • /
  • Only pass parameter if value is supplied

Only pass parameter if value is supplied

To reduce lines of code, it would be nice if you can only add parameter if they have a value. For instance when you want to install a PowerShell-Module. The way I did it before is like this:

[IST]

$ModuleName = "MicrosoftTeams"
$MinimumModuleVersion = $null

if ( $MinimumModuleVersion ) {
   if ( $null -eq ( Get-InstalledModule -Name $ModuleName -MinimumVersion $MinimumModuleVersion)) {
      Install-Module -Name $ModuleName -MinimumVersion $MinimumModuleVersion -Force -Scope CurrentUser -WhatIf:$WhatIfPreference
   }
} else {
   if ( $null -eq ( Get-InstalledModule -Name $ModuleName )) {
      Install-Module -Name $ModuleName -Force -Scope CurrentUser -WhatIf:$WhatIfPreference
   }
}
Import-Module -Name $ModuleName -Force

[SOLL]

It would be nice, if there was something like this:

$ModuleName = "MicrosoftTeams"
$MinimumModuleVersion = $null

if ( $MinimumModuleVersion ) {
   if ( $null -eq ( Get-InstalledModule -Name $ModuleName if ( $MinimumModuleVersion) { -MinimumVersion $MinimumModuleVersion} )) {
      Install-Module -Name $ModuleName -MinimumVersion $MinimumModuleVersion -Force -Scope CurrentUser  -WhatIf:$WhatIfPreference
   }
}
Import-Module -Name $ModuleName -Force -WhatIf:$WhatIfPreference

[Solved]

Unfurtunately that wasn’t working. After some digging on the interweb and trial and error, I found a shorter way to write this. When $MaximumRequiredModuleVersion equals $null, then get the latest version; else, use the provided version:

# Splatting with hash tables
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7.3&viewFallbackFrom=powershell-6

$ModuleName = 'MicrosoftTeams'
$MaximumRequiredModuleVersion  = $null 

begin {
    $params = @{
        Name = $ModuleName;
        Force = $true;
        Scope = 'CurrentUser';
        MaximumVersion = if( $MaximumRequiredModuleVersion -ne $null ) { $MaximumRequiredModuleVersion } else { (Find-Module -Name $ModuleName).Version };
    }
}
process {
    Install-Module @params -WhatIf:$WhatIfPreference
}
Import-Module -Name $ModuleName -Force -WhatIf:$WhatIfPreference

[SOLVED with Update]

And here the complete script with update:

$ModuleName = "MicrosoftTeams"
$MaximumRequiredModuleVersion = '5.1.0'

if ( $null -eq (Get-InstalledModule -Name $ModuleName -ErrorAction SilentlyContinue)) {
   Write-Host "`t...module [$ModuleName] with version [$MaximumRequiredModuleVersion] isn't installed"
   Write-Host "`t...install [$ModuleName] with version [$MaximumRequiredModuleVersion]"

   # Install Module
   $params = @{
      Name = $ModuleName;
      Force = $true;
      Scope = 'CurrentUser';
      MaximumVersion = if( $null -ne $MaximumRequiredModuleVersion ) { $MaximumRequiredModuleVersion } 
         else { (Find-Module -Name $ModuleName).Version };
   }
   Install-Module @params -WhatIf:$WhatIfPreference

   Write-Host "`t...done"
   Write-Host ""
} else {
   Write-Host "`t...check if required version [$MaximumRequiredModuleVersion] is installed"

   $InstalledModuleVersion = (Get-InstalledModule -Name $ModuleName).Version
   if ($InstalledModuleVersion -lt $MaximumRequiredModuleVersion) {
      Write-Host "`t...installed module version [$InstalledModuleVersion] is older than required module version [$MaximumRequiredModuleVersion]"
      Write-Host "`t...update module [$ModuleName] to required module version [$MaximumRequiredModuleVersion]"

      # Update Module
      $params = @{
         Name = $ModuleName;
         Force = $true;
         RequiredVersion = if( $null -ne $MaximumRequiredModuleVersion ) { $MaximumRequiredModuleVersion } 
            else { (Find-Module -Name $ModuleName).Version };
      }
      Update-Module @params -WhatIf:$WhatIfPreference
   } else {
      Write-Host "`t...required version [$MaximumRequiredModuleVersion] is installed"
   }
} 


0
0

Leave a Reply