VBScript can work in its own console application but cannot be called in a service?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheTruthWhatsUp
    New Member
    • Jul 2010
    • 1

    VBScript can work in its own console application but cannot be called in a service?

    The following VBScript code runs smoothly when I use cscript and/or VB console applications:

    Code:
    Set wmiServices = GetObject("winmgmts:root/default") 
    Set colTreeChanges = wmiServices.ExecNotificationQuery _
        ("SELECT * FROM RegistryTreeChangeEvent " _
        & "WHERE Hive='HKEY_LOCAL_MACHINE' " _
        & "AND RootPath='SOFTWARE\\Microsoft'")
    
    While (True)
       Set TreeChange = colTreeChanges.NextEvent
       TreeChange.GetObjectText_()
       Wscript.Echo  "Hive = " & TreeChange.Hive & VBNewLine _
          & "RootPath = "& TreeChange.RootPath _
          & TreeChange.GetObjectText_()      
    Wend
    I plan to call this code into my windows service application, it currently has the following code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Management;
    using System.ServiceProcess;
    using System.Text;
    using System.Timers;
    
    namespace SoftwareDetectionService
    {
        public partial class SoftwareDetectionService : ServiceBase
        {
    
            public SoftwareDetectionService()
            {
                InitializeComponent();
                if (!System.Diagnostics.EventLog.SourceExists("MySource"))
                {
                    System.Diagnostics.EventLog.CreateEventSource(
                       "MySource", "MyNewLog");
                }
                eventLog1.Source = "MySource";
                eventLog1.Log = "MyNewLog";
            }
    
            protected override void OnStart(string[] args)
            {
                eventLog1.WriteEntry("Service has started");
                Process scriptproc = new Process();
                scriptproc.StartInfo.RedirectStandardOutput = true;
                scriptproc.StartInfo.UseShellExecute = false;
                scriptproc.StartInfo.FileName = @"cscript";
                scriptproc.StartInfo.Arguments = @"c:\Users\081254X\Documents\code11.vbs";
                scriptproc.Start();
                StreamReader SR = scriptproc.StandardOutput;
                string result = SR.ReadToEnd();
                eventLog1.WriteEntry(result);
                scriptproc.WaitForExit();
                SR.Close();
                scriptproc.Close();
            }
    
            protected override void OnStop()
            {
                eventLog1.WriteEntry("Service has ended");
            }
        }
    }
    For some odd reason, the VBScript code only works on cscript but when I call in the code to the service, it doesn't work... someone explain why?
  • anreinor
    New Member
    • Jun 2010
    • 4

    #2
    do you also get the message that access (to filesystem) is denied?

    Comment

    • !NoItAll
      Contributor
      • May 2006
      • 297

      #3
      I believe this is a security issue. Creating a service that can run VB Scripts smells like a Trojan to me...

      Why not put the functionality inside the service, or wrap it into a replaceable DLL?

      Comment

      • anreinor
        New Member
        • Jun 2010
        • 4

        #4
        try the following solution to access scripts from within your application:


        Is a FULL description of "how-to"! Very good documentation about using scripting-engines!

        Comment

        Working...