Home > Software engineering >  How to translate -WhatIf into a switch of a native command
How to translate -WhatIf into a switch of a native command

Time:01-04

My advanced script (with [CmdletBinding(SupportsShouldProcess)] attribute) calls a native command. That native command supports a switch that causes it to run in "dry run" mode, i.e., just outputting what would be copied or deleted without actually doing any copying/deleting.

Sample code of the script:

[CmdletBinding(SupportsShouldProcess)]
param ()
gsutil -m rsync -r -n something gs://something

Here the -n switch turns on the dry run mode.

How can I specify this switch based on whether WhatIf switch was passed to my script?

According to this official article, other cmdlets should inherit -WhatIf values automatically, but still it is suggested to explicitly pass the value to the cmdlets: -WhatIf:$WhatIfPreference. But in the call to a native command, I suppose, this approach doesn't work.

CodePudding user response:

As others have commented, you could check the $WhatIfPreference preference variable, which will be $true, when your script has been called with -WhatIf argument.

[CmdletBinding(SupportsShouldProcess)]
param ()

$dryMode = @( if( $WhatIfPreference ) { '-n' } )
gsutil -m rsync -r @dryMode something gs://something

To avoid duplicating the gsutil commandline, I've used argument splatting. To support splatting, $dryMode must be an array, which is enforced through the array subexpression operator @(…). If $WhatIfPreference is $true, the array will consist of a single string with the value -n. Otherwise the array will be empty and splatting an empty array doesn't insert anything into the commandline (not even a space).

Using PowerShell 7 , a cleaner syntax using ternary operator is possible:

$dryMode = $WhatIfPreference ? @('-n') : @()
gsutil -m rsync -r @dryMode something gs://something
  • Related