PHP to run VBS script on server

Hi,

I have a vbs script that currently reads a txt file for a computer name. For each computer name it audits the PC and gets the software installed and outputs it to a csv file. This works perfectly, however when I try and execute the script from PHP using
echo shell_exec("cmd /c wscript C:\test\text.vbs");
It doesnt output to the excel file. Both the vbs and output csv file are outputted to the webserver running Apache/PHP

Here is the code of the vbs

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("C:\test\servers.txt", ForReading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    'Wscript.Echo "Server name: " & arrServiceList(0)
    
   strIPvalue = arrServiceList(0)
   	

Const HKEY_LOCAL_MACHINE = &H80000002
'CALL GenerateReport(strIPvalue)


'=================================================================================
'SUB-ROUTINE GenerateReport
'SUB GenerateReport(strIPvalue)

'Script to change a filename using timestamps
strPath = "\\server\software\" 'Change the path to appropriate value
strMonth = DatePart("m", Now())
strDay = DatePart("d",Now())

if Len(strMonth)=1 then
strMonth = "0" & strMonth
else
strMonth = strMonth
end if


if Len(strDay)=1 then
strDay = "0" & strDay
else
strDay = strDay
end if


strFileName = DatePart("yyyy",Now()) & "."
strFileName = Replace(strFileName,":","")
'=================================================================================

'Variable Declarations
Const ForAppending = 8

'===============================================================================
'Main Body
On Error Resume Next


'CompName
strComputer = strIPvalue
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'===============================================================================

'================================================================

'Get Operation System & Processor Information
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor",,48)
For Each objItem in colItems
CompName = objItem.SystemName
Next

Set objFSO2 = CreateObject("Scripting.FileSystemObject")
if objFSO2.FileExists(strPath & CompName & ".csv") then
WScript.Quit
end if

'Set the file location to collect the data
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objTextFile2 = objFSO2.OpenTextFile(strPath & CompName & ".csv", ForAppending, True)

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys
strSubKeyPath = strKeyPath & "\" & subkey

strString = "DisplayName"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strSubKeyPath, strString, strDisplayName

strString = "DisplayVersion"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strSubKeyPath, strString, strDisplayVersion

strDisplayName=Trim(strDisplayName)
strDisplayVersion=Trim(strDisplayVersion)
If strDisplayName <> "" And strDisplayVersion <> "" Then
objTextFile2.Write " " & strDisplayName & " " & strDisplayVersion & VBCRLF
End If
Next

strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
objReg.EnumKey HKEY_LOCAL_USER, strKeyPath, arrSubKeys

For Each subkey In arrSubKeys
strSubKeyPath = strKeyPath & "\" & subkey

strString = "DisplayName"
objReg.GetStringValue HKEY_LOCAL_USER, strSubKeyPath, strString, strDisplayName

strString = "DisplayVersion"
objReg.GetStringValue HKEY_LOCAL_USER, strSubKeyPath, strString, strDisplayVersion

strDisplayName=Trim(strDisplayName)
strDisplayVersion=Trim(strDisplayVersion)
If strDisplayName <> "" And strDisplayVersion <> "" Then
objTextFile2.Write " " & strDisplayName & " " & strDisplayVersion & VBCRLF
End If
Next


'===========================================
'Close text file after writing logs
objTextFile2.Close

'Clean Up

SET colIESettings=NOTHING
SET colItems=NOTHING
SET colSettings=NOTHING
SET colDisks=NOTHING
SET AdapterSet=NOTHING
SET objWMIService=NOTHING
SET objWMIService2=NOTHING
SET objFSO2=NOTHING
SET objTextFile2=NOTHING


'===================================================================
'END SUB


Loop

Open in new window

kev8326Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

x-menIT super heroCommented:
Why not:
("cmd /c cscript C:\test\text.vbs")
0
kev8326Author Commented:
Hi,

I tired this shell_exec("cmd /c cscript C:\\test\\text.vbs");

However it only created a file .csv in the directory instead of a file with the name of the computer i.e. computer1.csv. So still not quite working.

Thanks
0
x-menIT super heroCommented:
have tryen to output the   srting :

strPath & CompName & ".csv"

to see if its the right path and file name ?
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

kev8326Author Commented:
Outputting the string strPath & CompName & ".csv" works fine, which is the case if I double click and run the script. Im not sure how I can test that in a browser though. The script only doenst work when I try to run it from PHP
0
kev8326Author Commented:
Hi

I've not got the script to create the file with the computer name, however the contents are empty!?

If i run the script through a browser using the call as above comments the file is created but the contents are empty, however if i double click on the vbs script the file contains all software installed in a list. I dont understand why it works through doubl clicking on the script but not when passing through a browser?

Thanks
0
x-menIT super heroCommented:
sorry, no PHP knowledge here...
0
webwyzsystemsCommented:
The apache user has no privileges to execute shell scripts in that particular location. This is for safety sake.

You can execute it locally, because you are executing it from the console.When the apache process tries to execute the script - it is denied from running some portion or another.

Be very careful if you plan to edit permissions to allow this behaviour. It can be extremely unsafe.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.