我有以下cmd文件: -
PowerShell.exe -noexit E:\wwwroot\domains\processes\AddDirectory.ps1 -Param testdomain.co.uk
进入: -
$Session = New-PSSession -ComputerName 192.168.0.25
$script = {
Param($Param1)
set-executionpolicy unrestricted -force
# Set Variables
$domain = $Param1
$sitepath = "e:\domains\" + $domain
# Check for physical path
if (-not (Test-Path -path $sitePath))
{
New-Item -Path $sitepath -type directory
New-Item -Path $sitepath\wwwroot -type directory
}
set-executionpolicy restricted -force
}
Invoke-Command -Session $Session -ScriptBlock $script
但它只是运行但什么也没做。
如果我将$ domain变量声明为$ domain ='testdomain.co.uk',它可以工作,但它不想从cmd文件中传递var。我究竟做错了什么?我试图把它作为-ArgumentsList - $ Param1放在Invoke-Command中,但这也不起作用.....
任何有意义的想法
由于 保罗
更新 - 我已根据以下内容更新了我的代码,但遇到同样的问题: -
param($domainName)
$script = {
Param($Param1)
set-executionpolicy unrestricted -force
# Set Variables
$domain = $Param1
$sitepath = "e:\domains\" + $domain
# Check for physical path
if (-not (Test-Path -path $sitePath))
{
New-Item -Path $sitepath -type directory
New-Item -Path $sitepath\wwwroot -type directory
New-Item -Path $sitepath\db -type directory
New-Item -Path $sitepath\stats -type directory
}
set-executionpolicy restricted -force
}
$Session = New-PSSession -ComputerName 192.168.0.25
Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $domainName
答案 0 :(得分:2)
您需要在脚本中使用param块,传递给该文件的参数将分配给$ domainName,您将使用它将值传递给scriptblock:
PowerShell.exe -noexit E:\wwwroot\domains\processes\AddDirectory.ps1 testdomain.co.uk
# script file
param($domainName)
$script = {
Param($Param1)
...
$domain = $Param1
...
}
$Session = New-PSSession -ComputerName 192.168.0.25
Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $domainName