【发布时间】:2019-10-31 13:47:36
【问题描述】:
目前我们想在检索文件信息时涵盖这三种情况:
- 文字路径
- 相对路径,相对于脚本根目录
- 相对路径,相对于当前工作目录
为了涵盖这 3 种情况,我们创建了以下函数:
Function Get-FilePathItemHC {
Param (
[Parameter(Mandatory)]
[String]$Path
)
$Params = @(
@{
# Path relative to the script root
LiteralPath = Join-Path -Path $PSScriptRoot -ChildPath $Path
}
@{
# Literal or path relative to the present work directory
Path = $Path
}
)
$Item = $null
foreach ($P in $Params) {
if ($Item = Get-Item @P -ErrorAction Ignore) {
$Item
Break
}
}
if (-not $Item) {
throw "Cannot find path '$Path' because it does not exist."
}
}
这是正确的做法吗?看来我们正在重新发明轮子。
【问题讨论】:
标签: powershell path