Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. public static void ToggleIcs(string connectionInterface, bool state)
  2. {
  3. string toggle;
  4. string par1;
  5. string par2;
  6. if (state){
  7. toggle = "EnableSharing";
  8. par1 = "0";
  9. par2 = "1";
  10. }else{
  11. toggle = "DisableSharing";
  12. par1 = "";
  13. par2 = "";
  14. }
  15. using (PowerShell powerShellInstance = PowerShell.Create())
  16. {
  17. // this script enables or disables internet sharing with the connectionInterface given.
  18. powerShellInstance.AddScript("" +
  19. "regsvr32 hnetcfg.dll /s;" +
  20. "$m = New-Object -ComObject HNetCfg.HNetShare;" +
  21. "$m.EnumEveryConnection |% { $m.NetConnectionProps.Invoke($_) };" +
  22. "$c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Name -eq '" + connectionInterface + "' };" +
  23. "$config = $m.INetSharingConfigurationForINetConnection.Invoke($c);" +
  24. "Write-Output $config.SharingEnabled;" +
  25. "Write-Output $config.SharingConnectionType;" +
  26. "$config." + toggle + "(" + par1 + ");" +
  27.  
  28. "$m2 = New-Object -ComObject HNetCfg.HNetShare;" +
  29. "$m2.EnumEveryConnection |% { $m2.NetConnectionProps.Invoke($_) };" +
  30. "$c2 = $m2.EnumEveryConnection |? { $m2.NetConnectionProps.Invoke($_).DeviceName -Match 'Microsoft Hosted Network Virtual Adapter' };" +
  31. "$config2 = $m2.INetSharingConfigurationForINetConnection.Invoke($c2);" +
  32. "Write-Output $config2.SharingEnabled;" +
  33. "Write-Output $config2.SharingConnectionType;" +
  34. "$config." + toggle + "(" + par2 + ");");
  35.  
  36.  
  37. PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
  38. IAsyncResult result = powerShellInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection);
  39. Console.WriteLine(DateTime.Now + ">> Started Powershell script");
  40.  
  41. int i = 0;
  42. while (result.IsCompleted == false)
  43. {
  44. if (i != 60)
  45. {
  46. Console.WriteLine(DateTime.Now + ">> Running script");
  47. }
  48. else
  49. {
  50. Console.WriteLine(DateTime.Now + ">> Script takes too long. Stopping script");
  51. powerShellInstance.Stop();
  52. }
  53. i++;
  54. Thread.Sleep(1000);
  55. }
  56.  
  57. Console.WriteLine(DateTime.Now + ">> Executed Internet Sharing script");
  58. }
  59. }
  60.  
  61. 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