检查变量是否存在

Check if variable does or does not exist

我正在使用支持 VBScript 条件的 XML 文件,然后将其与 SCCM 任务序列变量一起使用。从我所做的所有互联网搜索来看,我并不完全清楚我应该使用什么来检测变量是否存在。比如我有两个变量,一个叫%DriveIndex1%,另一个叫%DriveIndex2%。这些变量仅在检测到某些磁盘驱动器时才存在。那么代码如何检测这些变量是否存在呢?这是一个片段:

<CheckboxInput Condition='%DriveIndex1% OR %DriveIndex2% <> ""' Variable="FormatAll" Question="Also partition and format the other drive(s)?" CheckedValue="True" UncheckedValue="False" Default="True" />

我认为条件不对,我不知道是否应该改用 IsEmptyIsObjectIsNull 这样的函数。

我通常会结合使用 isEmpty 和 isNull 来说明两者:

if isnull(testvalue) or isempty(testvalue) then
    Response.Write "true"
else
    Response.Write "false"
end if

这可能是最简单的解决方案,尽管我没有使用 sccm 的经验...

%DriveIndex1% OR %DriveIndex2% <> ""不会评价你想要的。此语法将评估两个条件,一个 %DriveIndex1% 和另一个 %DriveIndex2% <> ""。你需要像

这样的东西

%DriveIndex1% <> "" OR %DriveIndex2% <> ""

但是

在 vbscript 中 "" 不等于 Empty,未声明的变量是 Empty,所以如果你的 TS 变量不存在你会想要

Not IsEmpty(%DriveIndex1%) And Not IsEmpty(%DriveIndex2%)

您还可以像这样包括 Null"" 的检查

Not IsEmpty(%DriveIndex1%) And Not IsEmpty(%DriveIndex2%) and Not IsNull(%DriveIndex1%) And Not IsNull(%DriveIndex2%) And %DriveIndex1% <> "" And %DriveIndex2% <> ""

最后,我不熟悉你如何检查 xml 中的 vbscript 条件,但据我所知,TS 变量是通过类似于此

Microsoft.SMS.TSEnvironment 对象访问的]
Set env = CreateObject("Microsoft.SMS.TSEnvironment")
env("MyVariable") = "value"
If env("MyOtherVariable") Then etc...

在这种情况下,您的条件是

Not IsEmpty(env("DriveIndex1")) And Not IsEmpty(env("DriveIndex2"))

这是假设从 xml 文件调用条件的任何东西已经创建了 env 对象。