我想执行一个位于 script.ps1 文件旁边的 program.exe ......我怎样才能得到这个位置/文件夹?(这不是一个固定的位置/文件夹,可以是 U 盘或网络驱动器等)
我试过用
$MyInvocation.MyCommand.Path
但它似乎没有帮助我(或者我没有正确使用它)
谢谢
我想执行一个位于 script.ps1 文件旁边的 program.exe ......我怎样才能得到这个位置/文件夹?(这不是一个固定的位置/文件夹,可以是 U 盘或网络驱动器等)
我试过用
$MyInvocation.MyCommand.Path
但它似乎没有帮助我(或者我没有正确使用它)
谢谢
This is the pattern I normally use:
$exeName = "MyApplication.exe"
$scriptFolder = Split-Path -Parent $MyInvocation.MyCommand.Path
$exeFullPath = Join-Path -Path $scriptFolder -ChildPath $exeName
$MyInvocation is an automatic variable.
Contains an information about the current command, such as the name, parameters, parameter values, and information about how the command was started, called, or "invoked," such as the name of the script that called the current command.
Note that the object returned by $MyInvocation.MyCommand is different depending upon the context from which it was executed.
The type ScriptInfo is returned from the powershell command window, notice the lack of Path property:
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...
Whereas the type is ExternalScriptInfo when run from a script, notice the additional properties ScriptContents and Path etc.
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...