Difficulty with Running Executable Remotely

Started by keslaa, October 24, 2013, 05:17:17 AM

Previous topic - Next topic

keslaa

Hello,

I am trying to run an executable on a remote machine. If I run the executable from a command prompt on my machine, it works. I cannot get it to work through my script. This needs to run with an elevated privilege, so I am trying to use wntRunAsUser. If I use a 1 for the flag to allow the child processes to inherit the same security, I get a WIL Extender Error: 713: Unable to set windowstation access error message on the command itself. If I switch the flag value to a 0, the RunWait command I use for the executable returns with a 1932: WinExec: Function Failed error message. Again, if I open a command prompt using the same credentials in my wntRunAsUser command, the executable runs flawlessly. Any ideas on what to run down?

Deana

What Windows Platform are you running?

Is UAC on?

What happens if you change the RunWait to ShellExecute?

Post the Command that you are running from the command line that works.
Deana F.
Technical Support
Wilson WindowWare Inc.

keslaa

I am running this on Windows 7, but the target machines are all XPSP3.
UAC is off.
Have not tried ShellExecute

The utility I am using is delprof2, a profile removal utility. We have a large number of machines that have multiple stale profiles on them we need to clean up.

delprof2.exe /c:<TARGET MACHINE> /id:<PROFILE TO DELETE> /u

Deana

The documentation for wntRunAsUser states:

QuoteWindows Vista/2008 or newer: wntRunAsUser cannot be use  to escalate ( or elevate) privileges.  You can use this function to act as a specific standard user from an admin account as long as you do not run as an elevated admin.

Windows 7:  When UAC is on, a standard user can specify Administrative credentials but the script will still only act as a standard user.  When UAC is off, a standard user can specify Administrative credentials but privileges actually get reduced to less than a standard users (anonymous impersonation.)

If you are an administrator and UAC is on, you can use wntRunAsUser  to act as a standard user, as long as you don't elevate to full administrator privileges before you run the script.  If you elevate first, wntRunAsUser basically doesn't lower your privileges and the process continues to use your administrator privileges.

Check out the following tech article:
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/UAC+RunWithLogon~and~UAC.txt

First thing to try: We have found that using ShellExecute with the "RunAs" operation parameter can be useful in such situations.







Deana F.
Technical Support
Wilson WindowWare Inc.

keslaa

Thank you for the reply. Following the link you provided, I was able to successfully call the program and delete a single profile. However, if I try to incorporate this into a loop, I get all sorts of errors. I have included my code snippet below. For review, I have written a script that does the following:

  • Open a file with a list of computer names from a SCCM report
  • Retrieve the list of machine names
  • Loop through the list of machine names and try to access each machine

    • If the machine cannot be accessed, write the name to a file "BadNames.txt"
    • If the machine can be accessed:

      • Write the name to a file "GoodNames.txt"
      • Create a file named <MachineName>.txt
      • Scan the list of profiles stored on the computer
      • For every profile that meets specific criteria, write the profile name to <MachineName>.txt
The part of the script that I need help with will open "GoodNames.txt", get the name of a computer, open that computer's text file to retrieve the list of profiles to delete and then call the code to execute the external program (delprof2.exe). Each call to the external program will have two parameters: the machine name and the profile name. I first had the profile deletion lines embedded in the loop that retrieves the profiles, then I moved it to it's own sub-routine. I'm not sure if I need to put that portion in a UDF or as a separate script altogether and use a Call() function. Anyway, here is the code. There may be some lines in there that seem redundant (item counts, checking for blank values, etc.) that I used to work around the errors I was experiencing. Thank you for your time.

Eric

handleread = FileOpen("C:\Temp\GoodNames.txt", "READ")
count = 0
GoodNameList = ""
While @TRUE ; Loop till break do us end
line = FileRead(handleread)
If line == "*EOF*" Then Break
GoodNameList = strcat(GoodNameList, line, @TAB)
count = count + 1
EndWhile

FileClose(handleread)

c = ItemCount(GoodNameList, @TAB)

For x = 1 to c
ConfroomPC = ItemExtract(x, GoodNameList, @TAB)
if ConfroomPC == "" Then Break
ProfileList = ""
If FileExist("C:\Temp\%ConfroomPC%.txt") == @TRUE
handle = FileOpen("C:\Temp\%ConfroomPC%.txt", "READ")
ProfileName = FileRead(handle)
If ProfileName <> "*EOF*"
ProfileList = StrCat(ProfileList, ProfileName, @TAB)
ProfileName = FileRead(handle)
EndIf
if ItemCount(ProfileList, @TAB) > 0
gosub DeleteProfile
endif
FileClose(handle)
endif
next

:done
exit

:DeleteProfile

y = 1
While y <= ItemCount(ProfileList, @TAB)
ProfiletoDelete = ItemExtract(y, ProfileList, @TAB)
appname = DirScript():"delprof2.exe"
If param0 == 0
RunWithLogon(IntControl(1004, 0, 0, 0, 0), "Elevator", DirScript(), @NORMAL, @NOWAIT, "<admin_name>", "<admin_domain>", "<admin_password>", 0)
Else
If param1 == "Elevator"
ShellExecute(appname, "/c:%confroomPC% /id:%ProfiletoDelete% /u", FilePath(appname), @NORMAL, "")
EndIf
EndWhile

return


Deana

Try just using the ShellExecute function to launch delprof.exe.

Code (winbatch) Select
:DeleteProfile
y = 1
While y <= ItemCount(ProfileList, @TAB)
   ProfiletoDelete = ItemExtract(y, ProfileList, @TAB)
   appname = DirScript():"delprof2.exe"
   ShellExecute(appname, "/c:%confroomPC% /id:%ProfiletoDelete% /u", FilePath(appname), @NORMAL, "")
EndWhile
return
Deana F.
Technical Support
Wilson WindowWare Inc.

td

Assuming the script is not compiled and is being executed from a Standard User account, the simplest solution might be to rename the script's file extension from '.wbt' to 'wbt_af' and simply use the WinBatch Run function instead of RunWithLogon and ShellExecute.  That way, credentials would only need to be entered once. The solution would also work for an unevaluated Admin account, if UAC happens to be enabled.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Never mind.  Changing the script file's extension along with  ShellExecute works on Windows 8/8.1 with UAC off but you would still need to use RunWithLogon on Windows 7 with UAC off.  On Windows 7 do as Deana suggested and call RunWithLogon once to restart the script as an admin and then you could use either the ShellExecute or the Run function to start each instance of delprof2.exe.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

keslaa

Thanks, Deana.
For what its worth, this was my fix:

Code (winbatch) Select
wntRunAsUser("<domain>", "<admin_name>", "<admin_password>", 2, 0)
For y = 1 to ItemCount(ProfileList, @TAB)
ProfiletoDelete = ItemExtract(y, ProfileList, @TAB)
if ProfiletoDelete == "" Then Break
appname = DirScript():"delprof2.exe" ; compiled with the manifest 'HighestAvailable' or 'RequireAdministrator'
ShellExecute(appname, "/c:%confroomPC% /id:%ProfiletoDelete% /u", FilePath(appname), @NORMAL, "")
display(2,confroompc,profiletodelete)
Next
wntRunAsUser( "", "", "", 0, 0)

kornnuts

Thanks for the help with resolving my issue as well (Remove any version of Citrix on the machine, reboot, install Citrix Receiver, reboot, then remove a couple of reg keys created by the installation.)  After reading this post I was able to use RunWithLogon to call my executable file, then use the wntRunAsUser to remove some registry entries.  I posted my method in hopes it helps the next person.  Thanks again!!

RunWithLogon file:
Code (winbatch) Select

#DefineSubroutine RunElevated()
runas_user = "Administrator"
runas_pswd = "password"
runas_domain = "domain"
appname = DirScript():"cleanup.exe"
RunWithLogon(appname, "", "", @NORMAL, @NOWAIT, runas_user, runas_domain, runas_pswd, 1)
#EndSubroutine


wntRunAsUser file:
Code (winbatch) Select

#DefineSubroutine RegCheck()
wntRunAsUser("domain", "Administrator", "password", 2, 0)
if regexistvalue(@REGMACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion\Run[ConnectionCenter]')
regdelvalue(@REGMACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion\Run[ConnectionCenter]')
endif
if regexistvalue(@REGMACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion\Run[Redirector]')
regdelvalue(@REGMACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion\Run[Redirector]')
  endif
wntRunAsUser( "", "", "", 0, 0)
#EndSubroutine