Sabitha Abraham's profileSaby's spacePhotosBlogLists Tools Help
There are no photo albums.

Saby's space

December 14

Create share, add permission to a user to share folder in c#

//Creating the share folder and add a user with permission to a share folder

 

System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\"MachineName + @"\root\cimv2");

scope.Connect();

LogInfo("Creating fileShare..");

ManagementClass managementClass = new ManagementClass(scope, new ManagementPath("Win32_Share"), null);

ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");

inParams["Description"] = shareName;

inParams["Name"] = shareName;

inParams["Path"] = sharePath;

inParams["Type"] = 0x0;

inParams["Access"] = SecurityDescriptor;  // This is where we add the user with permission. I have shown 2 ways of creating this object below 

inParams = managementClass.InvokeMethod("Create", inParams, null);

 

 

//  Getting SecurityDescriptor from the share itself.

System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\"MachineName + @"\root\cimv2");

scope.Connect();

ManagementObject Win32LogicalSecuritySetting = new ManagementObject(string.Format "{0}:Win32_LogicalShareSecuritySetting.Name='{1}'", scopeString,shareName));

 

ManagementBaseObject Return = Win32LogicalSecuritySetting.InvokeMethod("GetSecurityDescriptor", null, null);

Int32 ReturnValue = Convert.ToInt32(Return.Properties["ReturnValue"].Value);

if (ReturnValue != 0)

    throw new Exception(String.Format("Error when calling GetSecurityDescriptor. Error code : {0}.", ReturnValue));

SecurityDescriptor = Return.Properties["Descriptor"].Value as ManagementBaseObject;

 

 

// Creating a new securty description for a new user

ManagementObject Trustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null);

Trustee["Domain"] = domainName;

Trustee["Name"] = userName;

 

ManagementObject AdminACE = new ManagementClass(new ManagementPath("Win32_Ace"), null);

AdminACE["AccessMask"] = 2032127;

AdminACE["AceFlags"] = 3;

AdminACE["AceType"] = 0;

AdminACE["Trustee"] = Trustee;

 

ManagementObject SecurityDescriptor= new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null);

SecurityDescriptor ["ControlFlags"] = 4; //SE_DACL_PRESENT

SecurityDescriptor ["DACL"] = new object[] { AdminACE };

 

Delete share or UNC c# or stop sharing share folder

//Delete a share or UNC in  c#            or             stop sharing a share folder

 

System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\machineName\\root\cimv2");

scope.Connect();

ObjectQuery oQuery = new ObjectQuery("SELECT * FROM Win32_Share WHERE Name = 'FileShare'");

ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(scope, oQuery);

ManagementObjectCollection shares = oSearcher.Get();

ManagementObjectCollection.ManagementObjectEnumerator sharedEnum = shares.GetEnumerator();

if (sharedEnum.MoveNext())

{

ManagementObject share = (ManagementObject)sharedEnum.Current;

if (share != null) share.Delete();

}

else

{

throw new HATestException("FileShare is not found..");

}  

September 12

Disable or Enable network card NIC in powershell

This is my favorate script.
 
$adapter=Get-wmiobject win32_NetworkAdapter | where {$_.AdapterType -like 'Ethernet*'} | where { $_.PhysicalAdapter -eq 'True'}  | where { $_.NetEnabled -eq 'True' }; sleep 3; foreach ($adp in $adapter) {$adp.disable()}  sleep 10; foreach ($adp in $adapter) {$adp.Enable()}
 
This will identify the NIC, disable the NIC, sleep for some time and Enable back. So you can let the box disconnect for a while and connect it back throught this.
 
 

Redirecting to file from powershell command output

Redirect to a file
powershellcommand > outputfile.txt
 
To append
powershellcommand >> outputfile.txt
 
 
Forloop or while loop result we might want to assign it to some variable na dprint it to a file
 
for(1...100) {$result = Get-DatabaseCopyStatus DatabaseCopy150522 -mailboxServer EXCH-B-204 | fl  *LastLogGenerated*, *LastLogInspected
*, *LastLogCopyNotified*, *CopyQueueLength*, *LogCopyQueueIncreasing* ; echo $result >> D:\CopyQ.txt; date >> D:\CopyQ.txt; sleep 1; }
July 22

Automatic properties in C# 3.0

 

The new C# compiler that ships in "Orcas" provides an elegant way to make your code more concise while still retaining the flexibility of properties using a new language feature called "automatic properties".  Automatic properties allow you to avoid having to manually declare a private field and write the get/set logic -- instead the compiler can automate creating the private field and the default get/set operations for you.  If we want to write a default getter and setter method for any variable then we could use the below shortcut.

public string MyProperty { get; set; }

So, This will be replaced with the following

private string myProperty;
public string MyProperty
{
get{return myProperty;}
set{myProperty = value;}
}

 

Sabitha Abraham

Occupation
Location
Interests
My new home page http://sabyshome.spaces.live.com/