Home > Software engineering >  RegistryValueKind NONE
RegistryValueKind NONE

Time:01-13

How can I set a none data type.[Microsoft.Win32.RegistryValueKind]::None in Powershell 2.0? Powershell 2.0 has types "Unknown, String, ExpandString, Binary, DWord, MultiString, QWord".
docs.microsoft.com

CodePudding user response:

(As implied in your question), [Microsoft.Win32.RegistryValueKind]::None (which translates to a REG_NONE registry value) requires at least .NET Framework 4, whereas Windows PowerShell v2 is built on .NET Framework 2. (Only Windows PowerShell v3 and above are built on .NET Framework 4 and above).

Unlike C#, PowerShell does not allow you to pass the numerical value of an enum-typed parameter if that value doesn't represent an officially defined member of that enumeration (at that time, in the underlying framework), so attempting to pass (-1), the value of [Microsoft.Win32.RegistryValueKind]::None, does not work in Windows PowerShell v2 - neither with New-ItemProperty / Set-ItemProperty's -Type parameter, nor with .NET API calls ([Microsoft.Win32.Registry]::SetValue(...)).

Therefore, in Windows PowerShell v2, calling the Windows API via P/Invoke declarations implemented via Add-Type is probably your only option - see the RegSetKeyValue() WinAPI function (among others).

CodePudding user response:

Add-Type -TypeDefinition @"
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using Microsoft.Win32;
    using Microsoft.Win32.SafeHandles;
    namespace AdvapiSolution
    {
        public class Advapi
        {
            public enum HKEY : uint
            {
                HKEY_CLASSES_ROOT = 0x80000000,
                HKEY_CURRENT_USER = 0x80000001,
                HKEY_LOCAL_MACHINE = 0x80000002,
                HKEY_USERS = 0x80000003,
                HKEY_PERFORMANCE_DATA = 0x80000004,
                HKEY_PERFORMANCE_TEXT = 0x80000050,
                HKEY_PERFORMANCE_NLSTEXT = 0x80000060,
                HKEY_CURRENT_CONFIG = 0x80000005
            }
            
            private enum VALUE_TYPE : uint
            {
                REG_NONE= 0,
                REG_SZ = 1,
                REG_EXPAND_SZ = 2,
                REG_BINARY = 3,
                REG_DWORD = 4,
                REG_DWORD_LITTLE_ENDIAN = 4,
                REG_DWORD_BIG_ENDIAN = 5,
                REG_LINK = 6,
                REG_MULTI_SZ = 7,
                REG_RESOURCE_LIST = 8,
                REG_FULL_RESOURCE_DESCRIPTOR = 9,
                REG_RESOURCE_REQUIREMENTS_LIST = 10,
                REG_QWORD_LITTLE_ENDIAN = 11
            }

            [DllImport("advapi32.dll", CharSet = CharSet.Auto, BestFitMapping = false)]
            private static extern int RegSetKeyValueW  (
            HKEY hkey, 
            string lpSubKey,
            string lpValueName,
            VALUE_TYPE type, 
            byte[] data, 
            uint dataLength);
            public int set_key(HKEY hkey, string subkey, string valuename){
                return RegSetKeyValueW(hkey, subkey, valuename, VALUE_TYPE.REG_NONE, null, 0);
            }
       }
}
"@

In my case data equal null and type is REG_NONE you can change set_key as you like. with object

$Advapiobject = New-Object 'AdvapiSolution.Advapi'
$Advapiobject.set_key('HKEY_CURRENT_USER',$yoursubkey, $yourvaluename, 'REG_NONE')

another way using a static method set_key
change set_key to public static int set_key [AdvapiSolution.Advapi]::set_key('HKEY_CURRENT_USER',$yoursubkey, $yourvaluename, 'REG_NONE').

If it returns 0, it's okay. If the function fails, the return value is a nonzero error code defined in Winerror.h.

System Error Codes

  •  Tags:  
  • Related