Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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 - Fatal编程技术网

Powershell “错误”;无法从中找到路径;在程序启动期间

Powershell “错误”;无法从中找到路径;在程序启动期间,powershell,Powershell,为什么这个代码没有按计划工作? powershell的自动变量用于脚本运行的目标目录的位置是什么 Set-Location -Path $PSScriptRoot -PassThru $file1.text="$PSScriptRoot\MIK_Autokontinent.xml 在启动过程中,程序返回错误无法从c:\MIK\u autocontinent.xml 但是该文件位于c:\program\MIK\u autocontinent.xml 这行正在返回错误 $inputpecentw1

为什么这个代码没有按计划工作? powershell的自动变量用于脚本运行的目标目录的位置是什么

Set-Location -Path $PSScriptRoot -PassThru
$file1.text="$PSScriptRoot\MIK_Autokontinent.xml
在启动过程中,程序返回错误无法从
c:\MIK\u autocontinent.xml
但是该文件位于
c:\program\MIK\u autocontinent.xml

这行正在返回错误

$inputpecentw1.Text = [xml](Get-Content $file1.text) | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") } } | Out-String

您正试图在根目录中查找该文件,但该程序位于
program
文件夹中。如果脚本与文件本身位于同一文件夹中,只需使用文件名作为参考,如下所示
$file1.text=“MIK\u Autokontinent.xml”

但是,如果您想以您现在的方式执行此操作,并从根文件夹中执行此操作,请执行以下操作:
$file1.text=“$PSScriptRoot\program\MIK\u autokontint.xml”

,但如果我从其他位置运行此脚本。如何重新制作从任何地方运行的程序?如何从脚本生成exe文件,使其在任何目录下都能工作?我在回答末尾提供的示例中解释过,如果要从根文件夹中查找文件,请使用
$file1.text=“$PSScriptRoot\program\MIK_autokontient.xml”
,这意味着当脚本在文件夹之外运行时可以找到它。您是否仍会收到路径错误,如果收到,是否可以在此处提供该错误?PS C:\program\t>C:\program\prg.ps1 get Content:找不到路径“C:\program\program\MIK\u AutoPremium.xml”,因为它不存在。C:\program\prg.ps1:59个字符:37+$InputSpecentW2.Text=[xml](获取内容$file2.Text)| ForEac…+~~~~~~~~~~~~~~~~~~+CategoryInfo:ObjectNotFound:(C:\program\program\MIK_AutoPremium.xml:String)[获取内容],ItemNotFoundException+FullyQualifiedErrorId:PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand无法为空值的表达式调用方法。当我们从.ps1生成exe时,我们丢失了所有路径?为什么?