Powershell:获取A_Script.ps1执行位置的路径

时间:2013-04-26 11:46:05

标签: powershell

我想执行一个驻留在script.ps1文件旁边的program.exe ... 我怎样才能获得这个位置/文件夹? (这不是固定的位置/文件夹,可以是USB棒或网络驱动器等)

我尝试过使用

$MyInvocation.MyCommand.Path

但它似乎没有帮助我(或者我没有以严格的方式使用它

由于

1 个答案:

答案 0 :(得分:2)

这是我通常使用的模式:

$exeName        = "MyApplication.exe"
$scriptFolder   = Split-Path -Parent $MyInvocation.MyCommand.Path
$exeFullPath    = Join-Path -Path $scriptFolder -ChildPath $exeName

$MyInvocation是一个自动变量。

  

包含有关当前命令的信息,例如名称,   参数,参数值以及有关命令的信息   启动,调用或“调用”,例如脚本的名称   称为当前命令。

请注意,$ MyInvocation.MyCommand返回的对象根据执行它的上下文而有所不同。

从powershell命令窗口返回类型 ScriptInfo ,注意缺少Path属性:

   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...