80 lines
3.3 KiB
HTML
80 lines
3.3 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml"><head>
|
|
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<link rel="stylesheet" href="main.css" type="text/css" />
|
|
<link rel="stylesheet" href="blog.css" type="text/css" />
|
|
<link rel="alternate" type="application/rss+xml" title="Subscribe to this page..." href="feed.rss" />
|
|
<title>The correct way to implement -Whatif and -Confirm in a Powershell Cmdlet</title>
|
|
</head><body>
|
|
<div id="divbodyholder">
|
|
<div class="headerholder"><div class="header">
|
|
<div id="title">
|
|
<h1 class="nomargin"><a class="ablack" href="http://zigford.org/index.html">zigford.org</a></h1>
|
|
<div id="description"><a href="about.html">About</a><a href="links.html"> | Links</a><a href="scripts.html"> | Scripts</a><br>Sharing linux/windows scripts and tips</br></div>
|
|
</div></div></div>
|
|
<div id="divbody"><div class="content">
|
|
<!-- entry begin -->
|
|
<h3><a class="ablack" href="the-correct-way-to-implement--whatif-and--confirm-in-a-powershell-cmdlet.html">
|
|
The correct way to implement -Whatif and -Confirm in a Powershell Cmdlet
|
|
</a></h3>
|
|
<!-- bashblog_timestamp: #201901082208.39# -->
|
|
<div class="subtitle">January 08, 2019 —
|
|
Jesse Harris
|
|
</div>
|
|
<!-- text begin -->
|
|
<p>When I first learned about <code>[CmdLetBinding()]</code> to add automatic support for
|
|
<code>Write-Verbose</code> I didn't understand why -WhatIf and -Confirm were not passed
|
|
to Cmdlets used in my functions.</p>
|
|
<hr />
|
|
<p>I wrote functions like this to fake it:</p>
|
|
<pre><code> function Set-BadIdea {
|
|
[CmdLetBinding()]
|
|
Param(
|
|
[string]$File,
|
|
[switch]$Whatif,
|
|
[switch]$Confirm
|
|
)
|
|
|
|
Remove-Item $File -WhatIf:$WhatIf -Confirm:$Confirm
|
|
|
|
}
|
|
</code></pre>
|
|
<p>While this kinda works, it doesn't allow any control over where Whatif's and
|
|
confirms are used. After reading
|
|
<a href="https://vexx32.github.io/2018/11/22/Implementing-ShouldProcess/">Implementing ShouldProccess for your functions</a> post by Joel Francsis,
|
|
I'm now able to do things like this:</p>
|
|
<pre><code> function Set-GoodIdead {
|
|
[CmdLetBinding(SupportsShouldProcess)]
|
|
Param([string]$File)
|
|
|
|
If ($PSCmdlet.ShouldProcess("$File", "Get rid of junk")) {
|
|
Remove-Item $File
|
|
}
|
|
|
|
}
|
|
</code></pre>
|
|
<p>As I hope you can see, you could implement the <code>$PSCmdlet.ShouldProcess</code>
|
|
on any code, not just other functions that support -WhatIf or -Confirm or
|
|
-Force</p>
|
|
<p>Doing this will lead to more consistent behavior of functions and allow
|
|
behaviour to be more intuitive.</p>
|
|
<p>For a much more in depth explaination, go and check <a href="https://vexx32.github.io/2018/11/22/Implementing-ShouldProcess/">Joel's post</a></p>
|
|
<p>Tags: <a href='tag_powershell.html'>powershell</a>, <a href='tag_coding.html'>coding</a></p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- text end -->
|
|
<!-- entry end -->
|
|
</div>
|
|
<div id="footer">© <a href="http://twitter.com/zigford_org">Jesse Harris</a> — <a href="mailto:jesse@zigford.org">jesse@zigford.org</a><br/>
|
|
Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>
|
|
</div></div>
|
|
</body></html>
|