Aar*_*sen 492 powershell powershell-2.0
每当我需要引用一个公共模块或脚本时,我喜欢使用相对于当前脚本文件的路径,这样,我的脚本总能找到库中的其他脚本.
那么,确定当前脚本目录的最佳标准方法是什么?目前,我正在做:
$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
Run Code Online (Sandbox Code Playgroud)
我知道在模块(.psm1)中你可以使用它$PSScriptRoot来获取这些信息,但这不会在常规脚本(即.ps1文件)中设置.
获取当前PowerShell脚本文件位置的规范方法是什么?
Jar*_*Par 804
# This is an automatic variable set to the current file's/module's directory
$PSScriptRoot
Run Code Online (Sandbox Code Playgroud)
PowerShell 2
在PowerShell 3之前,没有比查询MyInvocation.MyCommand.Definition常规脚本属性更好的方法了
.我在基本上每个PowerShell脚本的顶部都有以下行:
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Run Code Online (Sandbox Code Playgroud)
And*_*der 60
如果要创建V2模块,可以使用名为的自动变量
$PSScriptRoot.
从PS>帮助automatic_variable
$PSScriptRoot
Contains the directory from which the script module is being executed.
This variable allows scripts to use the module path to access other
resources.
Cod*_*ing 30
适用于PowerShell 3.0
$PSCommandPath
Contains the full path and file name of the script that is being run.
This variable is valid in all scripts.
Run Code Online (Sandbox Code Playgroud)
那么功能是:
function Get-ScriptDirectory {
Split-Path -Parent $PSCommandPath
}
Run Code Online (Sandbox Code Playgroud)
nic*_*kzl 17
对于Powershell 3+
function Get-ScriptDirectory {
if ($psise) {
Split-Path $psise.CurrentFile.FullPath
}
else {
$global:PSScriptRoot
}
}
Run Code Online (Sandbox Code Playgroud)
我已将此功能放在我的个人资料中.也可以使用F8/Run Selection在ISE中运行.
Sea*_* C. 14
也许我在这里遗漏了一些东西......但是如果你想要现在的工作目录,你可以使用它:(Get-Location).Path用于字符串或Get-Location对象.
除非你指的是这样的东西,我在再次阅读这个问题后理解这一点.
function Get-Script-Directory
{
$scriptInvocation = (Get-Variable MyInvocation -Scope 1).Value
return Split-Path $scriptInvocation.MyCommand.Path
}
Run Code Online (Sandbox Code Playgroud)
小智 11
非常类似于已经发布的答案,但管道似乎更像PS.
$PSCommandPath | Split-Path -Parent
Run Code Online (Sandbox Code Playgroud)
Ran*_*ndy 10
我使用所有这些答案和评论中的片段,将其放在一起,供将来看到这个问题的任何人使用。它涵盖了其他答案中列出的所有情况,并且我添加了我发现的另一种情况作为故障保护。
function Get-ScriptPath()
{
# If using PowerShell ISE
if ($psISE)
{
$ScriptPath = Split-Path -Parent -Path $psISE.CurrentFile.FullPath
}
# If using PowerShell 3.0 or greater
elseif($PSVersionTable.PSVersion.Major -gt 3)
{
$ScriptPath = $PSScriptRoot
}
# If using PowerShell 2.0 or lower
else
{
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Path
}
# If still not found
# I found this can happen if running an exe created using PS2EXE module
if(-not $ScriptPath) {
$ScriptPath = [System.AppDomain]::CurrentDomain.BaseDirectory.TrimEnd('\')
}
# Return result
return $ScriptPath
}
Run Code Online (Sandbox Code Playgroud)
花了一些时间来开发一些能够接受已接受答案的东西并将其变成一个强大的功能.
不确定其他人,但我在PowerShell版本2和3上的机器环境中工作,所以我需要处理这两个.以下功能提供了优雅的后备:
Function Get-PSScriptRoot
{
$ScriptRoot = ""
Try
{
$ScriptRoot = Get-Variable -Name PSScriptRoot -ValueOnly -ErrorAction Stop
}
Catch
{
$ScriptRoot = Split-Path $script:MyInvocation.MyCommand.Path
}
Write-Output $ScriptRoot
}
Run Code Online (Sandbox Code Playgroud)
它还意味着该函数指的是脚本范围而不是Michael Sorens在其博客中概述的父母范围
我需要知道脚本名称及其执行位置.
将"$ global:"前缀添加到MyInvocation结构时,将从主脚本和导入的.PSM1库文件的主线调用时返回完整路径和脚本名称.它也可以在导入库的函数内部工作.
经过多次摆弄后,我决定使用$ global:MyInvocation.InvocationName.它可靠地运行CMD,Run With Powershell和ISE.本地和UNC发射都返回正确的路径.
我总是使用这个小片段,以相同的方式适用于PowerShell和ISE:
# Set active path to script-location:
$path = $MyInvocation.MyCommand.Path
if (!$path) {
$path = $psISE.CurrentFile.Fullpath
}
if ($path) {
$path = Split-Path $path -Parent
}
Set-Location $path
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
457178 次 |
| 最近记录: |