![](/img/trans.png)
[英]How do I write an output to a file with parameter path in PowerShell?
[英]How to write a Powershell file parameter that accepts a relative path
当 PSD1 的路径是完全限定路径(或当前位置与当前目录相同)时,以下示例代码可以正确读取 PSD1 和常规文件。 提供相对路径时失败。
param (
[Parameter(Mandatory=$true)][System.IO.FileInfo]$PsdFile,
[Parameter(Mandatory=$false)][System.IO.FileInfo]$RegularFile = 'testfile'
)
$RegularFileContent = Get-Content $RegularFile
$PsdData = Import-LocalizedData -BaseDirectory $PsdFile.DirectoryName -FileName $PsdFile.Name
echo "regular file contents: $RegularFileContent"
echo "Psd data:"
echo $PsdData.Name
我该如何解决这个问题,以便用户可以输入相对路径?
我相信潜在的问题类似于这篇关于 FileInfo 的帖子中描述的问题,但我不知道如何将Resolve-Path
或类似的问题合并到参数处理中。
您的很多问题都取决于您指的是哪个亲戚,因为它本身就是relative 。 如果您希望路径相对于用户的工作目录而不是脚本的根目录,您可以像这样完成:
param(
[Parameter(Mandatory)]
[string]
$PsdFile,
[Parameter()] # Mandatory is $false by default
[string]
$RegularFile = 'testfile'
)
[System.IO.FileInfo]$psd = Join-Path -Path $PWD.Path -ChildPath $PsdFile
$data = Import-LocalizedData -BaseDirectory $psd.DirectoryName -FileName $PsdFile.Name
$content = Get-Content -Path $RegularFile
"regular file contents: $content"
"Psd data:" + $data.Name
param (
[Parameter(Mandatory = $true)]
[ValidateScript( {
if (-Not (Test-Path -Path $_ ) ) { throw "File not found." }
elseif (-Not (Test-Path -Path $_ -PathType Leaf) ) { throw "PsdFile should be a file." }
return $true
})]
[System.IO.FileInfo] $PsdFile,
[Parameter(Mandatory = $false)]
[ValidateScript( {
if (-Not (Test-Path -Path $_ ) ) { throw "File not found." }
elseif (-Not (Test-Path -Path $_ -PathType Leaf) ) { throw "PsdFile should be a file." }
return $true
})]
[System.IO.FileInfo] $RegularFile = 'testfile'
)
$PsdFile = (Resolve-Path -Path $PsdFile).Path;
$RegularFile = (Resolve-Path -Path $RegularFile).Path;
$RegularFileContent = Get-Content $RegularFile.FullName
$PsdData = Import-LocalizedData -BaseDirectory $PsdFile.DirectoryName -FileName $PsdFile.Name
我使用与@luri 类似的方法,结合ValidateScript
来测试路径是否存在。 此示例专门针对文件进行测试,并在传递目录时抛出错误。 这对于用户使用<TAB>
快速自动完成-Path
参数的路径的情况很有用。
function PathTestFunction
{
[CmdletBinding()]
param (
[Parameter(Mandatory,
ParameterSetName = 'Path',
ValueFromPipeline,
ValueFromPipelineByPropertyName
)]
[ValidateScript({
# Using Exists() because Test-Path doesn't test for system/hidden files
if ([System.IO.Directory]::Exists($(Resolve-Path -Path $_)))
{
throw "'$(Resolve-Path $_)' is a directory. Please use a file for the -Path parameter."
}
if ([System.IO.File]::Exists($(Resolve-Path -Path $_)) -eq $false)
{
$null = Resolve-Path $_ -ErrorVariable pathError
# Displays a full path name in the error message (vs a relative path if one was passed)
throw $pathError.Exception
}
return $true
})]
[System.IO.FileInfo]$Path
)
Get-Item $Path -Force
}
您可以编写第二个 ParameterSet 以包含-LiteralPath
参数以增加稳健性 - 这将允许用户传递包含通配符的路径(例如'C:\Test[txt]File.txt'
)。
使用<TAB>
自动完成带有通配符的路径仍然适用于上面的示例 - 但它不允许用户键入其中包含未转义通配符的完整路径名,因此需要-Path
和-LiteralPath
参数案件:
'.\Test`[txt`]File.txt'
将与-Path
一起使用(如果使用<TAB>
自动完成路径)'C:\Test`[txt`]File.txt'
将与-Path
一起使用(如果使用<TAB>
自动完成路径)'C:\Test[txt]File.txt'
不适用于-Path
(如果手动输入)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.