gpt4 book ai didi

vbscript - 将错误代码返回到VBScript

转载 作者:行者123 更新时间:2023-12-03 09:04:16 25 4
gpt4 key购买 nike

这是我正在尝试做的事情:

  • 获取一个VBScript以运行另一个VBScript。
  • 获取第二个VBScript,该错误将在完成时发布错误,如果成功则返回0,如果未返回原始脚本,则返回> 0,然后根据返回的错误代码在条件下工作。

  • 卸载2010年并复制Office 2013
    'Copy files from a network share to machine
    Set FSO = CreateObject("Scripting.FileSystemObject")
    WScript.Echo "Starting to uninstall Microsoft Office 2010 from the machine"

    FSO.CopyFile "\\data01\Tools\WSM\Copy_2013.vbs", "C:\temp\Copy_2013.vbs"
    FSO.CopyFile "\\data01\Tools\WSM\OffScrub10.vbs", "C:\Temp\OffScrub10.vbs"
    FSO.CopyFile "\\data01\Tools\WSM\DeleteOffice13Package.vbs", "C:\temp\DeleteOffice13Package.vbs"

    'Wait to execute rest of script where copied filed need to be in location
    WScript.Sleep 5000

    'Executes Office 2013 copy at the same time, do not wait to continue uninstalling office 2010
    Set objShell = WScript.CreateObject("WScript.Shell")
    Call objShell.Run("C:\temp\Copy_2013.vbs", 0, False)

    WScript.Sleep 3000

    'Run VBScript that uninstalls office 2010 (currently set to copy a non existent path for error capture test)
    strRemoveOffice10 = "c:\Temp\offscrub10.vbs ALL /Quiet /NoCancel"
    Call objShell.Run(strRemoveOffice10, 0, True)

    WScript.Echo Err.Number

    If Err.Number <> 0 Then WScript.Echo " Microsoft Office 2010 could not be uninstalled. Please uninstall again manually."
    If Err.Number = 0 Then WScript.Echo "Microsoft Office 2010 has uninstalled from the machine"

    Set objFileSys = Nothing

    WScript.Quit

    OffScrub10.vbs
    Dim objFileSys

    Set objFileSys = CreateObject("Scripting.FileSystemObject")
    objFileSys.GetFolder("C:\Temp\Temp1\bla").Copy "C:\WSM\Test"

    On Error Resume Next

    If Err.Number <> 0 WScript.Quit Err

    最佳答案

    要启用错误处理,您需要在可能导致错误的语句之前放置On Error Resume Next。然后,您可以返回如下状态代码:

    Set fso = CreateObject("Scripting.FileSystemObject")

    On Error Resume Next
    fso.GetFolder("C:\Temp\Temp1\bla").Copy "C:\WSM\Test"
    WScript.Quit Err.Number

    但是,由于您说过要在出现错误的情况下返回值> 0,并且 Err.Number是一个无符号整数,根据其实际值可能会解释为正值或负值,因此类似的方法可能是更好的选择:
    Set fso = CreateObject("Scripting.FileSystemObject")

    On Error Resume Next
    fso.GetFolder("C:\Temp\Temp1\bla").Copy "C:\WSM\Test"
    If Err Then WScript.Quit 1
    WScript.Quit 0 'can be omitted, because it's the default

    要在调用脚本中检查返回的值,您需要将其捕获到变量中。当像在第一个脚本中一样使用 Call语句时,仅丢弃返回值。 VBScript不会将外部命令的返回值放在 Err对象中。您可能还需要确保脚本使用 cscript.exe运行,以避免消息/ pop 窗口阻止执行。
    strRemoveOffice10 = "cscript.exe c:\Temp\offscrub10.vbs ALL /Quiet /NoCancel"
    rc = objShell.Run(strRemoveOffice10, 0, True)
    If rc = 0 Then
    'OK
    Else
    'an error occurred
    End If

    关于vbscript - 将错误代码返回到VBScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41547915/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com