How do you run another .exe from VB.NET as another User? - vb.net

How do you run another .exe from VB.NET but as another User?
I expect to launch a .exe like "Left Click -> Run As -> Enter User/Pass -> Click OK"
If I do that, my application runs as expected (I need to run it as another user to get access to some folders in the network)
But if I use this in VB.NET
System.Diagnostics.Process.Start(System.Windows.Forms.Application.ExecutablePath, PARAMETER, USER, PASSWORD, DOMAIN)
The application runs with the other user.. but Excel (inside my App with Interop) fails to open the file in the restricted folder.
(I run again the same app but with a different user, just to avoid creating more .exe files... but I already tried with vbScript)
Again, Process.Start FAILS to open excel using the other User... but Left Click -> Run as succedes at that... why?? another way??
this is what the app does:
Open the app
check if there's a parameter
if no parameter, then relaunch the application with the other user and send some parameter
if there is a parameter open excel
open a xlsx file
but if I double click... Excel opens... uses 50% CPU, and gives me the error that it can't open the file...
if I run it directly with the desired user and pass... everything executes fine
Any suggestions as how to solve this? (impersonate works fine.. but it opens Excel with the actual user.. not the one with rights)
Thanks!

If you get "Handle is invalid" error, you should try something like this:
dim info As New ProcessStartInfo("...")
info.UseShellExecute = False
info.RedirectStandardInput = True //This is the key
info.RedirectStandardError = True //This is the key
info.RedirectStandardOutput = True //This is the key
info.UserName = "username"
info.Password = "password"
Using (install As Process = Process.Start(info))
Dim output As String = install.StandardOutput.ReadToEnd()
install.WaitForExit()
End Using
Specifying any one of
RedirectStandardOutput=true, RedirectStandardError=true, or RedirectStandardInput=true
causes the process to be launched with STARTF_USESTDHANDLES. If your process does not have any of these handles, then CreateProcessWithLogon will fail with "Invalid Handle".
You MUST redirect it (even if you don't intend to write anything to it).
Regards

This is really interesting. By default, I believe the Excel COM components are set up to run as the Interactive User (ie the user logged into the box). If they are configured to run as the Launching User then impersonation should work. Of course, this does not explain why "Run As..." works (I don't know the mechanics of that so perhaps it's not impersonation).
One idea is to restructure the application to copy the files to a location Excel can access, manipulate them, and then copy the back.

Related

Applescript to open Safari

(newbie here) I'd to be able to save an applescript file as an app to use to open Safari, I cobbled together this from some old posts I found, it's not exactly what I want but it worked ok, however today it stopped working giving an error "The variable theURL is not defined."
If anyone could help me out with a better script that would work on Monterey I would be really grateful. Also, when I save as an app - should I select 'Stay open after run handler' or 'Run-only'? It just needs to open Safari when it's clicked, I don't need it to really open any particular page. Thanks in advance!
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:"https://google.com.au"}
on error
open location theURL
end try
end tell
I cobbled together this from some old posts I found:
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:"https://google.com.au"}
on error
open location theURL
end try
end tell
A lot (virtually all) applescripts you see online make overuse of try blocks, which, apart from being completely unnecessary, are problematic in a number of ways, including making debugging more difficult. I'd advise taking out the line try, and everything including and between the lines on error, and end try.
Without those lines, you'll probably get a different error message, which was being masked by the try block. It most likely that window 1 didn't exist, which would be the case if Safari had already been running and didn't have any windows open. It would have then thrown an error, forwarded the script on to the on error block, where open location theURL then, itself, threw an error because theURL hasn't been defined.
I don't need it to really open any particular page
Then all you need is this:
tell application "Safari" to activate
If Safari is already running without any open windows, all this does is bring Safari to the front; it doesn't open a new window for you. If you need to create a new window you can usually do this:
tell application "Safari"
make new document
activate
end tell
However, this will create a new window even if there's already a window open, which might not be what you want. To create a window but only if a window doesn't already exists, you can do this:
tell application "Safari"
if documents = {} then make new document
activate
end tell
I suspect this is the script you will want to use.
Should I select 'Stay open after run handler' or 'Run-only'?
No and no. The first option prevents your app from quitting without explicit instruction to do so, which isn't needed in this case: you simply need your app to open, start Safari, then quit. The last option prevents you from making any edits to the script in the future, which, again, probably isn't what you want.

