Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

VBScript WScript.Shell command not working

- - - - -

  • Please log in to reply
9 replies to this topic

#1
Tripredacus

Tripredacus

    K-Mart-ian Legend

  • Super Moderator
  • 9,766 posts
  • OS:Server 2012
  • Country: Country Flag

Donator

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?
MSFN RULES | GimageX HTA for PE 3-5 | lol probloms
msfn2_zpsc37c7153.jpg


How to remove advertisement from MSFN

#2
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,812 posts
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


#3
Tripredacus

Tripredacus

    K-Mart-ian Legend

  • Super Moderator
  • 9,766 posts
  • OS:Server 2012
  • Country: Country Flag

Donator

Since I use the objShell var later in the file, should I then set objShell to equal nothing before exiting the Sub?
MSFN RULES | GimageX HTA for PE 3-5 | lol probloms
msfn2_zpsc37c7153.jpg

#4
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,812 posts
Yes.

#5
cluberti

cluberti

    Gustatus similis pullus

  • Supervisor
  • 11,252 posts
  • OS:Windows 8.1 x64
  • Country: Country Flag
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.
MCTS Windows Internals, MCITP Server 2008 EA, MCTS MDT/BDD, MCSE/MCSA Server 2003, Server 2012, Windows 8
--------------------
Please read the rules before posting!
Please consider donating to MSFN to keep it up and running!

#6
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
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



GunSmokingMan



#7
cluberti

cluberti

    Gustatus similis pullus

  • Supervisor
  • 11,252 posts
  • OS:Windows 8.1 x64
  • Country: Country Flag
Here's the MsgBox value list from MSDN.
MCTS Windows Internals, MCITP Server 2008 EA, MCTS MDT/BDD, MCSE/MCSA Server 2003, Server 2012, Windows 8
--------------------
Please read the rules before posting!
Please consider donating to MSFN to keep it up and running!

#8
Tripredacus

Tripredacus

    K-Mart-ian Legend

  • Super Moderator
  • 9,766 posts
  • OS:Server 2012
  • Country: Country Flag

Donator

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.
MSFN RULES | GimageX HTA for PE 3-5 | lol probloms
msfn2_zpsc37c7153.jpg

#9
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
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>



GunSmokingMan



#10
Fredledingue

Fredledingue

    MSFN Expert

  • Member
  • PipPipPipPipPipPip
  • 1,267 posts
  • OS:98SE
  • Country: Country Flag
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).

HTASoft.com

superchargedwindows9xig1.png
Still Using W98SE+++ ...Daily.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN