.Bat ファイルを使用して powershell 関数を呼び出す方法を知りたいです。
私はこの単純な機能を持っています:
(スクリプト.ps1)
function testfunction
{
Write-Log "Calling from Bat file"
}
.Bat ファイル内で関数 testfunction を呼び出したいと思います。
powershell .\Script.ps1 ...
.Bat ファイルを使用して powershell 関数を呼び出す方法を知りたいです。
私はこの単純な機能を持っています:
(スクリプト.ps1)
function testfunction
{
Write-Log "Calling from Bat file"
}
.Bat ファイル内で関数 testfunction を呼び出したいと思います。
powershell .\Script.ps1 ...
スイッチで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
また、いくつかのサンプルを見てください。
私は通常、次のようにします。
powershell {Set-ExecutionPolicy unrestricted}
powershell.exe -executionpolicy ByPass ./script/powerhsell-script.ps1 %par1% %par2%
powershell {Set-ExecutionPolicy Restricted}
あなたがそれを必要とすることを願っています:)
私はそれを使ってやった
powershell.exe -ExecutionPolicy Bypass -file ".\Write_date.ps1"
他の人が書いたものと似ているように見えますが、スクリプトを呼び出す前にスクリプトのドット ソースを必要とするという vonPryz の意味がわかりません。上記は、実行ポリシーが制限付きに設定された Win7 で機能しました。