Q. How can I use a script to display a Yes/No confirmation box?

A. You can use the Msgbox function to display a Yes/No dialog box, as the following example shows:

Ret=Msgbox("Are you sure you want to reboot?",VBYesNo,"Reboot Confirm")

Ret is variable that contains the Msgbox return code, which varies depending on the button the user pressed (Yes returns a value of 6; No returns a value of 7). The parentheses contain the question text; the VBYesNo constant, which tells VBScript to display Yes and No buttons; and the dialog box title. The figure at Figure shows the Yes/No box that the code example produces.

The following code shows how to use the Msgbox function in a script:

Ret=Msgbox_
("Are you sure you want to reboot?",VBYesNo,"Reboot confirm")
If Ret=6 then
  Set OpSysSet =
GetObject("winmgmts:\{(Shutdown)\}//./root/cimv2").ExecQuery_
("select * from Win32_OperatingSystem where Primary=true")

  for each OpSys in OpSysSet
    OpSys.Reboot()
  next
end if

(The Ret=Msgbox and GetObject lines each wrap to two lines here because of space constraints.) You can download the Msgbox code example at Code.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish