PowerShell - How do I call a cmdlet in a function when overriding that cmdlet's name with the same name as the function?

℡╲_俬逩灬. 提交于 2019-11-29 03:03:26

问题


So I have a cmdlet named update-name that I have no access to change.

I have created a function named update-name (the same name as the cmdlet). How do I call the cmdlet from the function with the same name?

I've tried a few things and none of them seem to work.

function update-name {
param([string] something)
  #call cmdlet update-name here
}

There is a way to do it when it is just functions:

$unBackup = 'DefaultUpdateName'
if(!(Test-Path Function:\$unBackup)) {
    Rename-Item Function:\Update-Name $unBackup
}

function update-name {
  & $unName
}

Unfortunately that doesn't work if it is a CmdLet.


回答1:


You the cmdlet's module name to disambiguate the names:

PS> Get-Command Start-Process | Format-Table ModuleName

ModuleName
----------
Microsoft.PowerShell.Management

PS> Microsoft.PowerShell.Management\Start-Process Notepad



回答2:


This will do the trick as well - thanks Keith Dahlby! http://twitter.com/dahlbyk/status/55341994817503232

$unName=Get-Command 'Update-Name' -CommandType Cmdlet;

function update-name {
  & $unName
}



回答3:


Can you use a proxy function?



来源:https://stackoverflow.com/questions/5556651/powershell-how-do-i-call-a-cmdlet-in-a-function-when-overriding-that-cmdlets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!