Discussion Using VBScript to run a PowerShell script

  • 6 noiembrie 2009 20:42
    Proprietar
     
      Are cod
      Summary
     
    If you want to run a PowerShell script by just double-clicking it, you can do this calling the PowerShell script from a VBScript.
    The VBScript code below shows an example for this.
    To use the VBScript,
    • save the script in the folder where your PowerShell script is
    • assign the name of your PowerShell script to the VBScript (with a .vbs extension)

     

    ImportantImportant
    If you want to use a VBScript to run a PowerShell script, you need to enable this by setting the "executionpolicy" parameter on your computer. 
    To do this, start PowerShell, and then type "set-executionpolicy unrestricted".
    For more details, see Using the Set-ExecutionPolicy Cmdlet.

     

    Option Explicit
    Dim oShell, appCmd
    Set oShell  = CreateObject("WScript.Shell")
    appCmd      = "powershell -noexit &'" & Replace(WScript.ScriptFullName, ".vbs", ".ps1") & "'"
    oShell.Run appCmd, 4, false
    

     

      Go to the FIM ScriptBox

    Markus Vilcinskas, Knowledge Engineer, Microsoft Corporation

Toate mesajele

  • 12 noiembrie 2009 18:24
     
     
    Thank you Markus for posting this.  However, I am having a bit of trouble getting this to work on my Server 2003 R2 Server.  I have installed PowerShell 1.0 on the server and I am able to get my .ps1 file to work when I do a RUN from the Command line.

    But when I run this:
    Option Explicit
    Dim oShell, appCmd
    Set oShell  = CreateObject("WScript.Shell")
    appCmd      = "powershell -noexit &'" & Replace(WScript.DeleteFiles, ".vbs", ".ps1") & "'"
    oShell.Run appCmd, 4, false

    I get this error:
    Object doesn't support this property or method: 'WScript.DeleteFiles'
    and the error points to the line that starts appCmd ....

    I get this error when I have the VB script written as shown above or when I put in the full name of the PS1 script, DeleteFiles.ps1 
     
    Both the VB script and the ps1 script are in the same folder of the Server 2003 R2 server. The VB script is called DeleteFiles.vbs

    Thanks again for your work on this.  David Grand
  • 13 noiembrie 2009 01:30
    Proprietar
     
     
    David,

    if I read this correctly, the problem is simple.
    So, you do have a PowerShell script that does what it is supposed to do.
    This script has a name - for exampe, "Banana.ps1".

    What you need to do, is to take the VBScript code above as it is and save it in the same folder where "Banana.ps1" is as "Banana.vbs".

    In other words, there is no need to touch the VBScript code above.
    Just make sure that yoour PowerShell script and the VBScript have the same name.

    "appCmd      = "powershell -noexit &'" & Replace(WScript.ScriptFullName, ".vbs", ".ps1") & "'"
    "

    The word "Replace" in the script code is not an instruction for you - it is a script command.
    In other words, don't change the script code.


    Makes sense?

    Cheers,
    Markus


    Markus Vilcinskas, Knowledge Engineer, Microsoft Corporation
  • 13 noiembrie 2009 17:44
     
     
    Markus,

    Your reply makes a lot of sense.  I am just so used to having to tweak VB scripts I get from the Internet.  Sorry about that.  If I leave the script alone as you have presented in the first post of this thread, the script works perfectly.    I had already done the step where both the .ps1 and .vbs had the same name in the same location.

    Thank you for your great script.

    David Grand