Can I override my own Powershell cmdlet, but name it from my override

In Javascript, this is possible, I think. In Powershell, I'm not sure how:

Suppose I want to override each write-host call using my custom method, but at some point I want to execute my own internal write node inside my browser. Is it possible to save your own implantation under a different name so that you can later call it from a new implementation?

Update: it seems to me that the answer https://serverfault.com/a/642299/236470 does not fully answer the second part of my question. How to save and invoke an inline implementation?

+5
source share
2 answers

Function calls will override the cmdlets. You can read more about this from about_Command_Precedence on TechNet ...

If you do not specify a path, Windows PowerShell uses the following order of priority when executing commands:

  • Alias
  • Function
  • Cmdlet
  • Windows native commands

This way, just creating a function with the same name as the native cmdlet will give you what you want.

function Write-Host{ Process { # Executes once for each pipeline object If ($_ -match "bagels"){ Microsoft.PowerShell.Utility\Write-Host $_ -ForegroundColor Green }else{ Microsoft.PowerShell.Utility\Write-Host $_ } } } 

So now write-host works with pipelined input, with which we can filter. Calling a "real" cmdlet is as simple as specifying a module in a call. You can see that I did this twice in the above code example. Some examples of usage and output will be as follows:

enter image description here

Be careful that you do not forget that you did this if you save it in your profile or something like that. Use Get-Command Write-Host whenever in doubt. In my case, you can remove the override by calling Remove-Item function:write-host

You can also see what is called proxy functions, but I think this is too much for what you intend to do.

+8
source

Yes, you can. I have an answer for this here on ServerFault , but since this is a different site, I will copy it, since I cannot close it as a duplicate on another site.

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

Name your function The same. If you create 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 for this function with the same name as the cmdlet that 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 method is good 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 missing in your code.

To remove an alias:

 Remove-Item 'Alias:\Get-ChildItem' -Force 
+6
source

Source: https://habr.com/ru/post/1236101/


All Articles