Having issues executing commands via CMD

Started by radioman54, February 13, 2014, 08:28:35 PM

Previous topic - Next topic

radioman54

In the past I've been able to activate CHKDSK using the following command:

Run("cmd.exe", "/c echo y | chkdsk c: /f /r")

Recently this stopped working. When I attempted to test the command in the CMD window I received the following error:

Access Denied as you do not have sufficient privileges.
You have to invoke this utility running in elevated mode.

If I run CMD as Administrator I can execute the command. Is there any way to run my chkdsk command from my WinBatch script as Administrator?

Deana

One of the requirements that User Account Control (UAC) puts on developers is that you must mark your applications with a 'manifest' to declare if the application would like to run elevated or not. This can complicate matters when trying to develop and debug scripts using WinBatch, WinBatch+Compiler and WinBatch Studio. We have made available various WinBatch tools with all the possible manifest setting combinations.

How are you running the script? Double Click on .WBT, WinBatch Studio, or Compiled?

What user account will this script be run on? Administrator or standard user?

For more about Manifest see Window Interface Language help file topic 'Manifests'.
Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

It's a compiled script that is run as part of my StartUp folder. I have a WinBatch script that performs all sorts of maintenance tasks during the night and what day it happens to be. It's running from my account which has Administrator rights. The script (along with the chkdsk section) worked just fine until a couple of weeks ago. I've noticed lots of things that used to run fine no longer do. I do remember a Windows 7 update running 2 or 3 weeks ago but don't recall if this problem started right after that.

Deana

Ok if you are running under an administrator account, then make sure to compile your EXE using the Manifest setting HighestAvailable *or* RequireAdmin.
Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

Just did that. Will see if it runs tonight as it is supposed to. Will let you know.

radioman54

Well it didn't work. As a matter of fact it made things worse. Many of the commands in the script are no longer working and the chkdsk is still not being scheduled.

Deana

Quote from: radioman54 on February 18, 2014, 05:08:10 AM
Well it didn't work. As a matter of fact it made things worse. Many of the commands in the script are no longer working and the chkdsk is still not being scheduled.

You state that "it didn't work" but didn't specify if you received an error or the script simply didn't run at all.

Time to start debugging. I recommend using DebugTrace. Simply add DebugTrace(@on,"trace.txt") to the beginning of the script and inside any UDF, run it until the error or completion, then inspect the resulting trace file for clues as to the problem.

Feel free to post the trace file here ( removing any private info) if you need further assistance.
Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

The script ran but almost every line produced an error message. Surprisingly, the line that should invoke the CMD that runs CHKDSK didn't give an error but CHKDSK didn't run either.  I recompiled with it set for asInvoker and the script runs fine - not a single error. I'll try the debug.

Deana

Quote from: radioman54 on February 18, 2014, 08:10:18 AM
The script ran but almost every line produced an error message. Surprisingly, the line that should invoke the CMD that runs CHKDSK didn't give an error but CHKDSK didn't run either.  I recompiled with it set for asInvoker and the script runs fine - not a single error. I'll try the debug.

Well if you are logged in as an Admin and you compile using AsInvoker the script is not running with the full admin token.

I suspect the problem is that the script compiled with HighestAvailable or RequireAdministrator will display a UAC prompt which might not be allowed during startup.

Maybe rather than running at startup you should run it from the task scheduler, such that it is launched after login of a user, using a user account that has administrative access on the system, with the highest privileges that are afforded to processes launched by that account.
Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

I give up. Now the script will not load from the StartUp folder and when I click on it in the StartUp folders it gives errors on several lines in the script that do not error when I compile the script with noAUC.

Deana

Quote from: radioman54 on February 18, 2014, 10:18:28 AM
I give up. Now the script will not load from the StartUp folder and when I click on it in the StartUp folders it gives errors on several lines in the script that do not error when I compile the script with noAUC.

Why would you compile using the Option: NO UAC Info? You need to specify the proper manifest settings when you compile. This effects how the exe is treated by the OS!

If you are running on Windows 7 with UAC ON, apparently programs are not allowed to run elevated (run as administrator) automatically at 'startup' for security purposes. Try running the script from the task scheduler, such that it is launched after login of a user, using a user account that has administrative access on the system, with the highest privileges that are afforded to processes launched by that account.
Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

As I said in previous posts it will not load from the StartUp folder if it is compiled with any setting other than No UAC Info. If I compile it with Highest Available or Administrator it will not load when the computer boots.

I can't run the script via the task scheduler because I need the things in my script to happen at boot time. I will try running a script specifically for CHKDSK from the scheduler and see if that works.

Deana

Quote from: radioman54 on February 18, 2014, 11:12:09 AM
As I said in previous posts it will not load from the StartUp folder if it is compiled with any setting other than No UAC Info. If I compile it with Highest Available or Administrator it will not load when the computer boots.

I can't run the script via the task scheduler because I need the things in my script to happen at boot time. I will try running a script specifically for CHKDSK from the scheduler and see if that works.

Take a look at the Windows scheduler, you will find that you can set the task TRIGGER to run at startup, then point it at your properly manifested Compiled WinBatch EXE.
Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

I compiled the script with requireAdministrator and highestAvailable and put it in the task scheduler to run at startup. Didn't run. As far as I could tell it didn't even try to load.

Deana

Quote from: radioman54 on February 18, 2014, 12:01:37 PM
I compiled the script with requireAdministrator and highestAvailable and put it in the task scheduler to run at startup. Didn't run. As far as I could tell it didn't even try to load.

You are really going to need to debug at this point. The best way to do that from a scheduled service is to use DebugTrace. Please add the following lines of code to the very beginning of your code:

Code (winbatch) Select

IntControl(38,1,0,0,0) ; Its a good idea to enable quiet mode for service scripts
DebugTrace(@on,DirScript():'trace.txt') ; Creates a trace file in the same directory as the EXE


Compile the script using the manifest settings: requested execution level AsInvoker and uiAccess FALSE.

Create a task in the task scheduler, setting the trigger to Run at Start Up, Specify a user account that has administrative access on the system and check the box next to "Run with the highest privileges" .

Restart your system.

Post the resulting trace file here if you need further assistance.
Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

"The operation being requested was not performed because the user has not logged on to the network. The specified service does not exist (0x800704DD)" shows in the Task Scheduler. I have no idea what network is it looking for.

radioman54

I setup the scheduled task to sign-in as me and now the error is "Access is denied. (0x80070005)".

snowsnowsnow

I'll give you the best piece of advice you're going to get on this thread.

This stuff is horse manure!

Re-install XP!

radioman54

I agree with you but XP is not an option at this point.

Deana

Quote from: radioman54 on February 18, 2014, 12:50:49 PM
"The operation being requested was not performed because the user has not logged on to the network. The specified service does not exist (0x800704DD)" shows in the Task Scheduler. I have no idea what network is it looking for.

Please provide details about exactly which task settings were used.

Are you specifying a local account or domain account?
What trigger is used?
Where does your compiled WinBatch exe reside?

Does anything in your script require network access? If so remove it and try running the task under the local system account which only has access to the local machine ( no network access).

http://msdn.microsoft.com/en-us/library/bb325654.aspx


Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

This is the script....

ErrorMode(@Off)

;IntControl(38,1,0,0,0) ; Its a good idea to enable quiet mode for service scripts
;DebugTrace(@on,DirScript():'StartUp Trace.txt') ; Creates a trace file in the same directory as the EXE

;WinHide("")

AddExtender("C:\Program Files (x86)\WinBatch\Redistrb\wwwnt34i.dll")
AddExtender("C:\Program Files (x86)\WinBatch\Redistrb\wwctl44i.dll")

SystemStatus = IniReadPvt("System Status","Status","Home","WinBatch.ini")
If SystemStatus == "Home" Then SystemStatus = 1
If SystemStatus == "Away" Then SystemStatus = 2

StartUpStatus = IniReadPvt("StartUp Status","Status","StartUp","WinBatch.ini")
If StartUpStatus == "StartUp" Then StartUpStatus = 1
If StartUpStatus == "Backup" Then StartUpStatus = 2

If SystemStatus == 1
   StartTrillian = IniReadPvt("Trillian Status","Status","Yes","WinBatch.ini")
Else
   StartTrillian = "No"
EndIf

Date = TimeYmdHms()
JulianDate = TimeJulianDay(Date)
DayOfWeek = ((JulianDate + 5) mod 7) + 1