How to handle external applications in a selenium-test?

I am writing a test that verifies that a warning to the user before the user opens a document. If the user says that they still would like to open the document, the document opens in an external application (pdf or word).
But now I have an external application over the browser window, and it messes up for other tests.
So, what are the best practices around this kind of issue? Rewrite of the appliction to allow for not opening documents in test?
Added description:
The problem is twofold.
1) It starts processes (word and acrobat) that fills the desctop and requires resources from the test-slave
2) The external process seems to interfer with other tests since (guessing here) it is located over the browser window.
what i understood from your post is, the document(word/pdf) is opening in the browser window hence you are not able to proceed with further steps. If so, you can verify the Title to make sure the document is opened in browser window and can navigate back using below snippet.
driver.navigate().back();
Hope this helps.
What I understand from the line
'But now I have an external application over the browser window, and it messes up for other tests.'
is that once user clicks on open button a new window opens up (Window based application) since you have mentioned PDF or Word.
You can use robot class in such cases, below code snippet will close the current active window:
Robot rbt = new Robot();
rbt.keyPress(KeyEvent.VK_ALT);
rbt.keyPress(KeyEvent.VK_F4);
rbt.keyRelease(KeyEvent.VK_ALT);
rbt.keyRelease(KeyEvent.VK_F4);
Make sure you deal with sync issues properly so that intended window is closed instead of AUT.

My goal is to launch a file using vb.net via the click of a button

My goal is to launch a file using this in vb.net via the click of a button..
System.Diagnostics.Process.Start("X:\Desktop\Brutal Doom\PLAY ME BRUTAL DOOM!!!.pk3")
Which works, however as soon as I move to my laptop or another computer the button can no longer find the file as the drive letter has changed.. is there a way to code this so that it is no longer dependable on the drive letter and only will see \Brutal Doom\PLAM ME BRUTAL DOOM!!!.pk3 thus working on whatever computer I put the folder on...
Are you basically looking for this? How to get a path to the desktop for current user in C#?
ie System.Diagnostics.Process.Start(My.Computer.FileSystem.SpecialDirectories.Desktop & "\Brutal Doom\PLAY ME BRUTAL DOOM!!!.pk3")

How to schedule a .vbs script with UI elements?

I am new to vb scripting. I need to schedule a windows task which can run a vbs file.
The script opens up a application from its shortcut, feeds username and password and performs login.
In other words, I need to automate the restart process.
The scripts runs as expected when opened manually but scheduler cannot not run it.
I think the problem is with those UI elements(used for login).
Please suggest how to get through it.
Thank you!
AppActivate does not itself restore the window if it is for some reason minimized.
You can try to find the reason why it's minimized (maybe it has something to do with the batch calling the vbs calling the application, I cannot really say) or you can try a little hack in the vbscript like the following:
After the AppActivate do a:
oShell.SendKeys "% r"
This is equivalent of Alt+SpaceBar, r so it opens the windows dialog for Maximize, Minimize, Restore options and choses "Restore".
Note that this might only be r on english copies of windows. If you use another Language manually open that menu by pressing Alt+SpaceBar and looking what the actual hotkey on your system is (it would be underlined in the dialog). Afterwards replace the r in the script with whatever hotkey is used in your language.

Send key not working for custom application in Auto It

I want to open my application myprogram.exe and hit tab. I have written the following script
Local $Pid = ShellExecute("C:\Program Files\myProgram.exe","-a arg","","")
Sleep(5000)
Send("{TAB}")
But it doesn't work. My application gets open and it works if I manually hit TAB. But it's not accepting any key via the script.
I tried the same thing with notepad and it works without any problem
Local $Pid = ShellExecute("notepad.exe","","","")
Sleep(5000)
Send("{TAB}")
Be sure that the window has focus with WinActivate
Also as suggested try controlsend or controlclick, if ether of those doesn't work it is properly because the program you are trying to access doesn't take simulated inputs