使用vbscript设置注册表multiStringValue时出现无效参数错误
这是针对我之前问过的一个问题,但在执行另一个子项时,相同的条件不起作用。下面所有变量都定义为正确的字符串。我在此行设置值时出错:使用vbscript设置注册表multiStringValue时出现无效参数错误,vbscript,Vbscript,这是针对我之前问过的一个问题,但在执行另一个子项时,相同的条件不起作用。下面所有变量都定义为正确的字符串。我在此行设置值时出错: objReg.setMultiStringValue HKCU,IE_Main,mStrSecStartPages,allURLs 代码如下 return = objReg.getMultiStringValue (HKCU,IE_Main,mStrSecStartPages,multiStringValues) 'If values found in Second
objReg.setMultiStringValue HKCU,IE_Main,mStrSecStartPages,allURLs
代码如下
return = objReg.getMultiStringValue (HKCU,IE_Main,mStrSecStartPages,multiStringValues)
'If values found in Secondary Start Pages
If return=0 Then
ReDim allURLs(0)
'Read all values and only store non intranet values to array
For Each itemname In multiStringValues
If itemname <> strFunctionIntranet1 And itemname <> strFunctionIntranet2 And itemname <> strFunctionIntranet3 And itemname <> strFunctionIntranet4 Then
ReDim Preserve allURLs(UBound(allURLs)+1)
allURLs(UBound(allURLs)) = itemname
End If
Next
'Remove current key holding existing values
objReg.DeleteValue HKCU,IE_Main,mStrSecStartPages
'Set new values based on values read and user's intranet
if UBound(allURLs)>=0 Then
wscript.echo "in setting"
objReg.setMultiStringValue HKCU,IE_Main,mStrSecStartPages,allURLs
End If
wscript.echo "out setting"
End If
return=objReg.getMultiStringValue(HKCU,IE_Main,MSTRSECStartPage,multistringvalue)
'如果在辅助起始页中找到值
如果return=0,则
ReDim allURLs(0)
'读取所有值并仅将非intranet值存储到阵列
对于多字符串值中的每个itemname
如果itemname strFunctionIntranet1和itemname strFunctionIntranet2和itemname strFunctionIntranet3和itemname strFunctionIntranet4,则
雷迪姆保留冲积层(UBound(冲积层)+1)
allURLs(UBound(allURLs))=项目名称
如果结束
下一个
'删除保存现有值的当前键
objReg.DeleteValue HKCU,IE_Main,mStrSecStartPages
'根据读取的值和用户的intranet设置新值
如果UBound(allURLs)>=0,则
wscript.echo“在设置中”
objReg.setMultiStringValue HKCU,IE_Main,mStrSecStartPages,allURLs
如果结束
wscript.echo“放线设置”
如果结束
问题是,即使REG_MULTI_SZ值中没有任何值,您仍然会返回一个空数组,这意味着当您在数组中循环并使用
ReDim Preserve allURLs(UBound(allURLs)+1)
当传递给时,数组中的第一个位置始终有一个空白元素
objReg.setMultiStringValue HKCU,IE_Main,mStrSecStartPages,allURLs
如果它不是你能得到的唯一元素
SWbemObjectEx:参数无效
我做了一些测试来证明这一点
Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002
Dim oReg
Dim strKeyPath, strValueName, arrStringValues
Dim strComputer: strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Internet Explorer\Main"
strValueName = "Default_Secondary_Page_URL"
Dim rtn
rtn = oReg.GetMultiStringValue(HKEY_LOCAL_MACHINE, strKeyPath, strValueName, arrStringValues)
Dim i
If rtn = 0 Then
If IsArray(arrStringValues) Then
For i = 0 To UBound(arrStringValues)
WScript.Echo "arrStringValues(" & i & ") = " & arrStringValues(i)
Next
Else
WScript.Echo "Not Array"
End If
Else
WScript.Echo "Failed to GetMultiStringValue - Return (" & rtn & ")"
End If
rtn = oReg.SetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath,strValueName,arrStringValues)
WScript.Echo "SetMultiStringValue - Return (" & rtn & ")"
输出:
arrstringvalue(0)=
SetMultiStringValue-返回值(0)
添加以下行以在IsArray()下创建两个空白元素
ReDim Preserve arrStringValues(UBound(arrStringValues) + 1)
输出:
arrstringvalue(0)=
ARRStringValue(1)=
test36.vbs(31,1)SWbemObjectEx:无效参数
因此setmultistringvalue()
将接受一个包含空元素的数组(如果它是数组中唯一的元素),当您尝试添加更多元素时,就会出现如上所述的错误
关于原始代码
要在开始时停止创建额外的空白元素,您可以切换到对
使用,而不是对每个
使用,这样您就可以告诉循环只调用
ReDim Preserve allURLs(UBound(allURLs)+1)
当数组的索引大于0时
For i = 0 To UBound(multiStringValues)
itemname = multiStringValues(i)
If itemname <> strFunctionIntranet1 And itemname <> strFunctionIntranet2 And itemname <> strFunctionIntranet3 And itemname <> strFunctionIntranet4 Then
'Only expand if we have more then 1 value in multiStringValues
If i > 0 Then ReDim Preserve allURLs(UBound(allURLs)+1)
allURLs(UBound(allURLs)) = itemname
End If
Next
i=0到UBound(多字符串值)的
itemname=多字符串值(i)
如果itemname strFunctionIntranet1和itemname strFunctionIntranet2和itemname strFunctionIntranet3和itemname strFunctionIntranet4,则
'仅当多字符串值中的值超过1时才展开
如果i>0,则重读保留allURLs(UBound(allURLs)+1)
allURLs(UBound(allURLs))=项目名称
如果结束
下一个
当然,您可以为每个
使用一个,但您必须使用另一个变量手动跟踪数组索引,在我看来,当您为
使用时,这似乎已经毫无意义。问题是,即使REG_MULTI_SZ值中没有任何值,您仍然会返回一个空数组,这意味着当您在数组中循环并使用
ReDim Preserve allURLs(UBound(allURLs)+1)
当传递给时,数组中的第一个位置始终有一个空白元素
objReg.setMultiStringValue HKCU,IE_Main,mStrSecStartPages,allURLs
如果它不是你能得到的唯一元素
SWbemObjectEx:参数无效
我做了一些测试来证明这一点
Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002
Dim oReg
Dim strKeyPath, strValueName, arrStringValues
Dim strComputer: strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Internet Explorer\Main"
strValueName = "Default_Secondary_Page_URL"
Dim rtn
rtn = oReg.GetMultiStringValue(HKEY_LOCAL_MACHINE, strKeyPath, strValueName, arrStringValues)
Dim i
If rtn = 0 Then
If IsArray(arrStringValues) Then
For i = 0 To UBound(arrStringValues)
WScript.Echo "arrStringValues(" & i & ") = " & arrStringValues(i)
Next
Else
WScript.Echo "Not Array"
End If
Else
WScript.Echo "Failed to GetMultiStringValue - Return (" & rtn & ")"
End If
rtn = oReg.SetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath,strValueName,arrStringValues)
WScript.Echo "SetMultiStringValue - Return (" & rtn & ")"
输出:
arrstringvalue(0)=
SetMultiStringValue-返回值(0)
添加以下行以在IsArray()下创建两个空白元素
ReDim Preserve arrStringValues(UBound(arrStringValues) + 1)
输出:
arrstringvalue(0)=
ARRStringValue(1)=
test36.vbs(31,1)SWbemObjectEx:无效参数
因此setmultistringvalue()
将接受一个包含空元素的数组(如果它是数组中唯一的元素),当您尝试添加更多元素时,就会出现如上所述的错误
关于原始代码
要在开始时停止创建额外的空白元素,您可以切换到对
使用,而不是对每个
使用,这样您就可以告诉循环只调用
ReDim Preserve allURLs(UBound(allURLs)+1)
当数组的索引大于0时
For i = 0 To UBound(multiStringValues)
itemname = multiStringValues(i)
If itemname <> strFunctionIntranet1 And itemname <> strFunctionIntranet2 And itemname <> strFunctionIntranet3 And itemname <> strFunctionIntranet4 Then
'Only expand if we have more then 1 value in multiStringValues
If i > 0 Then ReDim Preserve allURLs(UBound(allURLs)+1)
allURLs(UBound(allURLs)) = itemname
End If
Next
i=0到UBound(多字符串值)的
itemname=多字符串值(i)
如果itemname strFunctionIntranet1和itemname strFunctionIntranet2和itemname strFunctionIntranet3和itemname strFunctionIntranet4,则
'仅当多字符串值中的值超过1时才展开
如果i>0,则重读保留allURLs(UBound(allURLs)+1)
allURLs(UBound(allURLs))=项目名称
如果结束
下一个
当然,你可以为每个
使用一个,但你必须使用另一个变量手动跟踪数组索引,在我看来,当你为
使用时,这似乎是毫无意义的。愚蠢的问题,但是已经调试了allURLs
数组,以确保它传递了正确的值?一个简单的For
循环和WScript.Echo
就可以了。它可能还与使用动态数组有关,看起来很傻,但目前无法验证我的理论。您可以尝试使用数组(…,…)
表单。如果这样做有效,您可能会发现构建一个带分隔符的字符串,然后使用Split()
使用该分隔符将字符串拆分为一个数组效果更好。尽管如此,我愿意打赌allURLs
数组包含导致错误的空元素,请参阅。实际上,查看正在发生的代码,您可以ReDim(0)
然后在循环中ReDim Preserve allURLs(UBound(allURLs)+1)
而不将第一个元素设置为值。感谢反馈Lankymart。这就是为什么我有一个if条件,在数组不>=0的情况下不设置值,并且当我对