我可以将变量存储在WSF文件中以便在脚本部分中使用吗?

时间:2016-06-23 11:51:17

标签: vbscript wsh

作为替代方案...

<job>
<script language="VBScript">
sqlquery = "    some text just for demonstrating what I mean " & _
           "    some text just for demonstrating what I mean " & _
           "    some text just for demonstrating what I mean "

statement = connection.execute(sqlquery)

'more code

</script>
</job>

这样的事情可能吗? ...

<job>

<sqlquery>
some text just for demonstrating what I mean
some text just for demonstrating what I mean
some text just for demonstrating what I mean
</sqlquery>

<script language="VBScript">

statement = connection.execute(sqlquery)

' more code


</script>
</job>

换句话说,是否可以在WSF文件的XML部分中存储变量(在本例中为大型多行字符串),然后在脚本中使用该变量?

我试图避免在vbscript代码中以这种丑陋的方式存储大型多行字符串。

我知道我可以将字符串存储在一个单独的文件中并将其读入(我会这样做)但是我要问这个问题以确定这是否可行。

1 个答案:

答案 0 :(得分:1)

我偶然必须阅读ActivePerl用户指南主题&#34; Windows脚本宿主&#34;并找到<resource>/getResource。在microsoft.com上,我找到了this。我修改了样本以显示CDATA的重要性:

<package>
<job id="VBS">
<resource id="errNonNumeric">
<![CDATA[
  Error: A non-numeric value was entered where a number was expected.
         And value <> nice!
         So we need CDATA
]]>
</resource>
<script language="VBScript">

Function getRandomNumber(upperBound)
   If IsNumeric(upperBound) Then
      getRandomNumber = CInt(upperBound * Rnd + 1)
   Else
      WScript.Echo qq(getResource("errNonNumeric"))
      WScript.Quit -1
   End If
End Function

Function qq(s):qq=""""&s&"""":End Function

NewValue = getRandomNumber("Bad Value")

</script>
</job>
</package>

输出:

cscript 37990815-4.wsf
"

  Error: A non-numeric value was entered where a number was expected.
         And value <> nice!
         So we need CDATA

"

有些工作似乎仍然需要空白。

旧版本;继续表明rtfm击败了你自己的&#39;

如果你愿意

  1. 通过书籍<#li;
  2. 撰写您的XML
  3. 使用MSXML2.DomDocument向每个.WSF添加一个函数
  4. 做一些空白的额外工作
  5. 你可以。

    演示:

    <?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>
    <package>
     <job id="main">
      <?job error="true" debug="false" ?>
      <script language="VBScript">
       <![CDATA[
    ' ############################################################################
    
    Function getXmlData(n)
      Dim oXDoc  : Set oXDoc = CreateObject("MSXML2.DomDocument.6.0")
      oXDoc.setProperty "SelectionLanguage", "XPath"
      oXDoc.async = False
      oXDoc.load WScript.ScriptFullName
      getXmlData = "shit hits fan"
      If 0 = oXDoc.ParseError Then
         Dim ndX : Set ndX = oXDoc.selectSingleNode("/package/job[@id='main']/data/" & n)
         getXmlData = ndX.text
      End If
    End Function
    Function qq(s):qq=""""&s&"""":End Function
    
    WScript.Echo qq(getXmlData("s"))
    
    
    ' ############################################################################
       ]]>
      </script>
      <data>
       <s>
        pi
        pa
        po
       </s>
      </data>
     </job>
     <job id="other">
      <data><s>wtf</s></data>
     </job>
    </package>
    

    输出:

    cscript 37990815-2.wsf
    "pi
        pa
        po"