Suppose I have a Powershell Format-Table
string, something like:
ls | Format-Table Name, @{expression={$_.Length / 1024}; label='KB'}
I'm happy with the output I get from this, but I don't want to type it every time I use it. I'd like to be able to call it with a simple one-word command, something like:
ls | Format-KiloBytes
I gather I should be defining a function for this, since an alias can't specify the parameters. However, if I define something like:
function kilobytes {format-table Name, @{expression={$_.Length / 1024}; label='KB'}}
then it doesn't have any effect:
PS> ls | format-table Name, @{expression={$_.Length / 1024}; label='KB'}
... Produces the formatted output
PS> ls | kilobytes
... Produces output with unchanged formatting, the same as 'ls'
Edit: It appears I was confused. When experimenting, I'd already created an alias kilobytes
that was aliased to Format-Table
. I'd forgotten this, but this meant that creating a function kilobytes
succeeded without any warning, but subsequently calling kilobytes
wasn't calling the newly-created function but the existing alias.
First you can try :
function kilobytes {$input | format-table Name, @{expression={$_.Length / 1024}; label='KB'}}
You can find the explanation of $input
in about_Functions . When you use a function in a pipeline, the objects piped to the function are assigned to the $input automatic variable.
Here's a working Filter version:
filter kilobytes {$_ | select Name,@{expression={$_.Length / 1024}; label='KB'}}
Or:
filter kilobytes {[PSCustomObject]@{Name=$_.name;KB=$_.length/1024}}
A proxy function provides a good way to bind one or more parameters while still supporting the original command in a natural way. A search on Bing for "powershell proxy function" will give lots of good details.
Below is a proxy function that does exactly what you want. You can see how you still have the Format-Table parameters like -AutoSize, but no -Property parameter as that has been hard coded.
A smarter version of this proxy might actually support adding additional properties to the hard coded ones.
function Format-Kilobytes
{
[CmdletBinding(HelpUri='http://go.microsoft.com/fwlink/?LinkID=113303')]
param(
[switch]${AutoSize},
[switch]${HideTableHeaders},
[switch]${Wrap},
[System.Object]${GroupBy},
[string]${View},
[switch]${ShowError},
[switch]${DisplayError},
[switch]${Force},
[ValidateSet('CoreOnly','EnumOnly','Both')]
[string]${Expand},
[Parameter(ValueFromPipeline=$true)]
[psobject]${InputObject})
begin
{
try {
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Format-Table',
[System.Management.Automation.CommandTypes]::Cmdlet)
$properties = "Name",@{expression={$_.Length / 1024}; label='KB'}
$scriptCmd = {& $wrappedCmd @PSBoundParameters -Property $properties }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}
process { try { $steppablePipeline.Process($_) } catch { throw } }
end { try { $steppablePipeline.End() } catch { throw } }
<#
.ForwardHelpTargetName Format-Table
.ForwardHelpCategory Cmdlet
#>
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.