2

私は次の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ファイルから変数を渡したくありません。私は何が間違っているのですか?Invoke-Commandに-ArgumentsList-$Param1として配置しようとしましたが、それも機能しません。

どんなアイデアも大歓迎

ありがとうポール

更新-以下のようにコードを更新しましたが、同じ問題が発生します:-

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
4

1 に答える 1

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
于 2012-10-23T19:11:43.410 に答える