繁体   English   中英

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

[英]Powershell script to pass parameters using psexec in commandline

嗨,我正在尝试远程运行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脚本中。 任何帮助将不胜感激。

您需要将/ 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 *******"'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM