[MSH] CMDlet in VB.NET

Discussion in 'Scripting' started by /\\/\\o\\/\\/, Sep 29, 2005.

  1. I'm trying to build a CMDlet in VB.NET.

    i have one in C#, that works (from the SDK example)

    now I'm trying to make a CMDlet in in VB.NET (I'm considering switching
    to C# but at the moment I'm more used to VBscript and VB.NET as to C#
    (esp. watching the Case))

    so I tryed to convert it into VB.NET, I think I'm almost there, but I
    can't get the ParameterAttribute right.

    i get error BC30451: Name 'Mandatory' is not declared.
    error BC30451: Position' is not declared.

    if I compile without I can not call it

    I posted the C$ , VB.NET translated version and errors below.

    someone knows where I go wrong ?

    gr /\/\o\/\/

    ---------------------------------
    using System.Management.Automation;
    namespace Mow

    {

    [Cmdlet("Get", "MethodInfo")]
    public class GetMethodInfo : Cmdlet
    {

    [Parameter(Position = 0,Mandatory = true)]
    public string WmiClass
    {
    get { return wmiClass; }
    set { wmiClass = value; }
    }

    private string wmiClass;


    // Overwrite the ProcessRecord virtual method
    // to add functionality to the Cmdlet.

    protected override void ProcessRecord()
    {
    WriteObject("Class "+ wmiClass);
    }
    }
    }
    -----------------------------------

    I translated it to VB.NET as follows :

    ---------------------------------------
    imports system.management
    imports System.Management.Automation
    namespace Mow

    <Cmdlet("Get", "MethodInfo")> _
    public class GetMethodInfo
    inherits Cmdlet

    Dim m_wmiClass As String

    <Parameter(Position = 0,Mandatory = true)> _
    Public Property wmiClass() as string
    Get
    wmiClass = m_wmiClass
    End Get
    Set(ByVal Value as string)
    m_wmiClass = Value
    End Set
    End Property

    Protected overrides sub ProcessRecord()
    Dim mp As New ManagementPath
    mp.Server = "."
    mp.ClassName = m_wmiClass
    Dim mc As New ManagementClass(mp)
    msgbox(MC.Methods("create").Qualifiers("Description").Value)
    End sub

    end class

    end Namespace
    --------------------------------

    I get the following errors while compiling :

    G:\Monad\CmdLets\let.vb(11) : error BC30451: Name 'Position' is not
    declared.

    <Parameter(Position = 0,Mandatory = true)> _
    ~~~~~~~~
    G:\Monad\CmdLets\let.vb(11) : error BC30451: Name 'Mandatory' is not
    declared.

    <Parameter(Position = 0,Mandatory = true)> _

    if I comment the line it will Compile but I get the following errors in
    MSH :

    MSHl> Get-MethodInfo
    Get-MethodInfo : Invalid parameter
    At line:1 char:15
    + Get-MethodInfo <<<<

    MSH> Get-MethodInfo -wmiclass win32_share
    Get-MethodInfo : A parameter cannot be found that matches parameter
    'wmiclass'.
    At line:1 char:25
    + Get-MethodInfo -wmiclass <<<< win32_share

    MSH> Get-MethodInfo win32_share
    Get-MethodInfo : A parameter cannot be found that matches parameter
    'win32_shar
    e'.
    At line:1 char:15
    + Get-MethodInfo <<<< win32_share
    MSH>
     
    /\\/\\o\\/\\/, Sep 29, 2005
    #1
    1. Advertisements

  2. Change the attribute to:

    <Parameter(Position:=0, Mandatory:=True)>
     
    Jouko Kynsijärvi, Sep 29, 2005
    #2
    1. Advertisements

  3. Jouko,

    mowMSH> get-methodinfo

    Cmdlet get-methodinfo at command pipeline position 1
    Supply values for the following parameters:
    wmiClass: win32_share

    thx again

    /\/\o\/\/

    so now i made one ;-)

    :

    Jouko Kynsijärvi wrote
    Not to be to picky, but you have just made a normal .NET class ;-)
     
    /\\/\\o\\/\\/, Sep 29, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.