問題:フォルダー名に角かっこを含む powershell.exe へのローカル パスを渡すと、powershell が渡されたスクリプト ファイルを見つけることができません。
オプション 1: powershell -File .\MyScript.ps1を使用する
オプション 2: powershell %CD%\MyScript.ps1 を使用する
powershell がパスを見つけられない理由ははっきりしていませんが、 -File コマンドライン引数で修正されているようです。'.' の代わりに %CD% バッチ ファイル プロパティを使用する場合と同様です。
単一行を含むファイル「HelloWorld.ps1」を作成すると、次のようになります。
Write-Output "Hello World"
フォルダー内
c:\work\joel\scream
次のコマンドを実行します。
C:\work\joel\scream>powershell .\HelloWorld.ps1
次に、期待される出力 (Hello World) を取得します。
フォルダの名前を [scream] に変更すると失敗します。
cd ..
ren scream [scream]
角括弧は範囲演算子です。
C:\work\joel\[scream]>powershell .\HelloWorld.ps1
今は得られます:
.\helloworld.ps1 : '.\helloworld.ps1' という用語は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認識されません。名前のスペルを確認するか、パスが含まれている場合は、パスが正しいことを確認してから再試行してください。
ただし、実行中:
C:\work\joel\[scream]>powershell -File .\HelloWorld.ps1
期待される出力 (Hello World) を提供します。
powershell /?
<snip/>
-File
Runs the specified script in the local scope ("dot-sourced"), so that the
functions and variables that the script creates are available in the
current session. Enter the script file path and any parameters.
File must be the last parameter in the command, because all characters
typed after the File parameter name are interpreted
as the script file path followed by the script parameters.
PowerShell またはコマンド プロンプト/バッチ スクリプトがパスを誤って解釈しているかどうかは明確ではありませんが、次のように言えます。
C:\work\joel\[scream]>powershell .\HelloWorld.ps1
動作しません:
C:\work\joel\[scream]>powershell %CD%\helloworld.ps1
動作します。そのため、「.」の展開に関連しているように見えます。現在のパスに。