Chapter 16. Making tools that make changes
So far, we’ve been focused on tools that get information, Get-MOLSystemInfo being our running example. But you’re obviously going to create tools that make changes, too. When you do so, there are a couple of extra steps that you should take in order to remain consistent with the rest of PowerShell. You’ve already seen a hint of this: the support for –Confirm and –WhatIf that our Invoke-MOLDatabaseQuery function included. In this chapter, we’ll walk you through those details.
Note
We’re going to be using some of PowerShell’s CIM commands in this chapter. Those work against Windows Management Instrumentation (WMI) but only work if PowerShell Remoting has been enabled on the computer. If you’re having problems using them, you can change to the older WMI command. We’ll provide tips along the way for doing so.
Look at the help for any PowerShell command that makes changes, and you’re likely to see both –Confirm and –WhatIf included in the command’s parameters. These parameters should be supported for any command that changes the system state in any way—even something as simple as changing a file on disk.
Fortunately, there’s no need to hand-code these parameters into your tools. If you’re writing an advanced function, which is more or less all we’ve been writing in this book, then just a little extra work can implement these two key parameters for you.