如何从VB.net 2017传递变量调用Powershell脚本?

时间:2020-09-04 14:55:38

标签: vb.net powershell

通过将打印机信息的值读取到VB.net并设置变量来尝试将打印机添加到计算机。变量全部设置好后,我尝试调用以下

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

设置变量后,该行如下所示

    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 ""

我意识到我可能需要在值周围使用双引号或单引号。

获取充满错误消息的页面

这是外壳程序脚本。

    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

任何人看到完全错误的地方

1 个答案:

答案 0 :(得分:1)

如果您希望脚本接受命名参数实参绑定(即getsource <- function(fn) { fn <- rlang::ensym(fn) utils:::.getHelpFile(help(deparse(fn))) } getsource(help) #> \title{Documentation}\name{help}\alias{help}\keyword{documentation}\description{ #> \code{help} is the primary interface to the help systems. #> }\usage{ #> help(topic, package = NULL, lib.loc = NULL, #> verbose = getOption("verbose"), #> try.all.packages = getOption("help.try.all.packages"), #> help_type = getOption("help_type")) #> }\arguments{ #> \item{topic}{usually, a \link{name} or character string specifying the #> topic for which help is sought. A character string (enclosed in #> explicit single or double quotes) is always taken as naming a topic. #> ... 在脚本内部产生script.ps1 -Param "value"),则需要使用$Param -eq "value"块声明参数:< / p>

param()

...,并且在调用脚本时(例如,由于空格),您将需要在驱动程序名称周围加上引号。 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


有关高级功能和脚本中的参数的更多详细信息,请参见about_Functions_Advanced and related help files