我想執行位於script.ps1文件旁邊的program.exe ... 如何獲取此位置/文件夾? (這不是一個固定的位置/文件夾,可以是一個USB記憶棒或網絡驅動器等)Powershell:獲取A_Script.ps1執行位置的路徑
我曾嘗試使用
$MyInvocation.MyCommand.Path
但它似乎並沒有被幫我(還是我中號不使用它的分辯方式)
由於
我想執行位於script.ps1文件旁邊的program.exe ... 如何獲取此位置/文件夾? (這不是一個固定的位置/文件夾,可以是一個USB記憶棒或網絡驅動器等)Powershell:獲取A_Script.ps1執行位置的路徑
我曾嘗試使用
$MyInvocation.MyCommand.Path
但它似乎並沒有被幫我(還是我中號不使用它的分辯方式)
由於
這是模式I通常使用:
$exeName = "MyApplication.exe"
$scriptFolder = Split-Path -Parent $MyInvocation.MyCommand.Path
$exeFullPath = Join-Path -Path $scriptFolder -ChildPath $exeName
$MyInvocation是一個自動變量。
包含當前命令的信息,如姓名, 參數,參數值,以及有關命令 如何啓動,調用,或「調用」,如腳本的名稱 稱爲當前命令。
請注意,由$ MyInvocation.MyCommand返回的對象根據執行它的上下文而不同。
類型將scriptinfo從PowerShell命令窗口中返回,發現缺少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和路徑等
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代表什麼? – Nomistake 2013-04-26 12:01:39
當我執行此操作(替換$ execName的值後)我得到錯誤: 拆分路徑:無法將參數綁定到參數'路徑',因爲它爲空。 Join-Path:無法將參數綁定到參數'Path',因爲它爲空。 有什麼想法? – Nomistake 2013-04-26 12:23:17
你是從PowerGui運行的嗎?嘗試從powershell命令行運行它或從另一個腳本調用它。 - http://stackoverflow.com/questions/2989069/myinvocation-mycommand-path-is-null-in-powergui-script-editor – 2013-04-26 12:27:56
欺騙http://stackoverflow.com/questions/5466329/whats-the-best-way-to-determine-the-location-of-the-current-powershell-script – 2013-04-26 14:53:33