简体   繁体   English

PowerShell - 使用与函数相同的名称覆盖该 cmdlet 的名称时,如何在函数中调用该 cmdlet?

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

So I have a cmdlet named update-name that I have no access to change.所以我有一个名为 update-name 的 cmdlet,我无权更改。

I have created a function named update-name (the same name as the cmdlet).我创建了一个名为 update-name 的函数(与 cmdlet 同名)。 How do I call the cmdlet from the function with the same name?如何从具有相同名称的函数调用 cmdlet?

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.不幸的是,如果它是 CmdLet,这将不起作用。

You the cmdlet's module name to disambiguate the names:您可以使用 cmdlet 的模块名称来消除名称的歧义:

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

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

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

This will do the trick as well - thanks Keith Dahlby!这也能解决问题——感谢基思·达尔比! http://twitter.com/dahlbyk/status/55341994817503232 http://twitter.com/dahlbyk/status/55341994817503232

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

function update-name {
  & $unName
}

可以使用代理功能吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM