pentest-distro-builder/filesystem/etc/skel/.vscode/extensions/ms-vscode.powershell-1.8.4/examples/PathProcessingWildcards.ps1

93 lines
No EOL
4.1 KiB
PowerShell

<#
.SYNOPSIS
Demonstrates how to write a command that works with paths that allow
wildards and must exist.
.DESCRIPTION
This command also demonstrates how you need to supply a LiteralPath
parameter when your Path parameter accepts wildcards. This is in order
to handle paths like foo[1].txt. If you pass this path to the Path
parameter, it will fail to find this file because "[1]" is interpreted
as a wildcard spec e.g it resolves to foo1.txt. The LiteralPath parameter
is used in this case as it does not interpret wildcard chars.
.EXAMPLE
C:\PS> Import-FileWildcard -LiteralPath ..\..\Tests\foo[1].txt -WhatIf
This example shows how to use the LiteralPath parameter with a path
that happens to use the wildcard chars "[" and "]".
#>
function Import-FileWildcard {
[CmdletBinding(SupportsShouldProcess=$true, DefaultParameterSetName='Path')]
param(
# Specifies a path to one or more locations. Wildcards are permitted.
[Parameter(Mandatory=$true,
Position=0,
ParameterSetName="Path",
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage="Path to one or more locations.")]
[ValidateNotNullOrEmpty()]
[SupportsWildcards()]
[string[]]
$Path,
# Specifies a path to one or more locations. Unlike the Path parameter, the value of the LiteralPath parameter is
# used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters,
# enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any
# characters as escape sequences.
[Parameter(Mandatory=$true,
Position=0,
ParameterSetName="LiteralPath",
ValueFromPipelineByPropertyName=$true,
HelpMessage="Literal path to one or more locations.")]
[Alias("PSPath")]
[ValidateNotNullOrEmpty()]
[string[]]
$LiteralPath
)
begin {
}
process {
# Modify [CmdletBinding()] to [CmdletBinding(SupportsShouldProcess=$true, DefaultParameterSetName='Path')]
$paths = @()
if ($psCmdlet.ParameterSetName -eq 'Path') {
foreach ($aPath in $Path) {
if (!(Test-Path -Path $aPath)) {
$ex = New-Object System.Management.Automation.ItemNotFoundException "Cannot find path '$aPath' because it does not exist."
$category = [System.Management.Automation.ErrorCategory]::ObjectNotFound
$errRecord = New-Object System.Management.Automation.ErrorRecord $ex,'PathNotFound',$category,$aPath
$psCmdlet.WriteError($errRecord)
continue
}
# Resolve any wildcards that might be in the path
$provider = $null
$paths += $psCmdlet.SessionState.Path.GetResolvedProviderPathFromPSPath($aPath, [ref]$provider)
}
}
else {
foreach ($aPath in $LiteralPath) {
if (!(Test-Path -LiteralPath $aPath)) {
$ex = New-Object System.Management.Automation.ItemNotFoundException "Cannot find path '$aPath' because it does not exist."
$category = [System.Management.Automation.ErrorCategory]::ObjectNotFound
$errRecord = New-Object System.Management.Automation.ErrorRecord $ex,'PathNotFound',$category,$aPath
$psCmdlet.WriteError($errRecord)
continue
}
# Resolve any relative paths
$paths += $psCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($aPath)
}
}
foreach ($aPath in $paths) {
if ($pscmdlet.ShouldProcess($aPath, 'Operation')) {
# Process each path
$aPath
}
}
}
end {
}
}