Author Posts

August 30, 2017 at 6:55 pm

I've got a function, as listed below.

Function Restart-MOLCimComputer
{
  [CmdletBinding(SupportsShouldProcess = $True,
                 ConfirmImpact = 'High')]
  Param
  (
    [Parameter(Mandatory = $True,
               ValueFromPipeline = $True,
               ValueFromPipelineByPropertyName = $True)]
    [string[]] $ComputerName
  )

  Process
  {
    ForEach($Computer in $ComputerName)
    {
      Invoke-CimMethod -ComputerName $Computer -ClassName Win32_OperatingSystem -MethodName Reboot
    } #End 'ForEach' block
  } #End 'Process' block
} #End 'Restart-MOLCimComputer' function

When I try to run it the first time, after I open a new PowerShell console, I get the following:

PS C:\> Restart-MOLCimComputer -ComputerName localhost -WhatIf
What if: Performing the operation "Set Alias" on target "Name: gcim Value: Get-CimInstance".
What if: Performing the operation "Set Alias" on target "Name: scim Value: Set-CimInstance".
What if: Performing the operation "Set Alias" on target "Name: ncim Value: New-CimInstance".
What if: Performing the operation "Set Alias" on target "Name: rcim Value: Remove-cimInstance".
What if: Performing the operation "Set Alias" on target "Name: icim Value: Invoke-CimMethod".
What if: Performing the operation "Set Alias" on target "Name: gcai Value: Get-CimAssociatedInstance".
What if: Performing the operation "Set Alias" on target "Name: rcie Value: Register-CimIndicationEvent".
What if: Performing the operation "Set Alias" on target "Name: ncms Value: New-CimSession".
What if: Performing the operation "Set Alias" on target "Name: rcms Value: Remove-cimSession".
What if: Performing the operation "Set Alias" on target "Name: gcms Value: Get-CimSession".
What if: Performing the operation "Set Alias" on target "Name: ncso Value: New-CimSessionOption".
What if: Performing the operation "Set Alias" on target "Name: gcls Value: Get-CimClass".
What if: Performing the operation "Invoke-CimMethod: Reboot" on target "Win32_OperatingSystem".

1. What is with all the "Set Alias" lines?
2. Why is "Win32_OperatingSystem" listed as the target on the bottom line instead of "localhost"? What am I doing wrong?

If I re-run the command, I just get the last line....again, with the wrong target listed.

PS C:\> Restart-MOLCimComputer -ComputerName localhost -WhatIf
What if: Performing the operation "Invoke-CimMethod: Reboot" on target "Win32_OperatingSystem".

August 30, 2017 at 6:59 pm

1.
-WhatIf is "inherited" by the commands within your function. So you're seeing some of the internal workings of Invoke-CimMethod. You could suppress this by adding -whatif:$false to the command. However, in doing so, you'd be negating your own -WhatIf, because you haven't otherwise handled -WhatIf in your code.

2.
The command determines that "target" message, and in this case it's listing the class it's operating against. That's just the way they coded it. It isn't "wrong," it just isn't what you were thinking of. You haven't implemented your own message, although you could do so in lieu of letting Invoke-CimMethod inherit the -whatif.

August 30, 2017 at 7:14 pm

Thanks, Don. I should have looked closer at the output of listing 16.1 in your "Learn Toolmaking In A Month Of Lunches" book because I now see that you're output shows the same thing. The output of the -WhatIf is not helpful, in this case, with it being coded that way because it's not clear what the affect of running the function really is. The inner workings stuff adds to the confusion. Maybe you can let Jason Snover know it needs fixing. 😉 I'll put it on the UserVoice page for PowerShell. If you know of a better place to leave the feedback, please let me know. Thanks.

August 30, 2017 at 7:17 pm

Jeffrey Snover ;).

And I'm not sure I'd regard it as unhelpful; were I running the command on its own, it's nice to be able to see what's happening internally.

That said, I usually suppress it in my own scripts.

If ($pscmdlet.ShouldProcess("target")) {
 Invoke-CimMethod -whatif:$false
}

This lets me specify my own "target" message, and define my own WhatIf output. It suppresses the native command output, which I've decided I don't need, buried as the command is within my own code. This is the "proper" was of procedurally handling -WhatIf, which I greatly prefer to letting it "fall through" to the commands within my code.

August 30, 2017 at 7:23 pm

Oops...sorry...I meant Jeffrey Snover. :).

I hear ya. The inner workings are fine. I just meant that might add confusion for someone seeing it. Mainly, I was focused on the target being something other than the computer name on that last line. So, should I ask how to write you're own "WhatIf" target message, rather than letting it "fall through" the commands, or is that covered later in your book? Right now, I'm just going through the book as is, which is how I ran into this issue.

August 30, 2017 at 7:24 pm

That ^^ is how you write your own WhatIf message. The Action is the name of your function and the target is whatever you put in place of "target" – like a computer name.

August 30, 2017 at 7:26 pm

Oops! I get it now. Thanks. 🙂

August 30, 2017 at 7:42 pm

BTW, thanks for your time Don. It's greatly appreciated. Loving your toolmaking book and have already pre-ordered your scripting book.

August 30, 2017 at 7:43 pm

You're very welcome – and thanks so much!

August 30, 2017 at 7:58 pm

When accessing CIM classes -WhatIf is dependent on the CIM provider supporting WhatIF to function correctly. Most CIM providers DON'T support WhatIf

August 30, 2017 at 10:58 pm

Good to know. Thanks, Richard. 🙂