PowerShell V3 – ObsoleteAttribute

PowerShell V3 now supports the ObsoleteAttribute for compiled cmdlets but unfortunately not advanced functions.  This is handy to let your users know that a binary cmdlet will be going away in a future release of your binary module.

As we work on PSCX 3.0 there are a few binary cmdlets that we will mark with this attribute to let you know to switch over to PowerShell’s built-in equivalent before we eliminate the cmdlet completely in the next release.  Here’s a snippet that shows how to apply the ObsoleteAttribute in your source code:

1 [OutputType(typeof(MailMessage))] 2 [Cmdlet(VerbsCommunications.Send, PscxNouns.SmtpMail, 3 DefaultParameterSetName = "Authenticated", 4 SupportsShouldProcess = true)] 5 [Obsolete(@"The PSCX\SendSmtpMail cmdlet is obsolete " + 6 "and will removed in the next version of " + 7 "PSCX. Use the built-in Send-MailMessage.")] 8 public class SendSmtpMailCommand : PscxCmdlet 9 {

The resulting of executing this cmdlet with PSCX v3 loaded is:

C:\PS> Send-SmtpMail
WARNING: The PSCX\SendSmtpMail cmdlet is obsolete and will removed in the next version of 
PSCX. Use the built-in Send-MailMessage.

There is an ObsoleteAttribute constructor overload that takes a boolean that converts the warning to an error.  I’m not sure how useful that is but PowerShell does honor that setting and will generate a terminating error in this case:

C:\PS> Send-SmtpMail
The PSCX\SendSmtpMail cmdlet is obsolete and will removed in the next version of PSCX. Use 
the built-in Send-MailMessage. At line:1 char:1 + Send-SmtpMail + ~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Send-SmtpMail:String) [], RuntimeException + FullyQualifiedErrorId : UseOfDeprecatedCmdlet

It’s nice to see PowerShell honoring more of the .NET attributes – where it makes sense that is.

Advertisements
This entry was posted in PowerShell 3.0. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s