Twfyq Bhyry
Twfyq Bhyry

Reputation: 170

where can i find the Get-WmiObject file location in powershell?

I would like to change some code in the Get-wmiObject file itself to do some functionality for me and replace some others. most important thing is to change some command output for some reason. how can i do that or is it possible ?

Upvotes: 0

Views: 368

Answers (1)

Vish
Vish

Reputation: 466

As Santiago pointed out, proxy functions would be the right choice for modifying native code behaviour.

Step 1: Generate the proxy function

You can do this by:
$Command = Get-Command -Name Get-WmiObject
$ProxyFunction = [System.Management.Automation.ProxyCommand]::Create($Command)

$ProxyFunction | Out-File 'C:\temp\Proxy.ps1' -Force

The above will generate parameters for the Get-WmiObject plus the "calling mechanism" for the Get-WmiObject command, in C:\temp\Proxy.ps1

Step 2: Write your code

You can now write wrapper code. In the below example, I wrote some code to display PowerShell version (in the verbose stream) along with the original output.

Original working:

> Get-WmiObject win32_operatingsystem

SystemDirectory : C:\Windows\system32
Organization    :
BuildNumber     : 1234
RegisteredUser  : 1234
SerialNumber    : 1234
Version         : 1.2.3.4

Proxy function working:

> Get-WmiObject win32_operatingsystem

SystemDirectory : C:\Windows\system32
Organization    :
BuildNumber     : 1234
RegisteredUser  : 1234
SerialNumber    : 1234
Version         : 1.2.3.4

VERBOSE: PowerShell version is: 5.1.19041.1682

Full code can be found below. Keep in mind, most of the code was generated from step 1. I just added the output from step 1, plus my custom code into a function with the same name as the original function we're proxying.

If you want this function to be available as soon as you open a new PowerShell window, put it in your $PROFILE.

function Get-WmiObject
{

    [CmdletBinding(DefaultParameterSetName='query', HelpUri='https://go.microsoft.com/fwlink/?LinkID=113337', RemotingCapability='OwnedByCommand')]
    param(
        [Parameter(ParameterSetName='query', Mandatory=$true, Position=0)]
        [Parameter(ParameterSetName='list', Position=1)]
        [Alias('ClassName')]
        [string]
        ${Class},

        [Parameter(ParameterSetName='list')]
        [switch]
        ${Recurse},

        [Parameter(ParameterSetName='query', Position=1)]
        [string[]]
        ${Property},

        [Parameter(ParameterSetName='query')]
        [string]
        ${Filter},

        [switch]
        ${Amended},

        [Parameter(ParameterSetName='query')]
        [Parameter(ParameterSetName='WQLQuery')]
        [switch]
        ${DirectRead},

        [Parameter(ParameterSetName='list')]
        [switch]
        ${List},

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

        [switch]
        ${AsJob},

        [Parameter(ParameterSetName='class')]
        [Parameter(ParameterSetName='path')]
        [Parameter(ParameterSetName='WQLQuery')]
        [Parameter(ParameterSetName='query')]
        [Parameter(ParameterSetName='list')]
        [System.Management.ImpersonationLevel]
        ${Impersonation},

        [Parameter(ParameterSetName='path')]
        [Parameter(ParameterSetName='class')]
        [Parameter(ParameterSetName='WQLQuery')]
        [Parameter(ParameterSetName='query')]
        [Parameter(ParameterSetName='list')]
        [System.Management.AuthenticationLevel]
        ${Authentication},

        [Parameter(ParameterSetName='path')]
        [Parameter(ParameterSetName='class')]
        [Parameter(ParameterSetName='WQLQuery')]
        [Parameter(ParameterSetName='query')]
        [Parameter(ParameterSetName='list')]
        [string]
        ${Locale},

        [Parameter(ParameterSetName='WQLQuery')]
        [Parameter(ParameterSetName='class')]
        [Parameter(ParameterSetName='path')]
        [Parameter(ParameterSetName='query')]
        [Parameter(ParameterSetName='list')]
        [switch]
        ${EnableAllPrivileges},

        [Parameter(ParameterSetName='query')]
        [Parameter(ParameterSetName='class')]
        [Parameter(ParameterSetName='WQLQuery')]
        [Parameter(ParameterSetName='path')]
        [Parameter(ParameterSetName='list')]
        [string]
        ${Authority},

        [Parameter(ParameterSetName='path')]
        [Parameter(ParameterSetName='class')]
        [Parameter(ParameterSetName='WQLQuery')]
        [Parameter(ParameterSetName='query')]
        [Parameter(ParameterSetName='list')]
        [pscredential]
        [System.Management.Automation.CredentialAttribute()]
        ${Credential},

        [int]
        ${ThrottleLimit},

        [Parameter(ParameterSetName='WQLQuery')]
        [Parameter(ParameterSetName='class')]
        [Parameter(ParameterSetName='path')]
        [Parameter(ParameterSetName='query')]
        [Parameter(ParameterSetName='list')]
        [Alias('Cn')]
        [ValidateNotNullOrEmpty()]
        [string[]]
        ${ComputerName},

        [Parameter(ParameterSetName='class')]
        [Parameter(ParameterSetName='WQLQuery')]
        [Parameter(ParameterSetName='query')]
        [Parameter(ParameterSetName='list')]
        [Parameter(ParameterSetName='path')]
        [Alias('NS')]
        [string]
        ${Namespace})

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

    process
    {
        if ($Class -eq 'win32_operatingsystem')
        {
            Write-Verbose -Message "PowerShell version is: $($PSVersionTable.PSVersion)" -Verbose
        }
        try {
            $steppablePipeline.Process($_)
        } catch {
            throw
        }
    }

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

    .ForwardHelpTargetName Microsoft.PowerShell.Management\Get-WmiObject
    .ForwardHelpCategory Cmdlet

    #>
}


Upvotes: 1

Related Questions