Share via


PowerShell Tip: To write a Proxy Function


Summary

Get-Service has no paging support feature. Admin needs to scroll down or up to see the information.

Requirement

Can we have a Paging Enabled for Get-Service?
 

Note

This is just an example to demo Proxy Function, there are many ways to achieve this. In this technet wiki I will show all the methods.

Paging Result

Get-Service | Where Status -EQ 'Running'
Get-Service | where Status -eq 'Running' | Out-Host -Paging

Making a Function

Function Get-RunningService{
Get-Service | ? Status -EQ 'Running' 
}

Get-RunningService | Out-Host -Paging

Proxy Function

$var = New-Object System.Management.Automation.CommandMetadata (Get-Command Get-Service)

$var
$var = New-Object System.Management.Automation.CommandMetadata (Get-Command Get-Service)

[System.Management.Automation.ProxyCommand]::Create($var) | Out-File CL\Temp\Custom.PS1

 The Get-Service code look like this:

[CmdletBinding(DefaultParameterSetName='Default', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113332', RemotingCapability='SupportedByCommand')]
param(
    [Parameter(ParameterSetName='Default', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('ServiceName')]
    [string[]]
    ${Name},

    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [Alias('Cn')]
    [ValidateNotNullOrEmpty()]
    [string[]]
    ${ComputerName},

    [Alias('DS')]
    [switch]
    ${DependentServices},

    [Alias('SDO','ServicesDependedOn')]
    [switch]
    ${RequiredServices},

    [Parameter(ParameterSetName='DisplayName', Mandatory=$true)]
    [string[]]
    ${DisplayName},

    [ValidateNotNullOrEmpty()]
    [string[]]
    ${Include},

    [ValidateNotNullOrEmpty()]
    [string[]]
    ${Exclude},

    [Parameter(ParameterSetName='InputObject', ValueFromPipeline=$true)]
    [ValidateNotNullOrEmpty()]
    [System.ServiceProcess.ServiceController[]]
    ${InputObject})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-Service', [System.Management.Automation.CommandTypes]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}
<#

.ForwardHelpTargetName Get-Service
.ForwardHelpCategory Cmdlet

#>

Add Out-Host -Paging in this line:

$scriptCmd = {& $wrappedCmd @PSBoundParameters }
[CmdletBinding(DefaultParameterSetName='Default', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113332', RemotingCapability='SupportedByCommand')]
param(
    [Parameter(ParameterSetName='Default', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('ServiceName')]
    [string[]]
    ${Name},

    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [Alias('Cn')]
    [ValidateNotNullOrEmpty()]
    [string[]]
    ${ComputerName},

    [Alias('DS')]
    [switch]
    ${DependentServices},

    [Alias('SDO','ServicesDependedOn')]
    [switch]
    ${RequiredServices},

    [Parameter(ParameterSetName='DisplayName', Mandatory=$true)]
    [string[]]
    ${DisplayName},

    [ValidateNotNullOrEmpty()]
    [string[]]
    ${Include},

    [ValidateNotNullOrEmpty()]
    [string[]]
    ${Exclude},

    [Parameter(ParameterSetName='InputObject', ValueFromPipeline=$true)]
    [ValidateNotNullOrEmpty()]
    [System.ServiceProcess.ServiceController[]]
    ${InputObject})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-Service', [System.Management.Automation.CommandTypes]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters | Out-Host -Paging }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}
<#

.ForwardHelpTargetName Get-Service
.ForwardHelpCategory Cmdlet

#>

 Execute from PowerShell console and not from ISE.

Function Custom-Service{
[CmdletBinding(DefaultParameterSetName='Default', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113332', RemotingCapability='SupportedByCommand')]
param(
    [Parameter(ParameterSetName='Default', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('ServiceName')]
    [string[]]
    ${Name},

    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [Alias('Cn')]
    [ValidateNotNullOrEmpty()]
    [string[]]
    ${ComputerName},

    [Alias('DS')]
    [switch]
    ${DependentServices},

    [Alias('SDO','ServicesDependedOn')]
    [switch]
    ${RequiredServices},

    [Parameter(ParameterSetName='DisplayName', Mandatory=$true)]
    [string[]]
    ${DisplayName},

    [ValidateNotNullOrEmpty()]
    [string[]]
    ${Include},

    [ValidateNotNullOrEmpty()]
    [string[]]
    ${Exclude},

    [Parameter(ParameterSetName='InputObject', ValueFromPipeline=$true)]
    [ValidateNotNullOrEmpty()]
    [System.ServiceProcess.ServiceController[]]
    ${InputObject})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-Service', [System.Management.Automation.CommandTypes]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters | Out-Host -Paging }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}
<#

.ForwardHelpTargetName Get-Service
.ForwardHelpCategory Cmdlet

#>


}

Custom-Service

Video Reference

View

We can achieve this using

Get-Service | Out-Host -Paging

Note: The above command will not work in PowerShell ISE.