C# Powershell cmdlets and type definitions

C# Powershell cmdlets and type definitions

Post by Normy » Thu, 26 Jul 2007 04:32:54



Hello all, I am trying my hand at C# and at the same time build a
cmdlet for Powershell. I have read the sparse documentation on MSDN
regarding building your cmdlets and it leaves a lot to be desired (but
then again PS is new so I don't expect the docs to be all that
anyways).

What I am trying to do is build a cmdlet called Get-PrintJobs that
will take the arguments "servername" and "printserver" which will then
return all the printjobs in the queue for the specified printer. I
found in the .NET FW 3.0 there is System.Printing.PrintQueue ( and I
have tried PrintQueCollection)  that has a method
"GetPrintJobInfoCollection" which if I'm correct returns all the info
on the jobs in a queue. I kept getting variations on the error below.

I get an error when I build it I get this warning

'System.Printing.PrintQueueCollection' is a 'type', which is not valid
in the given context

I think it something to do with type definitions but I'm unclear on
how to properly impliment it. Here is my code below. Any Suggestions
would be greatly appreciated from this newbie.

Norm

PS:: I used the PSCmdlet templet provided by David Aiken over at
Channel9

using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.Collections;

namespace PSGetPrintjobs
{
    [Cmdlet(VerbsCommon.Get, "PrintJobs", SupportsShouldProcess =
true)]
    public class GetPrintJobs : PSCmdlet
    {

        #region Parameters

        [Parameter(Position = 0,
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "Enter the Servername")]
        [ValidateNotNullOrEmpty]
        public string servername
        {
            get { return servername;}
            set {servername = value;}
        }

        [Parameter(Position = 1,
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "Enter the Printer name")]
        [ValidateNotNullOrEmpty]
        public string printername
        {
            get { return printername; }
            set { printername = value; }
        }

        #endregion

        protected override void ProcessRecord()
        {
            try
            {

WriteObject(System.Printing.PrintQueue.GetPrintJobInfoCollection());

            }
            catch (Exception)
            {
            }
        }
    }

Quote:}

 
 
 

1. Go To Definition confussed when a property name and its return type have the same name

Hi,

I have noticed that when a property name and its return type have the same
name, the "Go To Definition" feature does not work properly.

For example, create a new VB.Net class with the following code:

Public Class Class1

 Public ReadOnly Property AccessibleEvents2() As AccessibleEvents
  Get

  End Get
 End Property

 Public ReadOnly Property AccessibleRole() As AccessibleRole
  Get

  End Get
 End Property

End Class

Put the cursor on the "AccessibleEvents" word of the return type of the
first property and right-click the "Go To Definition" context menu. The
feature shows the enum type in the Object Browser, which is correct.

Now, do the same with the "AccessibleRole" return type of the second
property. The "Go To Definition" feature selects the name of the property
(which is not a type) instead of opening the Object Browser.

Comments?

Thanks in advance,

Carlos Quintero

2. anyone used a production 50o or 505?

3. XSD type definition to accept integer up to 4 digits or empty tag

4. Programatically Install & Manage FP SE 2000 on IIS5

5. Deployment question for enterprise services: no class type definition on client machine

6. Mac External Hard Drives

7. "Cannot find definition for referenced type System.Xml.XmlWriter"

8. Error: No such interface supported

9. Don't type class definitions in Visual Studio 6.0

10. Importing type definition from asp.xsd

11. Possible to share const definition file with VS C#?

12. Event types in C# vs Event types in managed C++.

13. Converting Microsoft Basic Float type to IEEE float type in C#