@ -182,13 +182,19 @@ namespace NTFSSecurity
#endregion
#endregion
}
}
public class BaseCmdletWithPrivControl : BaseCmdlet
public class BaseCmdletWithPrivControl : BaseCmdlet , IDisposable
{
{
protected PrivilegeAndAttributesCollection privileges = null ;
protected PrivilegeAndAttributesCollection privileges = null ;
protected PrivilegeControl privControl = new PrivilegeControl ( ) ;
protected PrivilegeControl privControl = new PrivilegeControl ( ) ;
private List < string > enabledPrivileges = new List < string > ( ) ;
private List < string > enabledPrivileges = new List < string > ( ) ;
Hashtable privateData = null ;
Hashtable privateData = null ;
// A cmdlet that enables the privileges for the session, such as Enable-Privileges, keeps them enabled.
protected virtual bool KeepEnabledPrivileges
{
get { return false ; }
}
protected override void BeginProcessing ( )
protected override void BeginProcessing ( )
{
{
privateData = ( Hashtable ) MyInvocation . MyCommand . Module . PrivateData ;
privateData = ( Hashtable ) MyInvocation . MyCommand . Module . PrivateData ;
@ -208,15 +214,40 @@ namespace NTFSSecurity
//disable all privileges that have been enabled by this cmdlet
//disable all privileges that have been enabled by this cmdlet
WriteVerbose ( string . Format ( "Disabeling all {0} enabled privileges..." , enabledPrivileges . Count ) ) ;
WriteVerbose ( string . Format ( "Disabeling all {0} enabled privileges..." , enabledPrivileges . Count ) ) ;
foreach ( var privilege in enabledPrivileges )
foreach ( var privilege in Disabl eE nabledPrivileges( ) )
{
{
DisablePrivilege ( ( Privilege ) Enum . Parse ( typeof ( Privilege ) , privilege ) ) ;
WriteVerbose ( string . Format ( "\t{0} disabled" , privilege ) ) ;
WriteVerbose ( string . Format ( "\t{0} disabled" , privilege ) ) ;
}
}
WriteVerbose ( string . Format ( "...finished" ) ) ;
WriteVerbose ( string . Format ( "...finished" ) ) ;
}
}
}
}
// PowerShell calls Dispose also when a later command or a terminating error stops the pipeline, and then
// skips EndProcessing. Before 5.0.0-rc6, the privileges that the cmdlet had enabled stayed enabled in the
// session in that case.
public void Dispose ( )
{
DisableEnabledPrivileges ( ) ;
GC . SuppressFinalize ( this ) ;
}
// Disables the privileges that this cmdlet enabled, once, and returns their names.
private List < string > DisableEnabledPrivileges ( )
{
var disabled = new List < string > ( ) ;
if ( ! KeepEnabledPrivileges )
{
foreach ( var privilege in enabledPrivileges )
{
DisablePrivilege ( ( Privilege ) Enum . Parse ( typeof ( Privilege ) , privilege ) ) ;
disabled . Add ( privilege ) ;
}
}
enabledPrivileges . Clear ( ) ;
return disabled ;
}
protected void EnablePrivilege ( Privilege privilege )
protected void EnablePrivilege ( Privilege privilege )
{
{
//throw an exception if the specified prililege is not held by the client
//throw an exception if the specified prililege is not held by the client