Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void ToggleIcs(string connectionInterface, bool state)
- {
- string toggle;
- string par1;
- string par2;
- if (state){
- toggle = "EnableSharing";
- par1 = "0";
- par2 = "1";
- }else{
- toggle = "DisableSharing";
- par1 = "";
- par2 = "";
- }
- using (PowerShell powerShellInstance = PowerShell.Create())
- {
- // this script enables or disables internet sharing with the connectionInterface given.
- powerShellInstance.AddScript("" +
- "regsvr32 hnetcfg.dll /s;" +
- "$m = New-Object -ComObject HNetCfg.HNetShare;" +
- "$m.EnumEveryConnection |% { $m.NetConnectionProps.Invoke($_) };" +
- "$c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Name -eq '" + connectionInterface + "' };" +
- "$config = $m.INetSharingConfigurationForINetConnection.Invoke($c);" +
- "Write-Output $config.SharingEnabled;" +
- "Write-Output $config.SharingConnectionType;" +
- "$config." + toggle + "(" + par1 + ");" +
- "$m2 = New-Object -ComObject HNetCfg.HNetShare;" +
- "$m2.EnumEveryConnection |% { $m2.NetConnectionProps.Invoke($_) };" +
- "$c2 = $m2.EnumEveryConnection |? { $m2.NetConnectionProps.Invoke($_).DeviceName -Match 'Microsoft Hosted Network Virtual Adapter' };" +
- "$config2 = $m2.INetSharingConfigurationForINetConnection.Invoke($c2);" +
- "Write-Output $config2.SharingEnabled;" +
- "Write-Output $config2.SharingConnectionType;" +
- "$config." + toggle + "(" + par2 + ");");
- PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
- IAsyncResult result = powerShellInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection);
- Console.WriteLine(DateTime.Now + ">> Started Powershell script");
- int i = 0;
- while (result.IsCompleted == false)
- {
- if (i != 60)
- {
- Console.WriteLine(DateTime.Now + ">> Running script");
- }
- else
- {
- Console.WriteLine(DateTime.Now + ">> Script takes too long. Stopping script");
- powerShellInstance.Stop();
- }
- i++;
- Thread.Sleep(1000);
- }
- Console.WriteLine(DateTime.Now + ">> Executed Internet Sharing script");
- }
- }
- Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement