5

I am looking on how to return one value from the registry. I only want the AGENTGUID value from this command.

$reg=reg query "\\$computer\HKLM\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent" /v Agentguid

$reg will return this as one line. I only need {F789B761-81BE-4357-830B-368B5B3CF5E5} HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent Aentguid REG_SZ {F789B761-81BE-4357-830B-368B5B3CF5E5}

CWL
  • 107
  • 2
  • 12

1 Answers1

4

I'd forget shelling out to REG.EXE and use native PowerShell commands (in this instance, you need a bit of .NET magic):

function getAgentGUID() {

    param( [String] $computername = "" );

    [String]                      $Local:strServerName     = $computername;
    [Microsoft.Win32.RegistryKey] $Local:objHKLMRootRegKey = $null;
    [Microsoft.Win32.RegistryKey] $Local:objMyKey          = $null;
    [String]                      $Local:strAgentGUID      = "";

    try {
        $objHKLMRootRegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey( [Microsoft.Win32.RegistryHive]::LocalMachine, $strServerName )

        try {
            $objMyKey = $objHKLMRootRegKey.OpenSubKey( "SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent" );
            $strAgentGUID = $objMyKey.GetValue( "Agentguid" );
            } #try
        catch { 
            Write-Error -Message "ERROR : Failed to get AgentGUID value.";
            } #catch

        } #try
    catch {
        Write-Error -Message "ERROR : Failed to connect to remote registry.";
        } #catch

return $strAgentGUID;
}


#
# Get the McAfee agent GUID for remote machine called fred.
#

[String] $Local:strAgentGUID = getAgentGUID -computer "fred";

Write-Host -foregroundColor "red" -backgroundColor "white" -Object ( "GUID is : [" + $strAgentGUID + "]" );

exit;
Simon Catlin
  • 5,222
  • 3
  • 16
  • 20
  • YOu could have also simply used the registry provider in powershell ad used get-childitem and test-path. using the .net framework calls certainly works but seems painful than gci – Jim B Sep 02 '12 at 04:15
  • Can you use the registry provider for remote machines? Couldn't see an obvious way of doing this... – Simon Catlin Sep 03 '12 at 16:12