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”.
What is with all the “Set Alias” lines?
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”.
-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.
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.
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.
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.
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.
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.