Hi can someone pls help me to run powershell from vbscript as a administrator
VB Script
MyPath = "c:\temp\folder1"
Dim objShell
Set objShell = CreateObject("Wscript.Shell")
objShell.run("powershell.exe -noexit -file C:\temp\power.ps1 " & MyPath)
PowerShell power.ps1
C:\temp\psfile.exe $args[0] -c
In VBScript Set is used for objects; so change
set input ="C:\temp\folder1\"
to
input = "C:\temp\folder1\"
Related
I once found a script to easily change my Wallpaper if I use multiple ones.
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys("^ ")
WshShell.SendKeys("+{F10}")
WshShell.SendKeys("n")
WshShell.SendKeys("{ENTER}")
However I need a separate link that calls the script.
Now I wonder if I can pass these multiple lines of code as parameter to powershell.exe directly.
I did read that I can allign multiple lines using ; and tried building the one-liner, however it doesn't run...or rather it does run, there is no feedback, nothing changes. If I paste the command line into a running powershell instance it just exits.
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -Command "set WshShell = WScript.CreateObject('WScript.Shell'); WshShell.SendKeys('^ '); WshShell.SendKeys('+{F10}'); WshShell.SendKeys('n'); WshShell.SendKeys('{ENTER}')"
I basically just concatenated them and replaced the double quotes with single quotes to escape the special characters, so why does it not work as the separate script?
In PowerShell:
$WSH = New-Object -ComObject 'WScript.Shell'
$WSH.SendKeys('^ ')
$WSH.SendKeys('+{F10}')
$WSH.SendKeys('n')
$WSH.SendKeys('{ENTER}')
Save this as a script (Filename.ps1), and call it from a .bat or .cmd file:
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -File "path\filename.ps1"
Alternatively, as a one-liner in a command file:
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -Command "$WSH = New-Object -ComObject 'WScript.Shell';$WSH.SendKeys('\^ ');$WSH.SendKeys('+{F10}');$WSH.SendKeys('n');$WSH.SendKeys('{ENTER}')"
I currently have a .cmd file that runs the following two commands on startup
PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
PowerShell C:\Users\elias\Desktop\Script123.ps1 >> "%TEMP%\StartupLog.txt" 2>&1
But when this runs on startup it shows the command prompt. I'm wondering if there's anyway to run this without showing the command prompt.
Thank You
Invoking a .cmd file directly invariably opens a console window, so you need to invoke it via a wrapper executable that hides it:
This answer of mine contains a VBScript script that does just that; assuming you've saved it as runHidden.vbs in the current dir and that you want to invoke some-batch-file.cmd from the current dir:
wscript .\runHidden.vbs cmd "/c .\some-batch-file.cmd"
Try powershell -command "& { $x = New-Object -ComObject Shell.Application; $x.minimizeall() }" in the beginning of the code.
This command can minimize all windows.
I hope this can be helpful, if you have any other questions, please comment below.
Create a wrapper with VBScript:
Option Explicit
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /c blah.cmd", 0, False
Then change the startup item to
wscript.exe wrapper.vbs
I have tried to run the script using command
cmd.exe /c Start /min powershell.exe -windowstyle hidden -file <file>.ps1
But getting a CMD window for a fraction of a second. I need it to run completely hidden.
Configure the scheduled task to run whether the user is logged on or not:
and reduce the commandline to this:
powershell.exe -File "C:\path\to\your.ps1"
This makes the task run in the background with no visible window.
I have had this issue and the only way I could fix it was to call the PowerShell script with a simple VBS wrapper:
https://github.com/gbuktenica/PsRun
http://blog.buktenica.com/run-a-powershell-task-silently/
' SYNOPSIS
' Run a PowerShell script in the user context without a script window
' EXAMPLE
' wscript.exe PsRun.vbs MyPsScript.ps1
' AUTHOR
' Glen Buktenica
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
Dim PSRun
PSRun = "powershell.exe -WindowStyle hidden -ExecutionPolicy bypass -NonInteractive -File " & arg
objShell.Run(PSRun),0
I'm trying to have a scheduler to run a Vbscript, which it will run a Powershell script.
Basically here is my Vbscript
command = "powershell.exe -nologo -command C:\Users\someuser\Desktop\appAPIMonitor.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0
When I run above script, it worked. But when I place the script at my preferred path, the script wont run as before. So it must be the path not valid. Here is my new script
command = "powershell.exe -nologo -command C:\Users\someuser\My Work\App\Project\My.API.App\Scripts\appAPIMonitor.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0
Can somebody point what is wrong with my path? Is it the whitespace in My Work? Or the dot in My.Api.App?
This is the solution I managed to get.
command = "powershell.exe -nologo -file ""C:\Users\someuser\My Work\App\Project\My.API.App\Scripts\appAPIMonitor.ps1"" "
set shell = CreateObject("WScript.Shell")
shell.Run command,0
Give the last quote some white space and change -command to -file should do the trick. I solve this in my case. Hopefully help anybody in future. By the way, thanks to those who spent their precious time to answer my question.
I am trying to call Powershell with the Exec method of the WshShell object. I am writing the script in JScript, but I have reproduced the problem in VBScript as well. Both of the following short test scripts will cause WSH to hang indefinitely:
test.js
var shell = new ActiveXObject("WScript.Shell");
WScript.Echo(shell.exec("powershell -Command $Host.Version; Exit").StdOut.ReadAll());
test.vbs
dim shell
set shell = CreateObject("WScript.Shell")
WScript.Echo shell.exec("powershell -Command $Host.Version; Exit").StdOut.ReadAll
Am I doing something wrong, or am I running into or a limitation/incompatibility? The Run method works very well, but I need to capture output, which it's not capable of doing.
Edit: I forgot to mention that my platform is Windows 7 Pro, 64-bit with PowerShell 3. I've tested on Windows XP with PowerShell 1 as well.
Edit 2: I've updated the test scripts that I'm running to fit with x0n's answer. Unfortunately, I'm still having trouble. Here are my current tests:
test.js:
var shell = new ActiveXObject("WScript.Shell");
WScript.Echo(shell.exec('powershell -noninteractive -noprofile -Command "& { echo Hello_World ; Exit }"').StdOut.ReadAll());
test.vbs:
dim shell
set shell = CreateObject("WScript.Shell")
WScript.Echo shell.exec("powershell -noninteractive -noprofile -Command ""& { echo Hello_World ; Exit }""").StdOut.ReadAll
You have to close StdIn:
var shell = new ActiveXObject("WScript.Shell");
var exec = shell.Exec('powershell -noninteractive -noprofile -Command "& { echo Hello_World ; Exit }"');
exec.StdIn.Close();
WScript.Echo(exec.StdOut.ReadAll());
Microsoft said:
StdIn is still open so PowerShell is waiting for input. (This is an
"implementation consideration" that we're hoping to fix in V2. The
PowerShell executable gathers all input before processing.) So
objexec.StdIn.Close() needs to be added.
Use:
powershell.exe -noninteractive -noprofile -command $host.version
as your string. For more complicated groups of commands, use this syntax:
powershell.exe -noninteractive -noprofile -command "& { $host.version; $host.version }"