Powershell:获取_Script.ps1执行位置的路径
我想执行位于script.ps1文件旁边的program.exe。。。 如何获取此位置/文件夹? (这不是固定位置/文件夹,可以是U盘或网络驱动器等) 我试着用Powershell:获取_Script.ps1执行位置的路径,powershell,Powershell,我想执行位于script.ps1文件旁边的program.exe。。。 如何获取此位置/文件夹? (这不是固定位置/文件夹,可以是U盘或网络驱动器等) 我试着用 $MyInvocation.MyCommand.Path 但是它似乎对我没有帮助(或者我没有正确地使用它) 谢谢这是我通常使用的模式: $exeName = "MyApplication.exe" $scriptFolder = Split-Path -Parent $MyInvocation.MyCommand.P
$MyInvocation.MyCommand.Path
但是它似乎对我没有帮助(或者我没有正确地使用它)
谢谢这是我通常使用的模式:
$exeName = "MyApplication.exe"
$scriptFolder = Split-Path -Parent $MyInvocation.MyCommand.Path
$exeFullPath = Join-Path -Path $scriptFolder -ChildPath $exeName
是一个自动变量
包含有关当前命令的信息,例如名称,
参数、参数值以及有关如何执行命令的信息
已启动、调用或“已调用”,例如
调用当前命令
请注意,$MyInvocation.MyCommand返回的对象根据执行它的上下文而有所不同
类型ScriptInfo是从powershell命令窗口返回的,请注意缺少路径属性:
TypeName: System.Management.Automation.ScriptInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CommandType Property System.Management.Automation.CommandTypes CommandType {get;}
Definition Property System.String Definition {get;}
Module Property System.Management.Automation.PSModuleInfo Module {get;}
ModuleName Property System.String ModuleName {get;}
Name Property System.String Name {get;}
OutputType Property System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.PSTyp...
Parameters Property System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Cult...
ParameterSets Property System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.Comma...
ScriptBlock Property System.Management.Automation.ScriptBlock ScriptBlock {get;}
Visibility Property System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
HelpUri ScriptProperty System.Object HelpUri {get=try...
当从脚本运行时,类型为ExternalScriptInfo,请注意其他属性ScriptContents和Path等
TypeName: System.Management.Automation.ExternalScriptInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CommandType Property System.Management.Automation.CommandTypes CommandType {get;}
Definition Property System.String Definition {get;}
Module Property System.Management.Automation.PSModuleInfo Module {get;}
ModuleName Property System.String ModuleName {get;}
Name Property System.String Name {get;}
OriginalEncoding Property System.Text.Encoding OriginalEncoding {get;}
OutputType Property System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.PS...
Parameters Property System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, C...
ParameterSets Property System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.Co...
Path Property System.String Path {get;}
ScriptBlock Property System.Management.Automation.ScriptBlock ScriptBlock {get;}
ScriptContents Property System.String ScriptContents {get;}
Visibility Property System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
HelpUri ScriptProperty System.Object HelpUri {get=try...
$MyInvocation代表什么?当我执行此操作时(在替换$execName的值之后),我在以下方面得到错误:Split Path:无法将参数绑定到参数“Path”,因为它为null。联接路径:无法将参数绑定到参数“Path”,因为它为null。知道吗?你是从PowerGui运行这个吗?尝试从powershell命令行运行它,或从其他脚本调用它。-我已经从命令和ISE中尝试过,属性路径似乎为null…它不应该为null,请从powershell命令提示符(它将创建一个名为test.ps1的文件):(“`$MyInvocation.MyCommand.path”| Out file test.ps1)\test.ps1dupe