Using .Net Objects within a Powershell (V5) Class -


this major edit done clarity purposes. appears need work on forming thoughts. below exact code having trouble with. brief description: trying setup powershell class hold objects of different types easy access. i've done numerous times in c#, thought straight forward. types wanted [system.printing] , wmi-objects.

originally had tried write class directly powershell profile easy usage, profile fails load when have class code in it. saying cant find type name "system.printing.printserver", or other explicitly listed types. after failed moved it's own specific module , set profile import module on open. however, when stored in own module, if explicitly list .net type of properties, entire module fails load. regardless of whether have added or imported type / dll. specific problem area this: [string]$name [system.printing.printserver]$server [system.printing.printqueue]$queue [system.printing.printticket]$ticket [system.management.managementobject]$unit [bool]$isdefault

when have set this, "kind of" works, properties have _object type, not helpful. [string]$name $server $queue $ticket $unit $isdefault

add-type -assemblyname system.printing add-type -assemblyname reachframework class printerobject {     [string]$name     [system.printing.printserver]$server     [system.printing.printqueue]$queue     [system.printing.printticket]$ticket     [system.management.managementobject]$unit     [bool]$isdefault     printerobject([string]$name)     {         #add-type -assemblyname system.printing         #add-type -assemblyname reachframework         $this.server = new-object system.printing.printserver -argumentlist [system.printing.printsystemdesiredaccess]::administrateserver         $this.queue =  new-object system.printing.printqueue (($this.server), ($this.server.getprintqueues() |          where-object {$_.name -match $name} | select-object -expandproperty name))          $this.ticket = $this.queue.userprintticket         $this.unit = get-wmiobject -query "select * win32_printer name `"%$name%`""     }      printerobject([string]$name, [bool]$isnetwork)     {         #add-type -assemblyname system.printing         #add-type -assemblyname reachframework         if($isnetwork -eq $true) {         $this.server = new-object system.printing.printserver ("\\server")         $this.queue =  new-object system.printing.printqueue (($this.server), ($this.server.getprintqueues() |          where-object {$_.name -match $name} | select-object -expandproperty name))          $this.ticket = $this.queue.userprintticket         $this.unit = get-wmiobject -query "select * win32_printer name `"%$name%`""         }         else {         $this.server = new-object system.printing.printserver -argumentlist [system.printing.printsystemdesiredaccess]::administrateserver         $this.queue =  new-object system.printing.printqueue (($this.server), ($this.server.getprintqueues() |          where-object {$_.name -match $name} | select-object -expandproperty name))          $this.ticket = $this.queue.userprintticket         $this.unit = get-wmiobject -query "select * win32_printer name `"%$name%`"" }     }     [void]setprintticket([int]$copies, [string]$collation, [string]$duplex)     {         $this.ticket.copycount = $copies         $this.ticket.collation = $collation         $this.ticket.duplexing = $duplex         $this.queue.commit()     }      [object]getjobs($option)     {             if($option -eq 1) { return $this.queue.getprintjobinfocollection() | sort-object -property jobidentifier | select-object -first 1}             else { return $this.queue.getprintjobinfocollection() }     }     static [object]showallprinters()     {         return get-wmiobject -class win32_printer | select-object -property name, systemname     }  } 

every powershell script parsed before first statement in script executed. unresolvable type name token inside class definition considered parse error. solve problem, have load types before class definition parsed, class definition has in separate file. example:

main.ps1:

add-type -assemblyname system.printing add-type -assemblyname reachframework  . $psscriptroot\class.ps1 

class.ps1:

using namespace system.management using namespace system.printing  class printerobject {     [string]$name     [printserver]$server     [printqueue]$queue     [printticket]$ticket     [managementobject]$unit     [bool]$isdefault } 

the other possibility embed class.ps1 string , use invoke-expression execute it. delay parsing of class definition time types available.

add-type -assemblyname system.printing add-type -assemblyname reachframework  invoke-expression @‘     using namespace system.management     using namespace system.printing      class printerobject     {         [string]$name         [printserver]$server         [printqueue]$queue         [printticket]$ticket         [managementobject]$unit         [bool]$isdefault     } ’@ 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -