簡體   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