2017-08-18 49 views
1

私はコマンドラインからpowershellスクリプトを実行するためにさまざまな方法を試しましたが、すべてがエラーを返します。ここでコマンドラインからパスに空白を入れたpowershellスクリプトを実行するには?

がこのパスです:

C:\Users\test\Documents\test\line space\PS Script\test.ps1 

私はこれらを試してみた:

powershell -File '"C:\Users\test\Documents\test\line space\PS Script\test.ps1"' 

powershell "& ""C:\Users\test\Documents\test\line space\PS Script\test.ps1""" 

Powershell "& 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'" 

Powershell -File 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'" 

私はすべてのこれらのエラーを取得:

& : The term 'C:\Users\test\Documents\test\line space\PS Script\' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Processing -File ''C:\Users\test\Documents\test\line space\PS Script\'' failed: The given path's format is not support ed. Specify a valid path for the -File parameter.

を任意の助けいただければ幸いです!

+1

よう-EncodedCommandパラメータを使用できますか? – TheIncorrigible1

答えて

1

あなたの例では、何も理由なく二重引用符と二重引用符を混在しています。

IF EXIST "C:\Users\test\Documents\test\line space\PS Script\test.ps1" (
    powershell -ExecutionPolicy Unrestricted -File "C:\Users\test\Documents\test\line space\PS Script\test.ps1" 
) 
+0

これは私のためには機能しませんでしたが、スクリプトは起動しませんでした。私はこの部分だけを実行しようとしました:powershell -ExecutionPolicy Unrestricted -File 'C:¥Users¥test¥Documents¥test¥line space¥PS Script¥test.ps1'そしてこのエラーが出ました: 指定されたパスの形式はサポートされていません。 – Awsmike

+0

@Awsmike一重引用符を削除して何が起こるかを確認してください。 – TheIncorrigible1

+0

処理ファイル - C:\ Users \ test \ Documents \ test \ line space \ PS Script \ test.ps1ファイルに '。ps1' 。有効なWindows PowerShellスクリプトファイル名を指定して、もう一度やり直してください。 – Awsmike

-1

これを試してみてください:

PS C:\> & "C:\Users\test\Documents\test\line space\PS Script\test" 
+2

を反映するように私の答えを更新しました。彼は 'cmd'を使用しようとしているので、' .cmd'または '.bat'ファイルから呼び出すと仮定しています。 PS Invoke( '&')演算子は彼に良いことはしません。 – TheIncorrigible1

-1

シンプルについて何:

powershell -f "C:\Users\test\Documents\test\line space\PS Script\test.ps1" 

は、コマンドラインが最初
cmd.exeのパーサを通過しなければならないとcmdは唯一"を知っていることを忘れないでください適切に引用する。あなたはコマンドラインからpowershell.exe -Fileを実行したい場合は

5

-Fileパラメータ

は、あなたは常にdoubleqoutes(")にスペースでパスを設定する必要があります。一重引用符(')は、PowerShellでのみ認識されます。しかし、powershell.exeが呼び出されると(そしてファイルパラメータが処理されるため)、コマンドラインで"を使用する必要があります。 "'を使用する - この場合にする必要があります -

powershell.exe -File "C:\Users\test\Documents\Test Space\test.ps1" -ExecutionPolicy Bypass 

-Commandパラメータ

あなたが -Commandパラメータを使用する場合

、代わりの-File-Commandコンテンツは、したがって、あなたがすることができ、PowerShellのによって処理されます。

powershell.exe -Command "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass 

二重引用符は、コマンドラインで処理され、& 'C:\Users\test\Documents\Test Space\test.ps1'は、実際のP​​owerShellによって処理されるコマンドです。

解決策1は明らかに簡単です。

いずれも指定しない場合は、-Commandがデフォルトパラメータであることに注意してください。

powershell.exe "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass 

これも機能します。

-EncodedCommandパラメータ

あなたは、Base64として、あなたのコマンドをエンコードすることができます。これは多くの "引用"問題を解決しますが、唯一の可能な方法ではありますが、あなたの場合はそうではありません。

まず、

$Command = "& 'C:\Users\test\Documents\Test Space\test.ps1'" 
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Command)) 

エンコードされたコマンドを作成するために持っていて、CMDからそれを実行しているなぜ、あなたはこの

powershell.exe -EncodedCommand JgAgACcAQwA6AFwAVQBzAGUAcgBzAFwAdABlAHMAdABcAEQAbwBjAHUAbQBlAG4AdABzAFwAVABlAHMAdAAgAFMAcABhAGMAZQBcAHQAZQBzAHQALgBwAHMAMQAnAA== -ExecutionPolicy Bypass 
+0

うまくいった; PowerShell _Core_では、 '-File'がデフォルトになりました(この変更はUnixのシバン行をサポートするために必要でした)。 – mklement0

関連する問題