Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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}

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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;
share|improve this answer
    
Thank you - it is what I needed –  CWL Sep 1 '12 at 15:59
    
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 2 '12 at 4:15
    
Can you use the registry provider for remote machines? Couldn't see an obvious way of doing this... –  Simon Catlin Sep 3 '12 at 16:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.