Register
: :
Login
Home
News
Forums
Scripts
User Groups
Resources
About
Forums
Search
Members
Unanswered
Active Topics
Forums
>
PowerShell Development
>
Cmdlet Development
How to write warning outside the cmdlet?
Last Post 25 Feb 2010 05:28 AM by
George Howarth
. 3 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
dhristov
New Member
Posts:2
24 Feb 2010 07:58 AM
Hello,
I'm developing PowerShell cmdlets and I'd like to do some more advanced functionality. I need to write a warning when the user access a specific property of one of my output objects (e.g. to tell him that this property is deprecated and he should use an alternative one). Here's the complete scenario:
I have a cmdlet Get-Foo with parameters X, Y and Z. This cmdlet returns object of type Foo - which has properties A, B and C. However the parameter X and the property A are deprecated. I want to warn the user not to use them (not only in the help - I want a warning to be printed whenever he uses some deprecated parameter/property). Is there any (easy?) way to do that?
I know that there's WriteWarning(...) method in PSCmdlet class and I can use it to write warnings when a deprecated cmdlet parameter is used. However this won't work when the user tries to access a deprecated property of an output object. There is a WriteWarningLine(...) method in PSHost.UI but using it in the case of object's property seems a bit of a dirty hack (since I have to store the PSHost in a static class during the first use of the cmdlet and then access it from the output object's property). I'd appreciate any ideas how to achieve the desired functionality.
P.S. I'm using PowerShell 1.0
George Howarth
Basic Member
Posts:360
24 Feb 2010 11:45 AM
Warn of parameter usage:
function Get-Foo
{
param ($X, $Y, $Z)
trap [System.ArgumentException]
{
Write-Host $_.Exception.Message -BackgroundColor Black -ForegroundColor Yellow
Write-Host "At:" $MyInvocation.ScriptName "Line:" $MyInvocation.ScriptLineNumber -BackgroundColor Black -ForegroundColor Yellow
continue
}
if ($X -ne $null)
{
throw (New-Object System.ArgumentException -ArgumentList "WARNING: Obsolete parameter usage.", "X")
}
}
Get-Foo -X "value1" -Y "value2" -Z "value3" # Use parameter X
Write-Host "Continuing with script..."
Warn of property access:
function Main
{
$foo = New-Object System.Management.Automation.PSObject
$foo | Add-Member -MemberType ScriptProperty -Name A -Value {
trap [System.MemberAccessException]
{
Write-Host $_.Exception.Message -BackgroundColor Black -ForegroundColor Yellow
Write-Host "At:" $MyInvocation.ScriptName "Line:" $MyInvocation.ScriptLineNumber -BackgroundColor Black -ForegroundColor Yellow
continue
}
throw (New-Object System.MemberAccessException -ArgumentList "WARNING: Accessed obsolete property A in object Foo")
return "Value of Property A still gets returned."
}
Write-Host $foo.A -ForegroundColor Green # Access property A
}
Main
Write-Host "Continuing with script..."
dhristov
New Member
Posts:2
25 Feb 2010 04:50 AM
Thanks for the response.
I haven't thought of using script properties. I'll give it a try.
George Howarth
Basic Member
Posts:360
25 Feb 2010 05:28 AM
This is, of course, assuming that you're using PowerShell. If you're using C#/VB.NET, you can just throw exceptions in the corresponding places and catch them to allow the script to continue.
You are not authorized to post a reply.
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Forums
>
PowerShell Development
>
Cmdlet Development
Active Forums 4.3
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2