Powershell脚本在命令行中使用psexec传递参数

时间:2018-07-18 09:26:49

标签: powershell parameters workflow

嗨,我正在尝试远程运行Powershell脚本来为2台服务器创建Web应用程序池:以下是我在2台服务器上运行的Website_pararrel.ps1中的代码:

 workflow pbatch{

 param ([string]$iisAppName,[string]$directoryPath,[string]$iisAppPoolName, 
 [string]$un,[string]$pw,[string]$path,[string]$servers)

 foreach -parallel ( $server in $servers)
 {             
     (InlineScript{

     Import-Module WebAdministration

     Set-Location "IIS:\AppPools"

    if (Test-Path $Using:iisAppPoolName -pathType container)
     {
        Remove-Item $Using:iisAppPoolName -recurse   
     }

        New-Item $Using:iisAppPoolName -Force           
        Set-ItemProperty -Path IIS:\AppPools\WhereIsTest -Name managedRuntimeVersion -Value v4.0
        Set-ItemProperty -Path IIS:\AppPools\WhereIsTest -Name processmodel.identityType -Value 3
        Set-ItemProperty -Path IIS:\AppPools\WhereIsTest -Name ProcessModel.userName -Value $Using:un1
        Set-ItemProperty -Path IIS:\AppPools\WhereIsTest -Name processmodel.password -Value $Using:pw1





        })


   }

}

pbatch -servers $servers -iisAppPoolName $iisAppPoolName -un $un -pw $pw 

我从第一台服务器使用此命令运行:

Psexec \\CPDEVWEB03.blfdev.lab /s cmd /c %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -file D:\Website_Parallel.ps1 -servers "xyz" -iisAppPoolName "Test" -un "***\****" -pw "*******"

但我收到此错误:

 Test-Path : Cannot bind argument to parameter 'Path' because it is an empty
 string.
 At pbatch:14 char:14
 +
 + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingVa
 lidationException
 + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl
 lowed,Microsoft.PowerShell.Commands.TestPathCommand
 + PSComputerName        : [localhost]

  New-Item : Cannot bind argument to parameter 'Path' because it is an empty
 string.
  t pbatch:14 char:14
 +
 + CategoryInfo          : InvalidData: (:) [New-Item], ParameterBindingVal
 idationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl
lowed,Microsoft.PowerShell.Commands.NewItemCommand
+ PSComputerName        : [localhost]

  Set-ItemProperty : Cannot process argument because the value of argument 
  "obj"
  is null. Change the value of argument "obj" to a non-null value.
  At pbatch:14 char:14
  +
  + CategoryInfo          : InvalidArgument: (:) [Set-ItemProperty], PSArgum
  entNullException
  + FullyQualifiedErrorId : InvalidArgument,Microsoft.PowerShell.Commands.Se
  tItemPropertyCommand
+ PSComputerName        : [localhost]

 Set-ItemProperty : Cannot process argument because the value of argument 
 "obj"
  is null. Change the value of argument "obj" to a non-null value.
  cmd exited on CPDEVWEB03.blfdev.lab with error code 0.

以某种方式,我的命令行脚本中的变量值没有传递到我的Powershell脚本中。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要将/ c的参数放在单引号(')中,以便将其视为一个参数。

以下工作吗?

Psexec \\CPDEVWEB03.blfdev.lab /s cmd /c '%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -file "D:\Website_Parallel.ps1 -servers xyz -iisAppPoolName Test -un ***\**** -pw *******"'