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
$printerto 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$Printer.Printernamecan't have a value before$Printerhas been assigned any value.