4

.Bat ファイルを使用して powershell 関数を呼び出す方法を知りたいです。

私はこの単純な機能を持っています:

(スクリプト.ps1)

function testfunction
{
Write-Log "Calling from Bat file"
}

.Bat ファイル内で関数 testfunction を呼び出したいと思います。

powershell .\Script.ps1 ...

4

5 に答える 5

3

スイッチでPowershellを起動し-command、スクリプトを含めて関数を呼び出します。関数を呼び出す前に、スクリプトをドットソースする必要があります。

:: Create a test script file
C:\temp>echo function test { write-host "Called from .bat" } > c:\temp\s.ps1
C:\temp>powershell -command ". c:\temp\s.ps1; test;"
:: Output
Called from .bat

また、いくつかのサンプルを見てください。

于 2013-06-11T05:38:32.650 に答える
0

私は通常、次のようにします。

powershell {Set-ExecutionPolicy unrestricted}
powershell.exe -executionpolicy ByPass ./script/powerhsell-script.ps1 %par1% %par2%
powershell {Set-ExecutionPolicy Restricted}

あなたがそれを必要とすることを願っています:)

于 2013-06-12T14:12:00.460 に答える
0

私はそれを使ってやった

powershell.exe -ExecutionPolicy Bypass -file ".\Write_date.ps1"

他の人が書いたものと似ているように見えますが、スクリプトを呼び出す前にスクリプトのドット ソースを必要とするという vonPryz の意味がわかりません。上記は、実行ポリシーが制限付きに設定された Win7 で機能しました。

于 2013-06-14T15:38:07.533 に答える