简体   繁体   English

PowerShell - 以管理员身份运行时恢复脚本位置的目录

[英]PowerShell - Restore directory of script location while being ran as administrator

My Situation: I have to run a PowerShell script as Administrator (because of accessing a privileged folder) and my script is also referencing files in the same directory as the script.我的情况:我必须以管理员身份运行 PowerShell 脚本(因为访问特权文件夹),并且我的脚本还引用与脚本相同目录中的文件。 I need to use a relative file path but I can't due to PowerShell switching the directory to C:\\WINDOWS\\system32 when ran as admin.我需要使用相对文件路径,但由于 PowerShell 以管理员身份运行时将目录切换到C:\\WINDOWS\\system32 ,我无法使用。

In PowerShell, is there a way to restore the directory to the current directory that the script is located?在PowerShell中,有没有办法将目录恢复到脚本所在的当前目录?

My Script: (will be ran as admin)我的脚本:(将以管理员身份运行)

Copy-Item -Path .\file1.txt -Destination 'C:\Users\privileged_folder'

Directory Structure:目录结构:

MyDir\
    file1.txt
    myscript.ps1 <- ran as admin

By Changing Location通过改变位置

In your script, you can set the location to the script folder.在您的脚本中,您可以将位置设置为脚本文件夹。 From within your script, either of the following techniques will work:在您的脚本中,以下任一技术都将起作用:

# PowerShell 3+
cd $PSScriptRoot

# PowerShell 2
cd ( Split-Path -Parent $MyInvocation.MyCommand.Definition )

Changing to $PSScriptRoot works in all currently supported versions of PowerShell, but using Split-Path to get the script directory is useful if you for some reason still have nodes running PowerShell 2.更改为$PSScriptRoot适用于所有当前支持的 PowerShell 版本,但如果您出于某种原因仍有节点运行 PowerShell 2,则使用Split-Path获取脚本目录很有用。


If you want to change back to the previous directory after your script is done executing (probably a good move) you can also make use of Push-Location and Pop-Location instead of cd or Set-Location :如果你想在你的脚本执行完成后改回上一个目录(可能是一个很好的举动),你也可以使用Push-LocationPop-Location而不是cdSet-Location

# Also aliased to pushd
Push-Location $PSScriptRoot

# My script stuff

# Also aliased to popd
Pop-Location

These two cmdlets treat locations as a stack - Push-Location changes your location and adds the directory to the location stack while Pop-Location will remove the current directory from the stack and return you to the previous location.这两个 cmdlet 将位置视为堆栈 - Push-Location更改您的位置并将目录添加到位置堆栈,而Pop-Location将从堆栈中删除当前目录并将您返回到前一个位置。 It works much like push and pop operations on arrays.它的工作原理很像数组上的 push 和 pop 操作。


By Prefixing the Relative Paths通过为相对路径添加前缀

You could also prefix your relative paths in your script with either $PSScriptRoot or ( Split-Path -Parent $MyInvocation.MyCommand.Definition ) as shown in the previous section.您还可以在脚本中使用$PSScriptRoot( Split-Path -Parent $MyInvocation.MyCommand.Definition )作为相对路径的前缀,如上一节所示。 If we attach the prefix to the otherwise relative path of file1.txt :如果我们将前缀附加到file1.txt相对路径:

$filepath1 = "${PSScriptRoot}\file1.txt"

Now you have an absolute path to file1.txt .现在您有了file1.txt的绝对路径。 Note that this technique will work with any relative path to $PSScriptRoot , it does not have to be in the same folder as your ps1 script.请注意,此技术适用于$PSScriptRoot任何相对路径,它不必与ps1脚本位于同一文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM