예제 #1
0
        private static void DoWork(object data)
        {
            SrvLog.Write(string.Format("Thread Started: '{0}'", data.ToString()));

            string [] scriptsDir = Directory.GetDirectories((string)Globals.settings.ScriptsPath);
            if (scriptsDir.Length == 0)
            {
                SrvLog.Write(string.Format("Folder: '{0}' has no scripts", (string)Globals.settings.ScriptsPath));
            }

            foreach (string scriptDir in scriptsDir)
            {
                string scriptPath = Path.Combine(scriptDir, (string)Globals.settings.TargetScript);

                if (File.Exists(scriptPath))
                {
                    Thread scriptThread = new Thread(ScriptWorker);
                    scriptThread.IsBackground = true;
                    scriptThread.Start(scriptPath);
                }
                else
                {
                    SrvLog.Write("Script folder " + Directory.GetParent(scriptPath).Name + " doesn't contain  " + Path.GetFileName(scriptPath) + ". Ignored!");
                }
            }
        }
예제 #2
0
        private static void ScriptWorker(object data)
        {
            string scriptPath = (string)data;

            SrvLog.Write(string.Format("Script Started: '{0}'", Directory.GetParent(scriptPath).Name));

            // Create an AutoResetEvent to signal the timeout threshold in the
            // timer callback has been reached.
            var autoEvent = new AutoResetEvent(false);

            TimerCallback ScriptLauncher = new ScriptLauncher(scriptPath).RunScript;

            stateTimer = new Timer(ScriptLauncher, autoEvent, 1000, 60000); //1 seconds delay and every 1 minute

            // When autoEvent signals, dispose of the timer.
            autoEvent.WaitOne();
            stateTimer.Dispose();
        }
예제 #3
0
        // This method is called by the timer delegate.
        public void RunScript(Object stateInfo)
        {
            if (Monitor.TryEnter(lockObject))
            {
                try {
                    AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;

                    StringBuilder stringBuilder = new StringBuilder();

                    Runspace rs = RunspaceFactory.CreateRunspace();
                    rs.Open();

                    PowerShell ps = PowerShell.Create();
                    ps.Runspace = rs;

                    string scriptPolicy = "Unrestricted";
                    if ((bool)Globals.settings.SignedScripts)
                    {
                        scriptPolicy = "AllSigned";
                    }

                    ps.AddScript("Set-ExecutionPolicy -Scope Process -ExecutionPolicy " + scriptPolicy);
                    ps.Invoke();

                    ps.AddScript(string.Format("Get-AuthenticodeSignature \"{0}\"", scriptPath));

                    foreach (PSObject result in ps.Invoke())
                    {
                        if ((bool)Globals.settings.SignedScripts)
                        {
                            if (((Signature)result.BaseObject).Status != SignatureStatus.Valid)
                            {
                                SrvLog.Write("Script " + Directory.GetParent(scriptPath).Name + " Signature Error! Correct, and restart the service.");

                                //signal the waiting thread
                                autoEvent.Set();

                                break;
                            }
                        }

                        SrvLog.Write("Invoking: " + scriptPath);

                        Command myCommand = new Command(scriptPath);

                        // Pass -Automated switch and -CuttrentDateTimeUtc, as UTC ISO 8601 string
                        myCommand.Parameters.Add(new CommandParameter("Automated", true));
                        myCommand.Parameters.Add(new CommandParameter("CurrentDateTimeUtc", DateTime.UtcNow.ToString("o")));

                        ps.Commands.Commands.Add(myCommand);
                        ps.Invoke();

                        /*foreach (PSObject obj in ps.Invoke())
                         * {
                         *  SrvLog.Write(obj.ToString(), logPath);
                         * }*/


                        //signal the waiting thread
                        //autoEvent.Set();
                    }
                }
                catch (Exception ex) {
                    SrvLog.Write(ex.Message.ToString());
                }
                finally {
                    Monitor.Exit(lockObject);
                }
            }
        }
예제 #4
0
 protected override void OnStop()
 {
     SrvLog.Write((string)(Globals.settings.ServiceName) + " stopped");
 }
예제 #5
0
 protected override void OnStart(string[] args)
 {
     SrvLog.Write((string)(Globals.settings.ServiceName) + " started");
     srvThread.Start("PSScriptsService");
 }