Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do You Override a Function in Powershell

All I can find online about doing this is to override a cmdlet, but I need to override a windows form event. Here is the C# equivalent of what I need:

protected override Point ScrollToControl(Control activeControl)
{
    return this.AutoScrollPosition;
}

They were saying that you just use the name of the function in the same scope and it will automatically override it, but that is not working for me.

like image 509
ReddShepherd Avatar asked Aug 03 '16 20:08

ReddShepherd


People also ask

How do I override PowerShell?

In order to change the PowerShell Execution Policy you have to start PowerShell as an administrator and run the following command Set-ExecutionPolicy -ExecutionPolicy RemoteSigned . You can also set the RemoteSigned to unrestricted, but it is discouraged by Microsoft.

What does $_ do in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

What does the command @() mean in PowerShell?

In PowerShell V2, @ is also the Splat operator. PS> # First use it to create a hashtable of parameters: PS> $params = @{path = "c:\temp"; Recurse= $true} PS> # Then use it to SPLAT the parameters - which is to say to expand a hash table PS> # into a set of command line parameters.

Can you pipe commands in PowerShell?

Most PowerShell cmdlets are designed to support pipelines. In most cases, you can pipe the results of a Get cmdlet to another cmdlet of the same noun. For example, you can pipe the output of the Get-Service cmdlet to the Start-Service or Stop-Service cmdlets.


2 Answers

If you're referring to a method, Chris Dent's answer covers that nicely!

For native PowerShell cmdlets/functions I will copy my answer from ServerFault here:

Yes, you can override Get-ChildItem or any other cmdlet in Powershell.

Name Your Function The Same

If you make a function with the same name in the same scope, yours will be used.

Example:

Function Get-ChildItem {
[CmdletBinding()]
param(
    # Simulate the parameters here
)
    # ... do stuff
}

Using Aliases

Create your own function, and then create an alias to that function, with the same name as the cmdlet you want to override.

Example:

Function My-GetChildItem {
[CmdletBinding()]
param(
    # Simulate the parameters here
)
    # ... do stuff
}

New-Alias -Name 'Get-ChildItem' -Value 'My-GetChildItem' -Scope Global

This way is nice because it's easier to test your function without stomping on the built-in function, and you can control when the cmdlet is overridden or not within your code.

To remove the alias:

Remove-Item 'Alias:\Get-ChildItem' -Force

Know the Command Precedence

about_Command_Precedence lists the order in which commands of different types are interpreted:

If you do not specify a path, Windows PowerShell uses the following precedence order when it runs commands:

  1. Alias
  2. Function
  3. Cmdlet
  4. Native Windows commands
like image 182
briantist Avatar answered Nov 15 '22 05:11

briantist


You need to override the method on a class not a function.

You may be able to override it with a script block (I've not done much with the undisclosed Forms-based class so this is a guess).

The Force parameter will let Add-Member replace a method, depending a bit on the protection / access modifiers in the class. This approach would work well for overriding ToString for instance.

$baseObject | Add-Member Point -MemberType ScriptMethod -Value { return $this.AutoScrollPosition } -Force
like image 27
Chris Dent Avatar answered Nov 15 '22 05:11

Chris Dent