• Announcements

    • xper

      MSFN Sponsorship and AdBlockers!   07/10/2016

      Dear members, MSFN is made available via subscriptions, donations and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, become a site sponsor and ads will be disabled automatically and by subscribing you get other sponsor benefits.
Sign in to follow this  
Followers 0
Tripredacus

VBScript WScript.Shell command not working

10 posts in this topic

I'm experiencing a problem calling a function with VBScript. This method works:

   	Set objShell = CreateObject("WScript.Shell")
ObjShell.run "cmd /c mbr 0 0 /uh",0,True

However, this does not work:

  Sub confirmation2
Dim A1 :A1 = MsgBox("Recovery Complete, Would you like to Restart now?")
If A1 = True Then CreateObject("WScript.Shell").run("cmd /c wpeutil Reboot"),0,True
End Sub

The MsgBox opens properly, but the command is not run. If I chose to use single quote, as in this:

  Sub confirmation2
Dim A1 :A1 = MsgBox("Recovery Complete, Would you like to Restart now?")
If A1 = True Then CreateObject("WScript.Shell").run('cmd /c wpeutil Reboot'),0,True
End Sub

It breaks the HTA completely. What is the correct syntax to make a MsgBox run something from ComSpec?

0

Share this post


Link to post
Share on other sites

I would do it this way:

Sub confirmation2
Dim A1
A1 = MsgBox("Recovery Complete, Would you like to Restart now?",4)
If A1 = 6 Then
Set objShell = CreateObject("WScript.Shell")
ObjShell.run("cmd /c wpeutil Reboot"),0,True
End If
End Sub

0

Share this post


Link to post
Share on other sites

Since I use the objShell var later in the file, should I then set objShell to equal nothing before exiting the Sub?

0

Share this post


Link to post
Share on other sites

Until your script crashes because you've got GC running at the same time you go to use a variable. If it's going to be solely enclosed within a sub or function, set it to nothing when you're done to force GC to clean it up (and only DIM or var it within the sub as well. If you're using it inside and out, don't clean it up (rule of thumb). Might be worth putting a lot of these into a helper class though, if you use certain functions a lot. Creating and destroying objects are intensive (relatively speaking) operations that would probably be best handled in the creation of a class at script start, rather than doing it JIT.

0

Share this post


Link to post
Share on other sites

Try this code, adjust to suite your needs.


Dim objShell :Set objShell = CreateObject("WScript.Shell")
Sub confirmation2
If MsgBox("Would You Like To Do Some Thing?",4132,"Confirmation") = 6 Then objShell.Run("Exe /Switches),0,true
End Sub

0

Share this post


Link to post
Share on other sites

Seeing how my regular restart button works, I decided to compare its code to the one that is not working. It is interesting because I am doing something different. Since this project is over a year old, I am not exactly sure why it is different. Here is the code for the main HTA:

Sub RunRestart
Set objShell = CreateObject("WScript.Shell")
objShell.Run "fscommand\restart.exe"
On Error Resume Next
Set objShell = Nothing
End Sub

So you see, the one that works calls an EXE file... It is an AutoIT program, here is the code:

Switch MsgBox(1, "Restart", "You have chosen to restart the computer. Click OK to continue.")
Case 1;OK pressed
RunWait (@ComSpec & " /c c:\mbr 0 0 /h")
RunWait (@ComSpec & " /c x:\windows\system32\wpeutil reboot")
Case 2;CANCEL pressed
Exit()
EndSwitch

Possible reasons the previous posted code did not work:

1. It is being run off a second HTA?

2. I did not use the full path like the EXE does?

I have to wait until the XPe image gets its final build before I can try calling the EXE instead of wpeutil to restart the PC. I am going to have to rebuild the EXE as well to run the MBR.EXE from the X: instead of C: because it *should* still be hidden at that point.

0

Share this post


Link to post
Share on other sites

Im confused are you opening another HTA that had the reboot or is it a button function not working?

If you are trying to have a custom dialog for rebooting perhaps try this.

Save As DemoDialog.hta


<TITLE> « Custom Dialog »</TITLE>
<HTA:APPLICATION ID='Demo_Dialog'
Scroll='No'
SCROLLFLAT ='No'
SingleInstance='Yes'
SysMenu='Yes'
MaximizeButton='No'
MinimizeButton='Yes'
Border='Thin'
BORDERSTYLE ='complex'
INNERBORDER ='Yes'
Caption='Yes'
WindowState='Normal'
APPLICATIONNAME='DemoDialog'
Icon='%SystemRoot%\explorer.exe'>
<STYLE Type='text/css'>
Body
{
Font-Size:12.25pt;
Font-Weight:Bold;
Font-Family:Arial,Tahoma,Comic Sans MS,Segoe Ui;
Color:Black;
BackGround-Color:Transparent;
Filter:progid:DXImageTransform.Microsoft.Gradient
(StartColorStr='#ece6e0',EndColorStr='#c0bab4');
Margin-Top:3;
Margin-Bottom:3;
Margin-Left:4;
Margin-Right:4;
Padding-Top:2;
Padding-Bottom:2;
Padding-Left:4;
Padding-Right:4;
Text-Align:Center;
Vertical-Align:Top;
Border-Top:0px Transparent;
Border-Bottom:0px Transparent;
Border-Left:0px Transparent;
Border-Right:0px Transparent;
}
BUTTON
{
Cursor:Hand;
Width:77pt;
Height:13pt;
Font-Size:8.25pt;
Font-Weight:Bold;
Font-Family:Arial,Tahoma,Comic Sans MS,Segoe Ui;
Color:#001141;
Text-Align:Center;
Vertical-Align:Middle;
Filter:progid:DXImageTransform.Microsoft.Gradient
(StartColorStr='AliceBlue',endColorStr='LightSlateGray');
Border-Top:0px Transparent;
Border-Bottom:0px Transparent;
Border-Left:0px Transparent;
Border-Right:0px Transparent;
Padding-Top:0;
Padding-Bottom:2;
Padding-Left:0;
Padding-Right:0;
Margin-Top:1;
Margin-Bottom:1;
Margin-Left:1;
Margin-Right:1;
BackGround-Color:Transparent;
}
.M1
{
Font-Size:8.25pt;
Font-Weight:Bold;
Font-Family:Arial,Tahoma,Comic Sans MS,Segoe Ui;
Color:#001141;
Text-Align:Center;
Vertical-Align:Center;
Filter:progid:DXImageTransform.Microsoft.Gradient
(StartColorStr='#fdf7e1',EndColorStr='#b0a9a3');
Padding-Top:5;
Padding-Bottom:5;
Padding-Left:5;
Padding-Right:5;
Border-Top:1px solid;
Border-Bottom:1px solid;
Border-Left:1px solid;
Border-Right:1px solid;
}
</STYLE>
<script LANGUAGE='JScript'>
window.resizeTo (425,227)
window.moveTo(screen.availWidth / 2 - (425/2),screen.availHeight / 2 - (227/2))
// Show The Restart Dialog
function ShowMenu01()
{
if(Dialog01.style.visibility=="hidden")
{
Dialog01.style.visibility="";
}
else
{
Dialog01.style.visibility="hidden";
}}
// Restart Computer
function RebootComp()
{
alert('Code Here');
Dialog01.style.visibility="hidden";
}
</SCRIPT>
<BODY Scroll='No'>
<Table Width='100%' Align='Left' Style='Padding:5pt;'>
<BUTTON ID='Btn01' OnClick='ShowMenu01()'>Restart</BUTTON>
</TABLE>
<!-- Hidden Dialog Until Restart Button Pressed -->
<DIV ID='Dialog01' Class='M1'
Style='visibility:hidden;position:absolute;Top:62;Left:125;Padding-Bottom:7px;Width:201pt;Height:48pt;'>
<DIV Style='position:absolute;Top:13;Left:18;'>Press Confirm To Reboot the Computer</DIV>
<BUTTON ID='Btn02' OnClick='RebootComp()' Style='position:absolute;Top:36;Left:28;'>Confirm</BUTTON>
<BUTTON ID='Btn03' OnClick='ShowMenu01()' Style='position:absolute;Top:36;Left:133;'>Cancel</BUTTON>
</Div>
</BODY>

0

Share this post


Link to post
Share on other sites

In HTA

Dim objShell :Set objShell = CreateObject("WScript.Shell")

should be replaced by

Public objShell
Set objShell = CreateObject("WScript.Shell")

... and be placed before any function or sub.

Like this you can reuse the objShell object in every sub and function and you don't need to nullify it everytime.

The response returned from a MsgBox cannot be a boolean (True or False). It must be a number or a vb word (VbOk, VbCancel, VbYes, VbNo etc).

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.