Understanding -whatif behavior – I’m missing something here…

So, I’m trying to implement -whatif behavior on my functions moving forward, but I’m having trouble on a couple of fronts.

Here’s the function I’ve been testing: http://pastebin.com/sFD0UER2

I copied this function from here (and made some minor formatting changes): http://windowsitpro.com/blog/advanced-functions-part-2-shouldprocess-your-script-cmdlets

First Problem:

The text returned in STDOUT is different using -whatif vs actually executing the function.

Using -whaif:

PS C:Userszeroadmin.ZERO> 'sqlserver.zero.lab' | Win32Restart-Computer -action LogOff -whatif VERBOSE: Action set to LogOff VERBOSE: Attempting to connect to sqlserver.zero.lab What if: Performing the operation "Win32Restart-Computer" on target "sqlserver.zero.lab". 

Actually executing function:

PS C:Userszeroadmin.ZERO> 'sqlserver.zero.lab' | Win32Restart-Computer -action LogOff VERBOSE: Action set to LogOff VERBOSE: Attempting to connect to sqlserver.zero.lab Confirm Are you sure you want to perform this action? Performing the operation "Win32Restart-Computer" on target "sqlserver.zero.lab". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 0 PSComputerName : 

I’m not sure why -whatif doesn’t return “__GENUS…PSComputerName”. My guess is that it has something to do with the inner workings of invoke-wmimethod, but does that mean that I’m just out of luck getting -whatif to display the exact same STDOUT as actually executing the function?

Second Problem:

Let’s say I wanted to PURPOSELY make output to STDOUT different when using -whatif versus actually executing the function? For example, I’d like to add something like:

if ($whatif) { Write-Verbose "Dry run logging off of $computername" } if (! $whatif) { Write-Verbose "Actually logging off of $computername" } 

…but I’m not sure how the function recognizes the presence of -whatif in the command line.

Any advice would be greatly appreciated.

submitted by /u/fourierswager
[link] [comments]

Leave a Reply