Select SystemStatus
   Case 1
      Select StartUpStatus
         Case 1

            TimeDelay(30)

            If StartTrillian == "Yes"
               TimeDelay(10)
               RunIcon("C:\Program Files (x86)\Trillian\Trillian.exe","")
            EndIf

            TimeDelay(10)
            Run("C:\Program Files (x86)\KeePass Password Safe\KeePass.exe","")

            TimeDelay(10)
            window1=cWndByWndSpec("Shell_TrayWnd","explorer",3,303,40965,0)
            window2=cWndByID(window1,40965)
            window3=cWndByName(window2,`Running applications~`)
            ControlHandle=cWndByID(window3,0)
            winid=cWinidConvert(ControlHandle)
            MouseMove(18,20,winid,"")
            MouseClick(@LCLICK,0)
            TimeDelay(20)
            WinZoom("Internet Explorer~")
            TimeDelay(30)
            WinIconize("Internet Explorer~")

            TimeDelay(10)
            RunZoom("C:\Program Files (x86)\Windows Live\Mail\wlmail.exe","")
            TimeDelay(20)
            WinIconize("~Windows Live Mail~")

            Timedelay(10)
            DirChange("C:\Program Files (x86)\ECCO")
            RunZoom("C:\Program Files (x86)\ECCO\ECCOExt.exe","")
            TimeDelay(20)
            WinActivate("Ecco - [C:\Users\C.B. McCoy\Documents\My Ecco Files\CB McCoy.eco]")
            WinIconize("Ecco - [C:\Users\C.B. McCoy\Documents\My Ecco Files\CB McCoy.eco]")
            
            TimeDelay(15)
            RunIcon("C:\Program Files (x86)\MyPhoneExplorer\MyPhoneExplorer.exe","")

            Break

         Case 2

            TimeDelay(45)

            Run("c:\windows\system32\cleanmgr.exe","/sagerun:1")

            TimeDelay(120)

            Select @TRUE

               Case DayOfWeek == 7;
                  Run("cmd.exe", "/c echo y | chkdsk c: /f /r")

                  Break

            EndSelect

            IniWritePvt("StartUp Status","Status","StartUp","WinBatch.ini")

            Break

      EndSelect

      Break

EndSelect

Return

Deana

That code adds an extra level of difficulty because it needs interactive UI access and services no longer support interactive access on Windows 7.

REMOVE THE ERRORMODE(@OFF).  http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch+Two~Minute~Lecture~on~ErrorMode~No-Nos.txt

Ok Lets recap. You are trying to run this code on a Windows 7 System, logged in as the administrator and you would like it to run at startup. Correct?

The problem is that, on Windows 7;  applications that require administrator-level privileges to run are blocked when launched from the following locations:


  • Per-user startup folder
  • Per-machine startup folder
  • Per-user RUN key
  • Per-user machine RUN key

This is why I suggested running as a service under to local system account. It would run elevated and could be triggered at startup. Unfortunately services do not support interactive access and your script requires it.

The following article recommends splitting an application into Standard User and Administrator components:
http://msdn.microsoft.com/en-us/library/bb325654.aspx

This trick will be to determine which parts of the process need full admin privileges. The basic concept would be to compile the script using AsInvoker and place it in the startup dir. Then inside the script, at any point at which you need to elevate to admin you will run a second exe compiled with the manifest setting RequireAdmin ( or if logged in as admin you can use HighestAvailable ). This exe will prompt the user for elevation and run with the higher elevation. This higher elevation exe can contain all the code that needs to be run as an admin.

Clear as mud...I know. Its just how UAC works on Windows Vista and newer.



Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

Ok, I give up. No clue what is "interactive" about the script. To me interactive usually means that user input is require but the script requires nothing. I'll just live with running chkdsk manually and forget about it from the script. I don't know what has changed but something did. This script was running perfectly (the last run that successfully kicked off chkdsk was on 2/1/2014).

Deana

The Control manager and Mouse operations are considered "interactive" because they deal with the User Interface.

You state that the script must run elevated, and that you placed it in the startup directory(which blocks elevated apps), and it was working previously. Is this Correct?


Was it working on this same system?
Was the script compiled and running from a [startup] dir?
If compiled what manifest settings were used?
Was the script modified at all, when it stopped working?

Again you should remove the ERRORMODE(@off), it can turn a two minute debugging session into a week long nightmare....
Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

