using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using WebsitePanel.Server.Utils;
namespace WebsitePanel.Providers.DNS
{
/// This class is a generic helper hosting the PowerShell runtime.
/// It's probably a good idea to move to some utility module.
public class PowerShellHelper: IDisposable
{
private static InitialSessionState s_session = null;
static PowerShellHelper()
{
s_session = InitialSessionState.CreateDefault();
// s_session.ImportPSModule( new string[] { "FileServerResourceManager" } );
}
public PowerShellHelper()
{
Log.WriteStart( "PowerShellHelper::ctor" );
Runspace rs = RunspaceFactory.CreateRunspace( s_session );
rs.Open();
// rs.SessionStateProxy.SetVariable( "ConfirmPreference", "none" );
this.runSpace = rs;
Log.WriteEnd( "PowerShellHelper::ctor" );
}
public void Dispose()
{
try
{
if( this.runSpace == null )
return;
if( this.runSpace.RunspaceStateInfo.State == RunspaceState.Opened )
this.runSpace.Close();
this.runSpace = null;
}
catch( Exception ex )
{
Log.WriteError( "Runspace error", ex );
}
}
public Runspace runSpace { get; private set; }
public Collection RunPipeline( params Command[] pipelineCommands )
{
Log.WriteStart( "ExecuteShellCommand" );
List