header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
How to write warning outside the cmdlet?
Last Post 25 Feb 2010 05:28 AM by George Howarth. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
dhristovUser is Offline
New Member
New Member
Posts:2
Avatar

--
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 HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    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..."
    dhristovUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    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 HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    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.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer