UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
2 Pages V  1 2 >  (Go to first unread post)
   Reply to this topicStart new topic
> Run Powershell From Vbscript    
 
   
EssKayKay
post Feb 23 2020, 12:18 PM
Post#1



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


Hello,

I am no programmer so please bear with me.

I have a small vbscript file that assists in running a backup program with a few message boxes. I have it working fine. However I have a question on running a Powershell routine from inside the VBS file. Currently I call a couple of batch files (i.e., minimize.bat) to run my PS code. I was wondering if there is a way to run the PS routine directly from the VBS file. Any suggestions would be greatly appreciated. Thank you for viewing.


Here’s a snipit of the VBS code that runs Minimize.bat

Set objShell = CreateObject("Wscript.Shell")
runCmd = "c:\tools\back4sure\Minimize.bat"
WshShell.Run "cmd /c " & runCmd, 0, True
Set objFso = Nothing

=============================

Here is my PS code inside Minimize.bat

powershell -command "& { $x = New-Object -ComObject Shell.Application; $x.minimizeall() }"
Go to the top of the page
 
cheekybuddha
post Feb 23 2020, 12:31 PM
Post#2


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


Hi SKK,

Have you tried just using Shell() to run your Minimize.bat ?
CODE
  Dim lRet As Long, strBatFile As String

  strBatFile = "c:\tools\back4sure\Minimize.bat"
  lRet = Shell(strBatFile)


You can probably just run the powershell command firectly in the same way:
CODE
  Dim lRet As Long, strShell As String

  strShell = "powershell -command " & Chr(34) & " & { $x = New-Object -ComObject Shell.Application; $x.minimizeall() }" & Chr(34)
  lRet = Shell(strShell)


hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
EssKayKay
post Feb 23 2020, 01:33 PM
Post#3



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


Thanks CheekyBuddha for our response. However, I'm still having an issue.
I tried both strBatFile and strShell; both bombed on the "A" character of As Long... “Dim lRet As Long, strShell As String”

Error popup message reads:
Error: Expected end of statement
Code: 800A0401

Thanks again,
SKK
This post has been edited by EssKayKay: Feb 23 2020, 02:02 PM
Go to the top of the page
 
cheekybuddha
post Feb 23 2020, 02:23 PM
Post#4


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


Please post your whole code

--------------------


Regards,

David Marten
Go to the top of the page
 
cheekybuddha
post Feb 23 2020, 02:27 PM
Post#5


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


Oops, missed that this is a vbscript. blush.gif

In vbscript there are only Variants.

Remove the 'As Long' and 'As String' and see how it goes then.

--------------------


Regards,

David Marten
Go to the top of the page
 
EssKayKay
post Feb 23 2020, 02:49 PM
Post#6



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


OKAY here’s my code. Now I realize this is very sloppy and could be cleaned up considerably but it works. Like I said, obviously I’m no programmer.
I tried your suggestion of removing As Long and As String but did not make a difference but I may have removed it incorrectly.
Thanks again.



==== CODE ====

' Program to Backup all files
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Dim strPath, objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

' runCmd = "c:\tools\back4sure\Minimize.bat"
' WshShell.Run "cmd /c " & runCmd, 0, True

'==========================================================
Dim lRet As Long, strShell As String
strShell = "powershell -command " & Chr(34) & " & { $x = New-Object -ComObject Shell.Application; $x.minimizeall() }" & Chr(34)
lRet = Shell(strShell)
'==========================================================

X1=msgbox(" All open programs will be" & vbNewLine & " closed prior to backup. . ." & vbNewLine & "" & vbNewLine & " Do you wish to continue?",4, " ~~ BACKUP ~~")
if X1=6 then
strDrive = "K:\"
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.DriveExists(strDrive) then
Set WshShell = WScript.CreateObject("WScript.Shell")

runCmd = "c:\tools\back4sure\CloseALL.bat"
WshShell.Run "cmd /c " & runCmd, 0, True

runCmd = "c:\tools\back4sure\Run-b4s_SKK.bat"
WshShell.Run "cmd /c " & runCmd, 0, True

msgbox " BACKUP COMPLETE" & vbNewLine & " ________________________________"
Else
X2=Msgbox (" Missing Backup Drive " & vbNewLine & " ~~~~~~~~~~~~~~~~~~~" & vbNewLine & "" & vbNewLine & " Backup folder not available." & vbNewLine & " Verify external drive is connected" & vbNewLine & " and Rerun BACKUP program . . . ." & vbNewLine & "",0, " - OOPS -")
if X2=4 then
MsgBox " Backup program will close" & vbNewLine & " Connect external drive and rerun Backup. . . ."
End If
End If
Set objFso = Nothing
wscript.quit

Else
IF X1=7 then
runCmd = "c:\tools\back4sure\UndoMinimize.bat"
WshShell.Run "cmd /c " & runCmd, 0, True
wscript.quit
End If
End If
Go to the top of the page
 
cheekybuddha
post Feb 23 2020, 03:43 PM
Post#7


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


CODE
' Program to Backup all files
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Dim strPath, objFso, strShell As String

Set objFso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

strShell = "powershell -command " & Chr(34) & " & { $x = New-Object -ComObject Shell.Application; $x.minimizeall() }" & Chr(34)
WshShell.Run strShell

X1=msgbox(" All open programs will be" & vbNewLine & " closed prior to backup. . ." & vbNewLine & "" & vbNewLine & " Do you wish to continue?",4, " ~~ BACKUP ~~")
if X1=6 then
  strDrive = "K:\"
  Set objFso = CreateObject("Scripting.FileSystemObject")
  If objFso.DriveExists(strDrive) then
    Set WshShell = WScript.CreateObject("WScript.Shell")

    runCmd = "c:\tools\back4sure\CloseALL.bat"
    WshShell.Run "cmd /c " & runCmd, 0, True

    runCmd = "c:\tools\back4sure\Run-b4s_SKK.bat"
    WshShell.Run "cmd /c " & runCmd, 0, True

    msgbox " BACKUP COMPLETE" & vbNewLine & " ________________________________"
  Else
    X2=Msgbox (" Missing Backup Drive " & vbNewLine & " ~~~~~~~~~~~~~~~~~~~" & vbNewLine & "" & vbNewLine & " Backup folder not available." & vbNewLine & " Verify external drive is connected" & vbNewLine & " and Rerun BACKUP program . . . ." & vbNewLine & "",0, " - OOPS -")
    if X2=4 then
      MsgBox " Backup program will close" & vbNewLine & " Connect external drive and rerun Backup. . . ."
   End If
  End If
  Set objFso = Nothing
  wscript.quit

Else
  IF X1=7 then
    runCmd = "c:\tools\back4sure\UndoMinimize.bat"
    WshShell.Run "cmd /c " & runCmd, 0, True
    wscript.quit
  End If
End If


Try that; and, if it works, you can just bring in all the powershell code from all your bat files.

hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
EssKayKay
post Feb 23 2020, 04:31 PM
Post#8



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


Not quite there but very close. It bombed on the "A" of As String in line -
Dim strPath, objFso, strShell As String

I removed the “As String” and it appeared to run fine. However, I assume that is a bad idea. What do you think?


Go to the top of the page
 
cheekybuddha
post Feb 23 2020, 04:32 PM
Post#9


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


No, very good idea!

I forgot to remove it.

--------------------


Regards,

David Marten
Go to the top of the page
 
EssKayKay
post Feb 23 2020, 05:12 PM
Post#10



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


Thank very much for your time and consideration. I will be playing with this a bit more. Hopefully all is well.

Again my sincere thanks.....
Go to the top of the page
 
cheekybuddha
post Feb 23 2020, 06:31 PM
Post#11


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


Are all your other bat files here Powershell scripts?

--------------------


Regards,

David Marten
Go to the top of the page
 
EssKayKay
post Feb 23 2020, 07:03 PM
Post#12



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


Yes. I will look at those based on what you showed me. I am seeing one other thing I want to work on yet so you may not have heard the last of me (ha-ha). I will keep you posted. Again, thanks for your support.
Go to the top of the page
 
cheekybuddha
post Feb 23 2020, 07:57 PM
Post#13


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


yw.gif

--------------------


Regards,

David Marten
Go to the top of the page
 
EssKayKay
post Feb 23 2020, 09:17 PM
Post#14



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


Sorry to keep bugging you but we (read you) are so close...

I’ve got two of the three batch files working using the strShell and wshShell.run commands. However, I have one batchfile that’s not working (Run-b4s_SKK.bat).

This is the code for Run-b4s_SKK.bat
------------------------------------------------------------------------------------------------

"c:\tools\back4sure\back4sure.exe" -b -c -q -x "c:\tools\back4sure\Backup_SKK.b4j"
del “z:\backup\Dell_i7\*.log”

-------------------------------------------------------------------------------------------------

If this doesn't work I can always keep calling the batch file, it's just that we are so close.

again, any suggestions?
Thanks,
SKK
This post has been edited by EssKayKay: Feb 23 2020, 09:29 PM
Go to the top of the page
 
EssKayKay
post Feb 23 2020, 10:23 PM
Post#15



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


- DELETED -
OOPS - thought I had it but not....


This post has been edited by EssKayKay: Feb 23 2020, 10:45 PM
Go to the top of the page
 
cheekybuddha
post Feb 24 2020, 07:35 AM
Post#16


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


Hi,

I think we're probably in different timezones.

I did some tidying up to your vbs file to try and show how to strengthen it a bit.

I added Option Explicit to force you to declare your variables.

I removed unnecessary Set statements, and added constants to declare your magic number values.

See if it makes more sense:
CODE
' Program to Backup all files
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

' Require variable declaration
' This will help you when debugging
Option Explicit

Const vbOKOnly = 0, _
      vbYesNo = 4, _
      vbYes   = 6,
      vbNo    = 7,
      DQ      = """"      ' Double Quote

Dim objFso, WshShell      ' Object variables
Dim strShell              ' shell command variable
Dim strMsg, strTitle      ' MsgBox variables
Dim strDrive

Set objFso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

strShell = "powershell -command " & DQ & " & { $x = New-Object -ComObject Shell.Application; $x.minimizeall() }" & DQ
WshShell.Run strShell

strMsg = " All open programs will be" & vbNewLine & " closed prior to backup. . ." & vbNewLine & "" & vbNewLine & " Do you wish to continue?"
strTitle = " ~~ BACKUP ~~"
Select Case MsgBox(strMsg, vbYesNo, strTitle)
Case vbYes
  strDrive = "K:\"
  If objFso.DriveExists(strDrive) Then
    strShell = "*** COMMAND FROM c:\tools\back4sure\CloseALL.bat ***"
    WshShell.Run strShell
    strShell = DQ & "c:\tools\back4sure\back4sure.exe" & DQ & " -b -c -q -x " & DQ & "c:\tools\back4sure\Backup_SKK.b4j" & DQ
    WshShell.Run strShell
  ' Use FSO to delete log files
    objFso.DeleteFile "Z:\backup\Dell_i7\*.log"
  Else
    strMsg = " Missing Backup Drive " & vbNewLine & " ~~~~~~~~~~~~~~~~~~~" & vbNewLine & "" & vbNewLine & _
             " Backup folder not available." & vbNewLine & " Verify external drive is connected" & vbNewLine & _
             " and Rerun BACKUP program . . . ."
    strTitle = " - OOPS -"
    If MsgBox(strMsg, vbOKOnly, strTitle) = vbRetry Then
'     Not sure how you ever get here because the only MsgBox option
'     offered to the user is 'OK' - so it will never equal vbRetry
      strMsg = " Backup program will close" & vbNewLine & " Connect external drive and rerun Backup. . . ."
      MsgBox strMsg
    End If
  End If
Case vbNo
  strShell = "*** COMMAND FROM c:\tools\back4sure\UndoMinimize.bat ***"
  WshShell.Run strShell
End Select
Set objFSO = Nothing
Set WshShell = Nothing


The problem in Run-b4s_SKK.bat is that it runs two commands, you you would want to run one after the other. Since the second command is a delete files operation, you may as well leverage your FileSystemObject to carry it out.

Obviously, I haven't filled in the commands from the bat files which you got to work.

Shout back with any questions.

hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
EssKayKay
post Feb 24 2020, 09:39 AM
Post#17



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


David,

I’m receiving an “Expected Identifier” error message on Line 10 Char 19 which I believe is after “vbYes = 6,”.
Sorry for being such a pest. If this is getting too annoying I totally understand.

Steve K.

Go to the top of the page
 
cheekybuddha
post Feb 24 2020, 09:48 AM
Post#18


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


Apologies, I missed out a [couple of] continuation character[s]:

Substitute with:
CODE
Const vbOKOnly = 0, _
      vbYesNo = 4, _
      vbYes   = 6, _
      vbNo    = 7, _
      DQ      = """"      ' Double Quote


There may well be other errors - I just used a text editor and haven't run the script! blush.gif

d

--------------------


Regards,

David Marten
Go to the top of the page
 
EssKayKay
post Feb 24 2020, 10:21 AM
Post#19



Posts: 523
Joined: 5-April 03
From: NE Wisconsin


Still problem - Line 8, Char 1
Error: Name redfined: ‘vbOKOnly’

If you don't mind I will keep trying all your suggestions. Like I said, I don't want to be a pest.
Go to the top of the page
 
cheekybuddha
post Feb 24 2020, 11:37 AM
Post#20


UtterAccess Moderator
Posts: 12,621
Joined: 6-December 03
From: Telegraph Hill


OK, it probably knows the values of the constants already - I thought it might not since you were using literal numeric values.

I think all you need is:
CODE
Const DQ      = """"      ' Double Quote


Let us know what new errors pop up

--------------------


Regards,

David Marten
Go to the top of the page
 
2 Pages V  1 2 >


Custom Search


RSSSearch   Top   Lo-Fi    5th April 2020 - 04:35 PM