Powershell注册表值用作变量

Modified on: Sat, 12 Oct 2019 11:40:02 +0800

我正在研究如何从注册表中返回一个值。我只想要这个命令的AGENTGUID值。

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

$reg会将此作为一行返回。我只需{F789B761-81BE-4357-830B-368B5B3CF5E5} HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent Aentguid REG_SZ {F789B761-81BE-4357-830B-368B5B3CF5E5}

作者:user130370,CWL

最佳答案

我忘了炮轰REG.EXE并使用本机PowerShell命令(在这个例子中,你需要一些.NET魔法):

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;

相关问答

添加新评论