1

I am trying to make a plugin to a website that would show the server's CPU load, disk space, free memory etc.

I did a research and in most of the cases they suggested using wmic for that as it allows remote connection what I'll need.

I'm using the following code to get the CPU load in percentage:

<cfexecute name="C:/Windows/system32/wbem/wmic.exe" 
      arguments="cpu get LoadPercentage /format:csv" 
      timeout=100 />

When I load the site it times out. I've checked the server and the process is still running. Is there a way to make it work?

Commands from Server

Update: I've tried running WMIC as a different user, to make sure the problem is not coming from ColdFusion having insufficient rights. The code I'm using is the following:

<cfset Args='/user:*username* "C:\Windows\system32\wbem\wmic.exe cpu get LoadPercentage" | F:\Sanur\sanur.exe *password*'>

<cfdump var="#Args#">

<br />

<cfexecute
    name="C:\Windows\system32\runas.exe"
    arguments=#Args#
    variable="result"
    timeout=100
></cfexecute>

<cfdump var="#result#">

In this case it returns an [empty string] and does not start wmic.exe (checking task manager). If I try it without sanur I get the response below. I suspect the problem is using 2 commands in cfexecute arguments.

Enter the password for username:

Grabofus
  • 1,924
  • 14
  • 17
  • Are you definitely on a server OS? There are known issues with `/format:csv` in Windows 7 if you are not using US English locale. Have you tried running that exact command at a command prompt on the server to check whether it terminates with success, terminates with error, or fails to terminate? What version of CF are you on? Earlier versions of cfexecute before a CF8 patch did not capture stderr output, so were prone to hanging when the executed process was trying to return error text. The workaround in that case would be `cmd.exe "/c wmic.exe cpu get LoadPercentage /format:csv 2>&1"` – Sev Roberts Nov 12 '15 at 16:08
  • Yes, @ BaloghGábor - Take another look at the cfexecute example I linked on your other thread http://stackoverflow.com/a/1002585/104223. It uses a java class, but same principle for an exe, ie You need to use both the "/c" and "2>&1" flags. – Leigh Nov 12 '15 at 16:16
  • @Leigh - I've tried it the following way '' as Sev and you suggested. Am I doing something wrong? – Grabofus Nov 12 '15 at 16:37
  • @SevRoberts I am running it on Windows 2003 Server at my company. I believe we have ColdFusion MX 6.1 installed from 2007. First I tried opening and running the same command on server. So it should work. – Grabofus Nov 12 '15 at 16:39
  • @BaloghGábor - 1) Use a full path for cmd.exe (Actually, always use full paths for everything to avoid problems) 2) Are you capturing the output? I do not see a "variable" in there. Also does your version support an error variable/file? 3) When you say you ran it at the command line, did you copy and paste the *exact same* command string from the cfexecute "arguments"? 4) Does `wmic.exe` need any special permissions? Reason for asking is you are probably running it under a user account, while the CF Server runs under a limited system account by default.would have – Leigh Nov 12 '15 at 17:00
  • 1
    The example you gave in your latest comment - if I add a variable attribute and dump the result - returns for me on CF9 on Windows Server 2008 R2, but that's the oldest CF environment I have access to now. On that environment I get the well known `Invalid XSL format (or) file name` error returned unless I remove `/format:csv` - but that's not a CF issue. Your issue could be with the ancient version of CF server, or with Windows security permissions. – Sev Roberts Nov 12 '15 at 17:04
  • I've tried it with full path for cmd as : C:/Windows/system32/cmd.exe. Now it shows : "The syntax of the command is incorrect. ". I'm trying the exact same code (just uploaded an image of it, please find it at my question). I'm not quite sure what permission might it need, but again it starts wmic.exe ( I can see it in task manager on server ) it just seems like it doesn't stop, therefore not ending cfexecute. Errorvariable is not supported in my version. – Grabofus Nov 12 '15 at 17:07
  • @Leigh So right now the command is the following: nothing happens on the site, it keeps loading and loading until time out. I don't have a variable as I've read somewhere that if I don't have a variable and output then it automatically outputs it on the page. – Grabofus Nov 12 '15 at 17:20
  • Are you looking to get information about the server on which CF is running? – Dan Bracuk Nov 12 '15 at 17:23
  • @DanBracuk Well as a first step I want to make this one work. After this one is working the next step will be to include a /node:[server] in the command and put multiple modules on the site monitoring all main servers at the company. I didn't want to include it for now to have less source of error. – Grabofus Nov 12 '15 at 17:25
  • @BaloghGábor - You definitely need to use either "variable" or "outputfile" (I think that is the name). When it times out, do you see the process in the Task Manager? – Leigh Nov 12 '15 at 18:25
  • (Edit) Also, like I mentioned earlier, it might be a permissions issue. [This link](https://support.microsoft.com/en-us/kb/290216) says *"Wmic.exe can only be used by the local system administrators regardless of WMI namespace permissions on the local machine."*. The CF service runs under a limited "system" account by default. You probably ran the command line test under a user account with higher permissions. If it is permissions problem, you may need to run the CF service under a custom account with special permissions. Though do NOT just use the Administrator account .. obviously ;-). – Leigh Nov 12 '15 at 19:20
  • Sorry for the late reply... Yeah it is possible that it doesn't have permission to run it. Do you think it is possible to run just the command as a different user? The reason is im not an administrator, just a developer and I don't have permissions to change any system settings. – Grabofus Nov 13 '15 at 07:59
  • I asked the question too early.. After a bit of search I've found psexec, that might do the trick. I'll update as soon as I can try it. – Grabofus Nov 13 '15 at 08:12
  • @Leigh I couldn't solve the problem. I've created a batch file to make it easier as my code was getting longer and longer. I used the command RunAs to run WMIC with my user. I also had to us sAnuR to enter my password at the same line. It returns an empty string probably because runas opens the wanted application in a new cmd. I've also tried managing everything without a batch file but I couldn't get ColdFusion to accept 2 programs in cfexecute. – Grabofus Nov 13 '15 at 17:31
  • My batch file contains (and works outside of coldfusion) C:\Windows\system32\runas.exe /user:domain\username "C:\Windows\system32\wbem\wmic.exe cpu get LoadPercentage" | *fullpath*\sanur.exe /i password.txt – Grabofus Nov 13 '15 at 17:31
  • My code looks like the following (returns [empty string]): Is it even possible to use pipe in cfexecute? – Grabofus Nov 13 '15 at 17:34
  • A) Can you please move all the code above into your question? Not everyone reads comments (and it is a little hard to read unformatted) B) Not sure I understand this part *"My batch file contains ....My code looks like the following"*. If you moved the commands into a .bat file, why is the cfexecute not running the .bat file? – Leigh Nov 13 '15 at 23:31
  • @Leigh I've added the code into my question. I've tried it two different ways. One with batch file, one with only the code. At the end I decided to use only code. As if I execute runas in a batch file it opens an additional cmd, what will contain the result what I need. (the command line coldfusion executes remains empty) – Grabofus Nov 16 '15 at 09:44
  • Unfortunately sanur.exe does not run on win7, so I cannot really help with that part. What happened when you looked into psexec? – Leigh Nov 16 '15 at 14:14
  • Well, first I thought psexec is something build into windows.. sanur was easy to set up, as its just an exe file you need to copy. I gave up on it and spend my day to just write a short C# application what seem to do the trick for me. So the issue is solved for me although with a workaround.. It is going to be limited but I only need a couple specs so enough for me. :-) Thank you for the help! – Grabofus Nov 16 '15 at 16:04
  • Glad you found a solution. Consider posting that as a answer (with the relevant snippets of the final code) to help the next guy with this question. – Leigh Nov 17 '15 at 14:00

0 Answers0