RegistryValueKind NONE
RegistryValueKind NONE
如何在 Powershell 2.0 中设置 none 数据类型。[Microsoft.Win32.RegistryValueKind]::None
?
Powershell 2.0 具有类型 "Unknown, String, ExpandString, Binary, DWord, MultiString, QWord"
.
docs.microsoft.com
(如您的问题所暗示),[Microsoft.Win32.RegistryValueKind]::None
(转换为 REG_NONE
注册表值)至少需要 .NET Framework 4,而 Windows PowerShell v2 构建于.NET Framework 2。(只有 Windows PowerShell v3 及更高版本是基于 .NET Framework 4 及更高版本构建的)。
与 C# 不同,PowerShell 不 允许您传递 enum
类型参数的 numerical 值,如果value 不代表该枚举的官方定义成员(当时,在底层框架中),因此尝试传递 (-1)
,[Microsoft.Win32.RegistryValueKind]::None
的值, 不是 在 Windows PowerShell v2 中工作——既不使用 New-ItemProperty
/ Set-ItemProperty
的 -Type
参数,也不使用 .NET API 调用([Microsoft.Win32.Registry]::SetValue(...)
).
因此,在 Windows PowerShell v2 中,通过 P/Invoke 声明调用 Windows API,通过 Add-Type
is probably your only option - see the RegSetKeyValue()
WinAPI 函数(以及其他函数)。
基于 ,这是一个简化的解决方案,其中:
使用 Add-Type
的 -MemberDefinition
参数来简化包装 P/Invoke 调用的类型的创建。
公开一个静态[WinApiHelper.Registry]::SetNoneValue()
方法,其中:
只专注于创造REG_NONE
价值
也支持用字节数据创建这样的值([byte[]]
数组)
没有 return 值,但如果发生错误则抛出 Win32Exception
。
Add-Type -Namespace WinApiHelper -Name Registry -MemberDefinition @'
public enum HKEY : uint
{
HKEY_CLASSES_ROOT = 0x80000000,
HKEY_CURRENT_CONFIG = 0x80000005,
HKEY_CURRENT_USER = 0x80000001,
HKEY_LOCAL_MACHINE = 0x80000002,
HKEY_USERS = 0x80000003
}
[DllImport("advapi32.dll")]
private static extern int RegSetKeyValue(
HKEY hkey,
string lpSubKey,
string lpValueName,
UInt32 type,
byte[] data,
UInt32 dataLength
);
public static void SetNoneValue(HKEY hkey, string subkey, string valuename, byte[] bytes)
{
int rc = RegSetKeyValue(hkey, subkey, valuename, 0 /* REG_NONE */, bytes, (UInt32) (bytes == null ? 0 : bytes.Length));
if (rc != 0) {
// Access the error code in PowerShell with $Error[0].Exception.InnerException.NativeErrorCode
throw new System.ComponentModel.Win32Exception(rc);
}
}
// Overload that creates an empty value.
// Note: .NET 2 doesn't support optional parameters, so an explicit overload is required.
public static void SetNoneValue(HKEY hkey, string subkey, string valuename) {
SetNoneValue(hkey, subkey, valuename, null);
}
'@
调用示例:
# Create an empty REG_NONE value named 'bar1' in key HKEY_CURRENT_USER\foo.
[WinApiHelper.Registry]::SetNoneValue('HKEY_CURRENT_USER', 'foo', 'bar1')
# Create a REG_NONE value named 'bar2' with a byte array.
[WinApiHelper.Registry]::SetNoneValue('HKEY_CURRENT_USER', 'foo', 'bar2', [byte[]] (42, 43))
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);
}
}
}
"@
在我的例子中,数据等于 null
,类型是 REG_NONE
。您可以根据需要更改 set_key
object:
$Advapiobject = New-Object 'AdvapiSolution.Advapi'
$Advapiobject.set_key('HKEY_CURRENT_USER',$yoursubkey, $yourvaluename, 'REG_NONE')
另一种方法是使用静态方法set_key
。
将 set_key
更改为 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
.
如何在 Powershell 2.0 中设置 none 数据类型。[Microsoft.Win32.RegistryValueKind]::None
?
Powershell 2.0 具有类型 "Unknown, String, ExpandString, Binary, DWord, MultiString, QWord"
.
docs.microsoft.com
(如您的问题所暗示),[Microsoft.Win32.RegistryValueKind]::None
(转换为 REG_NONE
注册表值)至少需要 .NET Framework 4,而 Windows PowerShell v2 构建于.NET Framework 2。(只有 Windows PowerShell v3 及更高版本是基于 .NET Framework 4 及更高版本构建的)。
与 C# 不同,PowerShell 不 允许您传递 enum
类型参数的 numerical 值,如果value 不代表该枚举的官方定义成员(当时,在底层框架中),因此尝试传递 (-1)
,[Microsoft.Win32.RegistryValueKind]::None
的值, 不是 在 Windows PowerShell v2 中工作——既不使用 New-ItemProperty
/ Set-ItemProperty
的 -Type
参数,也不使用 .NET API 调用([Microsoft.Win32.Registry]::SetValue(...)
).
因此,在 Windows PowerShell v2 中,通过 P/Invoke 声明调用 Windows API,通过 Add-Type
is probably your only option - see the RegSetKeyValue()
WinAPI 函数(以及其他函数)。
基于
使用
Add-Type
的-MemberDefinition
参数来简化包装 P/Invoke 调用的类型的创建。公开一个静态
[WinApiHelper.Registry]::SetNoneValue()
方法,其中:只专注于创造
REG_NONE
价值也支持用字节数据创建这样的值(
[byte[]]
数组)没有 return 值,但如果发生错误则抛出
Win32Exception
。
Add-Type -Namespace WinApiHelper -Name Registry -MemberDefinition @'
public enum HKEY : uint
{
HKEY_CLASSES_ROOT = 0x80000000,
HKEY_CURRENT_CONFIG = 0x80000005,
HKEY_CURRENT_USER = 0x80000001,
HKEY_LOCAL_MACHINE = 0x80000002,
HKEY_USERS = 0x80000003
}
[DllImport("advapi32.dll")]
private static extern int RegSetKeyValue(
HKEY hkey,
string lpSubKey,
string lpValueName,
UInt32 type,
byte[] data,
UInt32 dataLength
);
public static void SetNoneValue(HKEY hkey, string subkey, string valuename, byte[] bytes)
{
int rc = RegSetKeyValue(hkey, subkey, valuename, 0 /* REG_NONE */, bytes, (UInt32) (bytes == null ? 0 : bytes.Length));
if (rc != 0) {
// Access the error code in PowerShell with $Error[0].Exception.InnerException.NativeErrorCode
throw new System.ComponentModel.Win32Exception(rc);
}
}
// Overload that creates an empty value.
// Note: .NET 2 doesn't support optional parameters, so an explicit overload is required.
public static void SetNoneValue(HKEY hkey, string subkey, string valuename) {
SetNoneValue(hkey, subkey, valuename, null);
}
'@
调用示例:
# Create an empty REG_NONE value named 'bar1' in key HKEY_CURRENT_USER\foo.
[WinApiHelper.Registry]::SetNoneValue('HKEY_CURRENT_USER', 'foo', 'bar1')
# Create a REG_NONE value named 'bar2' with a byte array.
[WinApiHelper.Registry]::SetNoneValue('HKEY_CURRENT_USER', 'foo', 'bar2', [byte[]] (42, 43))
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);
}
}
}
"@
在我的例子中,数据等于 null
,类型是 REG_NONE
。您可以根据需要更改 set_key
object:
$Advapiobject = New-Object 'AdvapiSolution.Advapi'
$Advapiobject.set_key('HKEY_CURRENT_USER',$yoursubkey, $yourvaluename, 'REG_NONE')
另一种方法是使用静态方法set_key
。
将 set_key
更改为 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 inWinerror.h
.