Get Private Key and Certificate from .PFX

Export the private key from .PFX file

C:\> openssl pkcs12 -in filename.pfx -nocerts -out privatekey.pem

Decrypt the encrypted private key

C:\> openssl rsa -in privatekey.pem -out private.key

The final private.key file is the the private key file.

Export the certificate from the .PFX file

C:\> openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem

The cert.pem output from the command is the certificate file.

Advertisements

Azure Batch Exit Code -532462766

Symptom: Task completed with exit code ‘-532462766’

Azure Batch would throw this exception when running the jobs or tasks. Application Insight may or may not catch this exception, but Azure Portal and Batch Labs would show the jobs / tasks exit with code ‘-532462766’.

Troubleshooting

Some ways to troubleshoot:

  • Remote desktop to the machine.
    Set PoolSpecification.KeepAlive to true
    to keep the virtual machine alive after the job is done. This allows you to investigate the TVM.
  • Check Window’s Event Viewer.
  • Run the Job / Task with elevated permission.
    task.UserIdentity = new UserIdentity(new AutoUserSpecification(elevationLevel: ElevationLevel.Admin, scope: AutoUserScope.Task));
  • Set Application Insight to diagnose the service.
  • Use Batch Labs to see more information for a particular pool, TVM, job or task.

Reference

Azure Batch User Accounts
Batch Labs

What’s My .NET Framework Version?

Using Registry

Most accurate is through registry. For .NET Framework 1-4:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP

And newer .NET Framework:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full

The DWORD represents different .NET Framework version.

Using Code

Easier way is to use code:

void Main()
{
    GetVersionFromRegistry();
    GetNet45PlusFromRegistry();
}

private static void GetVersionFromRegistry()
{
    // Opens the registry key for the .NET Framework entry.
    using (RegistryKey ndpKey =
        RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
        OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
    {
        // As an alternative, if you know the computers you will query are running .NET Framework 4.5
        // or later, you can use:
        // using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
        // RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
        foreach (string versionKeyName in ndpKey.GetSubKeyNames())
        {
            if (versionKeyName.StartsWith("v"))
            {
                RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                string name = (string)versionKey.GetValue("Version", "");
                string sp = versionKey.GetValue("SP", "").ToString();
                string install = versionKey.GetValue("Install", "").ToString();
                //no install info, must be later.
                if (install == "")
                {
                    var version = versionKeyName + "  " + name;
                    Console.WriteLine(version);
                    version.Dump();
                }
                else
                {
                    if (sp != "" && install == "1")
                    {
                        var version = versionKeyName + "  " + name + "  SP" + sp;
                        Console.WriteLine(version);
                        version.Dump();
                    }
                }
                if (name != "")
                {
                    continue;
                }
                foreach (string subKeyName in versionKey.GetSubKeyNames())
                {
                    RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                    name = (string)subKey.GetValue("Version", "");
                    if (name != "")
                        sp = subKey.GetValue("SP", "").ToString();
                    install = subKey.GetValue("Install", "").ToString();
                    //no install info, must be later.
                    if (install == "")
                    {
                        var version = versionKeyName + "  " + name;
                        Console.WriteLine(version);
                        version.Dump();
                    }
                    else
                    {
                        if (sp != "" && install == "1")
                        {
                            var version = "  " + subKeyName + "  " + name + "  SP" + sp;
                            Console.WriteLine(version);
                            version.Dump();
                        }
                        else if (install == "1")
                        {
                            var version = "  " + subKeyName + "  " + name;
                            Console.WriteLine(version);
                            version.Dump();
                        }
                    }
                }
            }
        }
    }
}

private static void GetNet45PlusFromRegistry()
{
    const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
    string version = string.Empty;

    using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
    {
        if (ndpKey != null && ndpKey.GetValue("Release") != null)
        {
            version = ".NET Framework Version: " + CheckFor45PlusVersion((int)ndpKey.GetValue("Release"));
            Console.WriteLine(version);
            version.Dump();
        }
        else
        {
            version = ".NET Framework Version 4.5 or later is not detected.";
            Console.WriteLine(version);
            version.Dump();
        }
    }
}

// Checking the version using >= will enable forward compatibility.
private static string CheckFor45PlusVersion(int releaseKey)
{
    if (releaseKey >= 461308)
        return "4.7.1 or later";
    if (releaseKey >= 460798)
        return "4.7";
    if (releaseKey >= 394802)
        return "4.6.2";
    if (releaseKey >= 394254)
    {
        return "4.6.1";
    }
    if (releaseKey >= 393295)
    {
        return "4.6";
    }
    if ((releaseKey >= 379893))
    {
        return "4.5.2";
    }
    if ((releaseKey >= 378675))
    {
        return "4.5.1";
    }
    if ((releaseKey >= 378389))
    {
        return "4.5";
    }
    // This code should never execute. A non-null release key should mean
    // that 4.5 or later is installed.
    return "No 4.5 or later version detected";
}

Reference
Microsoft Docs

GAC and GACUTIL in a Nutshell

What’s GAC?

Global Assembly Cache.

What’s in the GAC?

See GAC content:

C:\> gacutil -l
or
C:\> gacutil /l
or a specific assembly:
C:\> gacutil -l msshrtmi

Or alternatively, look into the following folders:

.NET Framework 4 and newer: %windir%\Microsoft.NET\assembly.
Earlier versions of the .NET Framework: %windir%\assembly.

Accessing GACUTIL

If you have Visual Studio installed, GACUTIL should be accessible in Visual Studio Developer Command Prompt:

C:\Users\JohnDoe\Source> gacutil

Where’s GACUTIL?

If you don’t have Visual Studio, chances are GACUTIL is not recognized in the command prompt. But it doesn’t mean you don’t have it.

Try look in the following folders:

C:\Program Files (x86)\Microsoft SDKs\Windows\v6.0A\bin
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.2 Tools

Or any other version available.

GACUTIL comes with Windows SDK. If you still can’t find, download Windows SDK.

Installing New GAC

C:\> gacutil /i MyLibrary.dll

GACUTIL will place the assembly in the GAC directory based on the version of .NET Framework used to compile the assembly.

You don’t need to restart your computer for the installation to take effect.

References

Global Assembly Cache GAC
GACUTIL
View GAC Content