예제 #1
0
        /// <summary>
        /// Starts a job as defined by the contained ScheduledJobDefinition object.
        /// </summary>
        public override void StartJob()
        {
            lock (SyncRoot)
            {
                if (_job != null && !IsFinishedState(_job.JobStateInfo.State))
                {
                    string msg = StringUtil.Format(ScheduledJobErrorStrings.JobAlreadyRunning, _jobDefinition.Name);
                    throw new PSInvalidOperationException(msg);
                }

                _statusInfo   = null;
                _asyncJobStop = false;
                PSBeginTime   = DateTime.Now;

                if (_powerShell == null)
                {
                    InitialSessionState iss = InitialSessionState.CreateDefault2();
                    iss.Commands.Clear();
                    iss.Formats.Clear();
                    iss.Commands.Add(
                        new SessionStateCmdletEntry("Start-Job", typeof(Microsoft.PowerShell.Commands.StartJobCommand), null));

                    // Get the default host from the default runspace.
                    _host     = GetDefaultHost();
                    _runspace = RunspaceFactory.CreateRunspace(_host, iss);
                    _runspace.Open();
                    _powerShell          = System.Management.Automation.PowerShell.Create();
                    _powerShell.Runspace = _runspace;

                    // Indicate SetShouldExit to host.
                    AddSetShouldExitToHost();
                }
                else
                {
                    _powerShell.Commands.Clear();
                }

                _job = StartJobCommand(_powerShell);

                _job.StateChanged += new EventHandler <JobStateEventArgs>(HandleJobStateChanged);
                SetJobState(_job.JobStateInfo.State);

                // Add all child jobs to this object's list so that
                // the user and Receive-Job can retrieve results.
                foreach (Job childJob in _job.ChildJobs)
                {
                    this.ChildJobs.Add(childJob);
                }

                // Add this job to the local repository.
                ScheduledJobSourceAdapter.AddToRepository(this);
            } // lock
        }
예제 #2
0
 public override void StartJob()
 {
     lock (base.SyncRoot)
     {
         if (this._job == null || this.IsFinishedState(this._job.JobStateInfo.State))
         {
             this._statusInfo   = null;
             this._asyncJobStop = false;
             base.PSBeginTime   = new DateTime?(DateTime.Now);
             if (this._powerShell != null)
             {
                 this._powerShell.Commands.Clear();
             }
             else
             {
                 InitialSessionState initialSessionState = InitialSessionState.CreateDefault2();
                 initialSessionState.Commands.Clear();
                 initialSessionState.Formats.Clear();
                 initialSessionState.Commands.Add(new SessionStateCmdletEntry("Start-Job", typeof(StartJobCommand), null));
                 this._host     = this.GetDefaultHost();
                 this._runspace = RunspaceFactory.CreateRunspace(this._host, initialSessionState);
                 this._runspace.Open();
                 this._powerShell          = System.Management.Automation.PowerShell.Create();
                 this._powerShell.Runspace = this._runspace;
                 this.AddSetShouldExitToHost();
             }
             this._job = this.StartJobCommand(this._powerShell);
             this._job.StateChanged += new EventHandler <JobStateEventArgs>(this.HandleJobStateChanged);
             base.SetJobState(this._job.JobStateInfo.State);
             foreach (Job childJob in this._job.ChildJobs)
             {
                 base.ChildJobs.Add(childJob);
             }
             ScheduledJobSourceAdapter.AddToRepository(this);
         }
         else
         {
             string str = StringUtil.Format(ScheduledJobErrorStrings.JobAlreadyRunning, this._jobDefinition.Name);
             throw new PSInvalidOperationException(str);
         }
     }
 }