6

Hey i want to run from an vbs script a powershell commando. Something like start powershell.exe and enter a specific command like Restart-Service. I thought something similar to this could work:

strCommand = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command Restart-Service [service name]"

    Set WshShell = WScript.CreateObject("WScript.Shell") 
    Set objExec = WshShell.Exec(strCommand)

Has someone an idea how can i manage this?

Alesfatalis
  • 769
  • 2
  • 13
  • 32
  • 1
    This will help: http://ss64.com/vb/run.html – Andy Arismendi Jul 12 '12 at 09:13
  • 1
    i have set it up like this `Dim objShell Set objShell = WScript.CreateObject ("WScript.shell") objShell.run "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe & Restart-Service BranchCache" Set objShell = Nothing` But is doesn't work. – Alesfatalis Jul 12 '12 at 09:28
  • 1
    Look at PowerShell.exe's command line parameters. Use the one to tell PowerShell to run a command... – Andy Arismendi Jul 12 '12 at 09:42
  • 1
    Ok i have looke now for the parameter and setuped it like this: `Dim objShell Set objShell = WScript.CreateObject ("WScript.shell") objShell.run "PowerShell -Command {Start-Service PeerDistSvc}"` When i run the vbs it runs through and the cmd window pops up and close and that was it, but the service didn't start what do i wrong? I am administrator on the computer. – Alesfatalis Jul 12 '12 at 10:57

6 Answers6

5

1) store your powershell command as powershell script.
2) Use the vbs to run the powershell

  Set objShell = CreateObject("Wscript.Shell")
 objShell.Run("powershell.exe -noexit c:\scripts\test.ps1")
HariHaran
  • 207
  • 1
  • 3
  • This is the last thing i try when i can't get it working i use youre method. – Alesfatalis Jul 12 '12 at 10:59
  • @HariHaran Hello do you know why this might work when a call via `objShell.Run "powershell -file ""c:\scripts\test.ps1""", 0, true` doesn't? – Bassie Mar 16 '16 at 17:23
  • Is there a reason why `C:\Users\lpeder6\Desktop\PowerShell Scripts\GUI Files\Basic GUI Script - Combo Box.ps1` opens in Windows PowerShell ISE, but not when I call it from a VBScript (example: `objShell.Run ("powershell.exe -noexit C:\Users\lpeder6\Desktop\PowerShell Scripts\GUI Files\Basic GUI Script - Combo Box.ps1")`)? – Lou Oct 27 '21 at 16:12
3

Try this:

powershell -command '& {command to run}'

/G

Gisli
  • 734
  • 2
  • 11
  • 34
  • I think it works ,but i noticed i need to be administrator to start/stop services how can i run this part of the vbs as a different user? – Alesfatalis Jul 12 '12 at 11:22
  • I'm not quite sure. You could try something like this: http://stackoverflow.com/questions/1566969/showing-the-uac-prompt-in-powershell-if-the-action-requires-elevation – Gisli Jul 12 '12 at 13:46
  • and I think there is also a switch (-verb runas). Try googling it – Gisli Jul 12 '12 at 13:52
1

You can use the shortest version.

CreateObject("Wscript.Shell").Run ("powershell.exe -noexit $c= 'lola'; write-host $c -ForegroundColor green")
boga khan
  • 11
  • 2
0

Try this:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit .\c:\scripts\test.ps1")

Or keep the files in the same folder where the PS exe is and then

Objshell.Run("powershell.exe -noexit .\test.ps1")
Brian J
  • 694
  • 1
  • 21
  • 34
abhinov
  • 125
  • 1
  • 3
  • 12
0

While researching how to use this to run PowerShell scripts using VBScript, I was running into the issue shown below:

enter image description here

I've since then figured out why it was not working. PowerShell cannot read spaces in the location of your script. As you can see in the image above, the error stops at C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays when the full path is C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays - Clear Method.ps1.

After removing all spaces and extra characters from my path names, running PowerShell scripts with VBScript works with the code below:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit -command C:\Users\lpeder6\Desktop\PowerShellScripts\GUIFiles\BasicGUIScript-ComboBox.ps1"),1,True

What I get, when I run it, is this:

enter image description here

I hope this helps someone as this is information I really could have used. :)

Lou
  • 389
  • 3
  • 20
  • 38
  • 1
    No need to change the path, what if you can't change the path and it has spaces? See [Spaces cause split in path with PowerShell](https://stackoverflow.com/a/18537263) – user692942 Nov 24 '21 at 10:18
  • 1
    @user692942 That worked! I've never heard of anything like that and it has caused a lot of issues during development of other solutions we've been looking into. This will really help us move forward using PowerShell. – Lou Dec 03 '21 at 13:54
0

Thank you , it worked for me after I removed the .\ from

objShell.Run("powershell.exe -noexit .\C:\Users\503327978\Downloads\Scripts\test\ONLY_popups_messages.ps1")

to

objShell.Run("powershell.exe -noexit C:\Users\503327978\Downloads\Scripts\test\ONLY_popups_messages.ps1")

Therefore my entire script is as followed:

Set objShell = CreateObject("Wscript.Shell") objShell.Run("powershell.exe -noexit C:\Users\503327978\Downloads\Scripts\test\ONLY_popups_messages.ps1")

worked perfectly, plus is shorter version. :-)