I'm trying to lauch outlook with CMD with simple script I have made. Problem becomes when I open this script on user's computer via CMD ran as admin (my user account) it will open me outlook as I would be opening it now user. So my inbox will apper not user. Script works perfectly if I just run it from normal cmd not elevated mode.
How do I make sure that from elevated CMD it will open me user's inbox not user's that has elvated CMD?
strPath = WshShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")
outlook15 = strPath & "\Microsoft Office\Office15\outlook.exe"
outlook14 = strPath & "\Microsoft Office\Office14\outlook.exe"
If fso.FileExists(outlook15) Then
msgbox "TITUS successfully fixed!", 64
WScript.Sleep 3000
WshShell.Run Chr(34) & outlook15 & Chr(34)
Set fso = Nothing
Set WshShell = Nothing
Else
msgbox "TITUS successfully fixed!", 64
WScript.Sleep 3000
WshShell.Run Chr(34) & outlook14 & Chr(34)
Set fso = Nothing
Set WshShell = Nothing
End If
To detect the current user running CMD you can grab the output from the whoami command like this:
Set oExec = WshShell.Exec("whoami")
strUser = oExec.StdOut.ReadLine
Then you can add runas to the WshShell.Run statement to change the user context:
If strUser = "admin" Then
WshShell.Run "runas /user:" & otherUser & " " & Chr(34) & outlook15 & Chr(34)
End If
The other username will need to be supplied, either through an input prompt, or it can be captured from another command(qwinsta, psloggedon, etc) and extracted via regex. It also should be noted that runas will prompt for the other user's password.
Related
I have a vbscript that spawns powershell in admin mode via:
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "powershell", "-executionpolicy bypass", "", "runas", 1
and then I attempt to send keys via:
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.sleep 2000
WshShell.sendkeys "hello"
Since the keys didn't work, I tried separating the latter part into a separate file and specifically did a WshShell.AppActivate on the PID to make sure it gets the right window focus before sendkeys, however it still won't send the string.
Conversely, if I don't run powershell in admin, everything works fine:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.run "powershell"
WScript.sleep 200
WshShell.sendkeys "cls"
Can someone please tell me what I'm doing wrong?
While I don't have an official reference for you, I'm pretty sure that by design, for security reasons, you can not send keystrokes to elevated (run-as-admin) processes from a non-elevated process.
If this were possible, non-elevated processes could bypass their limited privileges, which would be an obvious violation of the rules.
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\"
I would like to know how to install something, anything, such as notepad with powershell unattended. i really cant find much on this online. I renamed the exe file to notepad.exe and threw it on the root of c. this doesnt work but its what I got:
Start-Process c:\notepad.exe -ArgumentList "/q" -Wait
I cant get anything from the powershell help. this still makes me have to accept and click through the install process, im trying to avoid that. any help and educating me too would be greatly appreciated
If you run the install program with /? as a switch, it shows you the answer - either /passive for an unattended install, or /q for a silent install.
That totally depends on the installer you're trying to use. Unfortunately, Windows installers aren't all .msi packages, and the executables all have different switches.
You could check out Chocolatey, which certainly has loads of silent install scripts for installing apps (and everything's done with PowerShell scripts), including packages for Notepad++ and Notepad2, etc.
you can install a program in silent mode with a vbs script :
Here is an example :
Option Explicit
Dim MaCmd
MaCmd = "Start /Wait D:\Soft\file.exe /S"&_
" & echo Soft was fully installed > LogInstall.txt & Start LogInstall.txt"
Call Executer(MaCmd,0)
'**************************************************************************************************
Function Executer(StrCmd,Console)
Dim ws,MyCmd,Resultat
Set ws = CreateObject("wscript.Shell")
'Value 0 for hide Ms-Dos
If Console = 0 Then
MyCmd = "CMD /C " & StrCmd & " "
Resultat = ws.run(MyCmd,Console,True)
If Resultat = 0 Then
'MsgBox "Success"
Else
MsgBox "an unknown error occurred"
End If
End If
'Value 1 for show MS-Dos
If Console = 1 Then
MyCmd = "CMD /K " & StrCmd & " "
Resultat = ws.run(MyCmd,Console,False)
If Resultat = 0 Then
'MsgBox "Success"
Else
MsgBox "an unknown error occurred"
End If
End If
Executer = Resultat
End Function
'****************************************************************************************************
I'm trying to pass the user input computer name to the psexec command. I've tried about everything. I know the command works because if I run the file by manually entering the computer name is successful. I need to get the strComputer variable into the \strComputer psexec command. Thanks in advance.
Set objShell = CreateObject("Wscript.shell")
Dim strComputer
strComputer=InputBox("Run app on remote computer",,"Type name here & click OK")
objShell.run("powershell psexec.exe -i -s \\strComputer \\domain.com\folder\app.exe domain.com")
MsgBox("Scan complete")
Why not use a PowerShell script for this?
Contents of myscript.ps1
------------------------
$computerName = Read-Host "Enter name of remote computer"
psexec \\$computerName -i -s \\domain.com\folder\app.exe domain.com
"Scan complete"
User JPBlanc and others have given a nice solution to such a problem with the solution:
Set Args = Wscript.Arguments
'MsgBox "Chemin LDAP: " & Args(0)
'MsgBox "Classe: " & Args(1)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file c:\SlxRH\RhModif.ps1 " & chr(34) & Args(0) & chr(34) , 0
I have tested such a solution out and it works fine. However, my problem concerns the situation when the file path identified as the argument for the "-file" option has white space in it; as example, if the code example above had a file path of:
c:\Slx RH\RhModif.PS1
Observe the white space in the file path. The question is this: how to escape the file path correctly. I haven't found a way to do this (to make it work).
I have searched on this site and the general "interweb" and have learned (I think) that there are two levels of script interpretation going on: VBscript and the Wscript. Another very good example appears here (link to the full page with this title):
Run scheduled tasks with WinForm GUI in PowerShell
Dim objShell
Set objShell=CreateObject("WScript.Shell")
strExpression="get-date | add-content c:\temp\test.txt"
strCMD="powershell -sta -noProfile -NonInteractive -nologo -command " & Chr(34) & "&{" & strExpression &"}" & Chr(34)
objShell.Run strCMD,0
I have tested this and it works for me. While not using the "-file" option, this example gives (what you would think) would be a generic method of putting a "-command" or "-file" input for the strCMD variable. However, when I use the following code, it does not work:
Dim objShell
Set objShell=CreateObject("WScript.Shell")
strExpression="C:\aP_RDB\Set Up\SimpleInsert.PS1"
strCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & Chr(34) & "&{" & strExpression &"}" & Chr(34)
objShell.Run strCMD,0
In my example above, I essentially copied the code reference above (and from the link listed), replaced the PS command with my file path (which includes white space) for the variable strExpression, and updated the strCMD definition by replacing the "-command" argument with the "-file" argument. However, the error message points to an unrecognized argument "c:\aP_RDB\Set" that is, it does not see the whole path "C:\aP_RDB\Set Up\SimpleInsert.PS1". It appears that the escaping of the quotes (or something) is not working... better said, I am not doing it correctly.
Any thoughtful advice would be greatly appreciated. FYI: I have tried the -WindowStyle Hidden option in Powershell; I like many have found that not to be a consistent approach across Windows platforms (more specifically for me: I could not get it to work).
Thanks for the reference, here are two ways to start scripts with white space in the name :
1) using double " ("")
set Args = Wscript.Arguments
MsgBox "Chemin LDAP: " & Args(0)
'MsgBox "Classe: " & Args(1)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file ""c:\temp\test arg.ps1"" " & chr(34) & Args(0) & chr(34) , 0
2) Consist in writing your own PowerShell.exe with no console, as I explain at the end of this article ""PowerShell.exe" ne sait pas dĂ©marrer sans afficher une fenĂȘtre"(sorry in french)