Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell `启动进程`无法找到路径中存在的文件,即使给定了该文件的绝对路径_Powershell_Powershell Core - Fatal编程技术网

Powershell `启动进程`无法找到路径中存在的文件,即使给定了该文件的绝对路径

Powershell `启动进程`无法找到路径中存在的文件,即使给定了该文件的绝对路径,powershell,powershell-core,Powershell,Powershell Core,我试图在Powershell Core中使用启动进程,使用变量指定要启动的进程。我知道dotnet在我的路径中,所以这是可行的: $DotnetRunCommandApp='run--project path/to/my/project.csproj' 启动进程dotnet-ArgumentList$DotnetRunCommandApp 但是,当我尝试将dotnet移动到如下变量中时: $DotnetCommand='dotnet' $DotnetRunCommandApp='run--pr

我试图在Powershell Core中使用
启动进程
,使用变量指定要启动的进程。我知道
dotnet
在我的路径中,所以这是可行的:

$DotnetRunCommandApp='run--project path/to/my/project.csproj'
启动进程dotnet-ArgumentList$DotnetRunCommandApp
但是,当我尝试将
dotnet
移动到如下变量中时:

$DotnetCommand='dotnet'
$DotnetRunCommandApp='run--project path/to/my/project.csproj'
启动进程$DotnetCommand-参数列表$DotnetRunCommandApp
甚至使用
dotnet
的绝对路径,如下所示:

$DotnetCommand=Resolve Path((Get Command dotnet).Source | Out String-NoNewline)
if(-not(测试路径$DotnetCommand)){
写入错误“找不到“$DotnetCommand”
}否则{
编写调试“发现$DotnetCommand”#记录“调试:发现C:\Program Files\dotnet\dotnet.exe”
}
$DotnetRunCommandApp='run--project path/to/my/project.csproj'
启动进程$DotnetCommand-参数列表$DotnetRunCommandApp
我收到一个
无效操作异常

This command cannot be run due to the error: The system cannot find the file specified.
不确定为什么
Start Process
无法找到该文件,尽管它确实存在于我的路径中,或者甚至在我为cmdlt提供完整路径时


我的最终目标是能够指定对象中的参数,并将该对象传递给
启动进程
。这是在我的构建代理上运行的pwsh脚本的一部分,用于测试webjob模板。虽然我希望本地行为略有不同,但请参见下面的开关
$Azure

$StartProcessParams=@{
FilePath=$DotnetCommand
ArgumentList=$DotnetRunCommandApp
RedirectStandardError=(解析路径$WebJobErrorLogFile)
重定向标准输出=(解析路径$WebJobLogFile)
PassThru=$true;
#如果我们将进程保持在Azure上的同一“窗口”中,日志记录效果最好
#WebJob在新的windows中运行,以便在出现任何错误时很容易终止进程
NoNewWindow=$Azure;
}
$WebJobProcess=启动进程$StartProcess参数

根据启动过程的帮助文档

If you specify only a filename, use the WorkingDirectory parameter to specify the path."

The WorkingDirectory Paramter "specifies the location of the executable file or document that runs in the process. The default is the current folder."
请尝试以下命令:

Start-Process $DotnetCommand -ArgumentList $DotnetRunCommandApp -WorkingDirectory </dir/to/PATH>
启动进程$DotnetCommand-ArgumentList$DotnetRunCommandApp-WorkingDirectory

您的问题可能是它试图从您当前的目录而不是您的路径位置解析可变内容“dotnet”。

正如@iRon在评论中指出的,问题是我使用的不是正确的。我使用的是
$StartProcessParams
而不是
@StartProcessParams
(区别在于第一个字符;
$
@
)。这很好:

$StartProcessParams=@{
FilePath=$DotnetCommand
ArgumentList=$DotnetRunCommandApp
RedirectStandardError=(解析路径$WebJobErrorLogFile)
重定向标准输出=(解析路径$WebJobLogFile)
PassThru=$true;
#如果我们将进程保持在Azure上的同一“窗口”中,日志记录效果最好
#WebJob在新的windows中运行,以便在出现任何错误时很容易终止进程
NoNewWindow=$Azure;
}
$WebJobProcess=Start Process@StartProcessParams

我已经尝试过这些答案,但它们对我不起作用:我无法在PowerShell 5中重现这些答案。1@GeorgeChakhidze我使用的是powershell core,我用这些信息更新了问题。我猜您实际上想使用参数:
$WebJobProcess=Start Process@StartProcessParams
否,您的变量前面有
$
符号(
$StartProcessParams
),我有
@
符号:(
@StartProcessParams
),请阅读有关我的链接,但不适用于此特定场景
dotnet
需要在我所在的目录中运行,以避免项目使用错误的目录查找appsettings.json。但它可能会帮助那些不使用
dotnet
的人,特别是那些投票率较高的人!