0

Trying to add printers to a computer by reading values for Printer information into VB.net and setting the variables. Once the variables are all set I trying to call the following

     Shell("powershell.exe c:\makeports\test.ps1 -name " & pPortName & " -hostaddress " & pPortName & " -portNumber " & pPortNum & " -IPAddress  " & pIP & " -ComputerName " & pCompName & " -Printername " & pPrintName & " -Driver " & pDriver)

after variables are set the line looks like this

    c:\makeports\test.ps1 -name 192.168.10.231 -hostaddress 192.168.10.231 -portNumber 6101 -IPAddress  192.168.10.231 -ComputerName PRO-TM2-PRO -Printername PR111 -Driver Zdesigner TLP 2844  -DeviceID ""

I realize I probably need double or single quotes around the values.

Getting page full of error message

Here is the shell script.

    Function CreatePrinterPort {
    Param ($PrinterIP, $PrinterPort, $PrinterPortName, $ComputerName)
    $wmi = [wmiclass]"\\$ComputerName\root\cimv2:win32_tcpipPrinterPort"
    $wmi.psbase.scope.options.enablePrivileges = $true
    $Port = $wmi.createInstance()
    $Port.name = $PrinterPortName
    $Port.hostAddress = $PrinterIP
    $Port.portNumber = $PrinterPort
    $Port.SNMPEnabled = $false
    $Port.Protocol = 1
    $Port.put()
    }

   Function CreatePrinter 
   {
   param ($PrinterCaption, $PrinterPortName, $DriverName, $ComputerName)
   $wmi = ([WMIClass]"\\$ComputerName\Root\cimv2:Win32_Printer")
   $Printer = $wmi.CreateInstance()
   $Printer.Caption = $PrinterCaption
   $Printer.DriverName = $DriverName
   $Printer.PortName = $PrinterPortName
   $Printer.DeviceID = $PrinterCaption
   $Printer.Put()
   }


   CreatePrinterPort  $printer.Portname  $printer.port $printer.IPAddress               $printer.Computer
   CreatePrinter $printer.Printername $printer.Portname  $printer.Driver  $printer.Computer


   
-ExecutionPolicy : The term '-ExecutionPolicy' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At C:\makeports\test.ps1:27 char:1
+ -ExecutionPolicy ByPass
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-ExecutionPolicy:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Cannot convert value "\\\root\cimv2:win32_tcpipPrinterPort" to type "System.Management.ManagementClass". Error: "Invalid parameter "
At C:\makeports\test.ps1:3 char:1
+ $wmi = [wmiclass]"\\$ComputerName\root\cimv2:win32_tcpipPrinterPort"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastToWMIClass

The property 'enablePrivileges' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:4 char:1
+ $wmi.psbase.scope.options.enablePrivileges = $true
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\makeports\test.ps1:5 char:1
+ $Port = $wmi.createInstance()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The property 'name' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:6 char:1
+ $Port.name = $PrinterPortName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The property 'hostAddress' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:7 char:1
+ $Port.hostAddress = $PrinterIP
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The property 'portNumber' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:8 char:1
+ $Port.portNumber = $PrinterPort
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The property 'SNMPEnabled' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:9 char:1
+ $Port.SNMPEnabled = $false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The property 'Protocol' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:10 char:1
+ $Port.Protocol = 1
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\makeports\test.ps1:11 char:1
+ $Port.put()
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Cannot convert value "\\\Root\cimv2:Win32_Printer" to type "System.Management.ManagementClass". Error: "Invalid parameter "
At C:\makeports\test.ps1:17 char:1
+ $wmi = ([WMIClass]"\\$ComputerName\Root\cimv2:Win32_Printer")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastToWMIClass

You cannot call a method on a null-valued expression.
At C:\makeports\test.ps1:18 char:1
+ $Printer = $wmi.CreateInstance()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The property 'Caption' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:19 char:1
+ $Printer.Caption = $PrinterCaption
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The property 'DriverName' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:20 char:1
+ $Printer.DriverName = $DriverName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The property 'PortName' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:21 char:1
+ $Printer.PortName = $PrinterPortName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The property 'DeviceID' cannot be found on this object. Verify that the property exists and can be set.
At C:\makeports\test.ps1:22 char:1
+ $Printer.DeviceID = $PrinterCaption
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\makeports\test.ps1:23 char:1
+ $Printer.Put()
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Anybody see what is COMPLETELY wrong

6
  • Looks like the code you posted isn't actually the exact contents of the file on disk - according to the first error, line 27 is just "-ExecutionPolicy ByPass" Commented Sep 4, 2020 at 15:29
  • Yes I removed that. The results though are without that line Commented Sep 4, 2020 at 15:31
  • Well, you never declare any parameters in the script, so I'm not sure where you're expecting $printer to come from? As a result $computerName (and all other parameters inside your functions) are $null, which is why the [wmiclass] string comes out as \\\root\cimv2:win32_tcpipPrinterPort Commented Sep 4, 2020 at 15:34
  • I am not used to using powershell. I assumed the line CreatePrinter $Printer.Printername was the declaring of the variable Commented Sep 4, 2020 at 15:56
  • That's not have it works, $Printer.Printername can't have a value before $Printer has been assigned any value. Commented Sep 4, 2020 at 15:59

1 Answer 1

1

If you want the script to accept named parameter argument binding (ie. script.ps1 -Param "value" resulting in $Param -eq "value" inside the script), you need to declare your parameters with a param() block:

param(
  [string]$Name,
  [string]$HostAddress, 
  [uint32]$PortNumber,
  [string]$IPAddress,
  [string]$ComputerName,
  [string]$Printername,
  [string]$Driver,
  [string]$DeviceID
)

function CreatePrinterPort { ... }
function CreatePrinter { ... }

# Don't forget to construct port name
$PortName = "${Printername}_tcpip_${PortNumber}"

# Now we just pass the appropriate parameter values that the script received to our functions 
CreatePrinterPort -PrinterIP $IPAddress -PrinterPort $PortNumber -PrinterPortName $PortName -ComputerName $ComputerName
CreatePrinter -PrinterCaption $PrinterName -PrinterPortName $PortName -DriverName $Driver -ComputerName $ComputerName 

... and you'll need an extra set of quotes around the driver name (because of the spaces) when invoking the script, ie. -Driver "Zdesigner TLP 2844"


Please see the about_Functions_Advanced and related help files for more details on parameters in advanced functions and scripts

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I will try and implement that. Appreciate the pointers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.