It was working on this system without any issues what-so-ever. I've had this system for a little over a year. The script was compiled (using the 2013 version of WinBatch Compiler), on this machine and executed from the StartUp folder at boot time. It was compile with whatever the default manifest settings are because I never changed them. I didn't touch the script until it stopped working. I was trying to figure out why it wasn't working. The only modifications that I made were to do a DebugTrace so I could see what was happening. That's how I found out that the CHKDSK was  not loading. I attempted executing the CHKDSK command in a CMD window to make sure it was valid and that's how I found out about the execution level error.

When I'm doing debugging I do remove the ERRORMODE(@Off). I put it in because another issue cropped up about the same time the CHKDSK portion of the script failed. The WinIconize("Ecco...") stopped working. It indicates that it can't find the window even thought it's there and shows up in a WinItemize. I've tried coding it as a wild card (Ecco~) and using the full window name from the WinItemize list. It still says it doesn't exist.

Deana

By default the Compiler gives your compiled EXEs the default Manifest setting Requested Execution Level = AsInvoker and UI Access = False.

In order to run a compiled WinBatch EXE, that contains Control Manager Extender functions, on a Windows System with UAC enabled requires:

  • Manifest  uiAccess set  to TRUE
  • EXE is 'Signed'
  • EXE is installed in or under the "Program Files" folder or in the "Windows\System32" folder.

This leads me to believe that you previously had UAC disabled on the system. When it was re-enabled you would begin to see issues with this script because of where it is placed and its contents ( control manager code).

Is it possible you had UAC disabled previously?

Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

I haven't touched UAC since receiving this computer. Unless a Windows update did something with it I'm not sure what could have been changed.

Deana

Well lets have you run that code using DebugTrace, Compiled with Requested Execution Level = AsInvoker and UI Access = False and the ErrorMode REMOVED. Post the DebugTrace output.
Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

Okay it appears that the chkdsk requires elevated permissions and elevated apps cannot be run at startup. Since it appears that you only want to run it occasionally, I recommend setting up a system task to handle the chkdsk.

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/Win7+Run~Chkdsk~at~Start~Up.txt
Deana F.
Technical Support
Wilson WindowWare Inc.

radioman54

I setup a scheduled task to run chkdsk as a local service. It didn't run. The last run result shows (0x3).

td

System error 3 is "The system cannot find the path specified."
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

radioman54

The path is:  C:\Windows\System32\chkdsk.exe. That's the correct path to chkdsk.

Deana

Quote from: radioman54 on February 21, 2014, 07:04:44 AM
The path is:  C:\Windows\System32\chkdsk.exe. That's the correct path to chkdsk.

Chkdsk requires that you answer a prompt. Hence the code "echo y" being sent in your previous command line.

You will need to make sure your scheduled task is set up correctly.
Quote
General | User Account = SYSTEM
Trigger  | Weekly @ 11:59 PM every Tues
Action   | Program = C:\windows\System32\cmd.exe
              Arguments  = /c echo y|chkdsk c: /f
Settings | Checked = Allow task to be run on demand.

IMPORTANT: ChkDsk will NOT run until next restart! If you want it to reboot then you will need to create a script using ShellExecute, and IntControl 67. Compile AsInvoker.

An easy way to see if the volume is marked dirty is to open a command prompt as admin and type
Quotechkntfs c:

After you have confirmed that it is marked dirty and you want to cancel it type:
Quotechkntfs /x c:

When I run the task as indicated above, then run
Quotechkntfs c:
. It states that the volume is marked dirty and will run on restart.

I did notice that when viewing the task history it does state
QuoteTask Scheduler successfully completed task "\ChkDisk - Weekly" , instance "{23bcdc81-f4e0-4932-bc60-1b363d4d7cfc}" , action "c:\windows\system32\cmd.exe" with return code 3.

Can't explain why this is. However it seems to successfully instigate the chkdsk on restart.
Deana F.
Technical Support
Wilson WindowWare Inc.

td

Quote from: radioman54 on February 21, 2014, 07:04:44 AM
The path is:  C:\Windows\System32\chkdsk.exe. That's the correct path to chkdsk.

If you are starting chkdsk directly from the task scheduler and if the task scheduler is returning the actual chkdsk return code then return code 3 indicates one of the following possible errors, "Could not check the disk, errors could not be fixed, or errors were not fixed because /f was not specified."*

(*Based on MSFT documentation)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

radioman54

I have it set to run in the morning before my system reboots after backups. Will let you know.