PowerShell包含另一个脚本文件和获取当前脚本所在目录的方法例子
2019/7/10 21:27:20
本文主要是介绍PowerShell包含另一个脚本文件和获取当前脚本所在目录的方法例子,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文介绍在PowerShell脚本中,如何获取脚本文件(.ps1文件)所在的目录。本文介绍在方法适用于PowerShell 3.0。
在PowerShell 3.0中,有一个变量可以很方便的获取脚本所在的目录。我们在e:\ps\script1.ps1和script2.ps1,内容分别如下:
script1.ps1内容:
Write-Host "This is script1.ps1"
Write-Host "Let me call script2.ps1:"
. "$PSScriptRoot\script2.ps1"
script2.ps1内容:
Write-Host "This is script2.ps1"
运行script1.ps1,将用调用script2.ps1的脚本代码。
注意,在script1.ps1脚本中,我们使用了$PSScriptRoot这一变量来获取脚本所在的目录。这个变量在PowerShell 3.0中新增加的一个变量。在运行script1.ps1脚本时,这个变量就代指了E:\ps这个目录路径。
好了,关于PowerShell使用$PSScriptRoot获取脚本所在的目录,本文就介绍这么多,希望对大家有所帮助。
这篇关于PowerShell包含另一个脚本文件和获取当前脚本所在目录的方法例子的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-09-14SharePoint 2019 用 PowerShell将启用发布功能站点网站集另存为模板
- 2022-08-30PowerShell教程 - 程序性能和BUG分析工具
- 2022-08-30PowerShell教程 - 模块管理(Modules Management)
- 2022-08-29PowerShell教程 - Web requests(Web请求)
- 2022-08-26PowerShell教程 - 日期时间管理(Date & Time Management)
- 2022-08-25PowerShell教程 - 磁盘与硬件管理(Disk & Hardware Management)
- 2022-08-25PowerShell教程 - 系统事件管理(System Event Management)
- 2022-08-25PowerShell教程 - 文件系统管理(File System Management)
- 2022-08-24PowerShell教程 - 网络管理(Network Management)
- 2022-08-24PowerShell