Mic*_*ael 3 powershell command-line
我试图使用PowerShell调用位于包含空格的位置/路径的EXE.当我从命令行调用脚本时,EXE的完整路径不会传递给脚本.关于为什么会发生这种情况的任何想法?
PowerShell脚本内容(Untitled1.ps1)
以下是从命令行调用的整个脚本:
param(
[string] $ParamExePath
)
function Run-CallThisExe {
param(
[string] $ThisExePath
)
Write-Host "ThisExePath: " "$ThisExePath"
start-process -FilePath $ThisExePath
}
write-host "ParamExePath: " $ParamExePath
Run-CallThisExe -ThisExePath "$ParamExePath"
Run Code Online (Sandbox Code Playgroud)
命令行字符串
以下是从PowerShell脚本的父文件夹运行的命令行字符串:
powershell -command .\Untitled1.ps1 -NonInteractive -ParamExePath "C:\path with spaces\myapp.exe"
Run Code Online (Sandbox Code Playgroud)
产量
以下是运行脚本后输出的内容
ParamExePath: C:\path
ThisExePath: C:\path
start-process : This command cannot be run due to the error: The system cannot
find the file specified.
At C:\sample\Untitled1.ps1:11 char:5
+ start-process -FilePath $ThisExePath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
Run Code Online (Sandbox Code Playgroud)
只需改变这个:
powershell -command .\Untitled1.ps1 -NonInteractive -ParamExePath "C:\path with spaces\myapp.exe"
Run Code Online (Sandbox Code Playgroud)
对此:
powershell -file .\Untitled1.ps1 -NonInteractive -ParamExePath "C:\path with spaces\myapp.exe"
Run Code Online (Sandbox Code Playgroud)
-Command用于执行命令的参数,例如{Get-Date}
-File用于运行.ps1脚本文件的参数
-Command
Executes the specified commands (and any parameters) as though they were
typed at the Windows PowerShell command prompt, and then exits, unless
NoExit is specified. The value of Command can be "-", a string. or a
script block.
-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.
Run Code Online (Sandbox Code Playgroud)
输入Powershell /?以获取每个参数的完整详细信息
| 归档时间: |
|
| 查看次数: |
17035 次 |
| 最近记录: |