0

I have the following PowerShell script, that I want to run from within my C# application.

$adapters=(gwmi win32_networkadapterconfiguration ) 
Foreach ($adapter in $adapters){
Write-Host $adapter
  $adapter.settcpipnetbios(2)
}
$nics=([wmiclass]'Win32_NetworkAdapterConfiguration')
Foreach($nic in $nics){
 Write-Host $adapter
$nic.enablewins($false,$false)
}

This is what I tried so far, using the "using System.Management.Automation;," but the script is not working. Can someone point me in the right direction?

PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-Process");
ps.AddArgument("$adapters=(gwmi 
win32_networkadapterconfiguration )");
ps.AddArgument("Foreach($adapter in $adapters){");
ps.AddArgument(" Write - Host $adapter");
ps.AddArgument("$adapter $adapter.settcpipnetbios(2)}");
//WINS LMHOSTS lookup
ps.AddArgument("$nics = ([wmiclass]'Win32_NetworkAdapterConfiguration')");
ps.AddArgument("Foreach($nic in $nics){");
ps.AddArgument(" Write - Host $adapter");
ps.AddArgument("$nic.enablewins($false,$false)}");
6
  • Yes, see the exception below. Commented Apr 15, 2017 at 0:53
  • An unhandled exception of type 'System.Management.Automation.ParameterBindingException' occurred in System.Management.Automation.dll Commented Apr 15, 2017 at 0:53
  • "Write - Host" perhaps, and ps.Invoke Commented Apr 15, 2017 at 1:14
  • No, that did not work. Commented Apr 15, 2017 at 1:25
  • I just don't know why you are executing a powershell script from a c# app. You can use the same WMI commands inside of c# Commented Apr 16, 2017 at 0:27

2 Answers 2

1

It looks like you are missing a ps.Invoke(); at the end of your code. Or did you just leave that out of your listing?

You can find more information about the different ways to execute the PowerShell code in this blog post: https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/ (Section "Script/Command Execution:" and below.)

Sign up to request clarification or add additional context in comments.

6 Comments

Hey, Benjamin, the script seems to be running now, but I am getting the following exception "An unhandled exception of type 'System.Management.Automation.ParameterBindingException' occurred in System.Management.Automation.dll." Would you know what's wrong?
Have you tried to put your whole script into an AddScript call? I don't think you are supposed to use AddParameter the way you are doing it. This would only be needed if you wanted to input a dynamically generated parameter into your script. Instead you can just prepare the whole thing with one call to AddScript and invoke it.
Yes, I tried that, but am getting the same exception.
Hi Tony, could you please post more details from the exception?
Hey Benjamin see the details as requested below.
|
0

thanks for the help I found a solution. To make the script work, I had to structure the code as follow.

 //Disable NetBIOS over TCP/IP - 2=disable, 1=enable, 0=DHCP default
 //And WINS LMHOSTS lookup
 string script = @"
 $adapters=(gwmi win32_networkadapterconfiguration )
 Foreach($adapter in $adapters)
 {
    Write-Host $adapter
    $adapter.settcpipnetbios(2)
  }
  $nics=([wmiclass]'Win32_NetworkAdapterConfiguration')
  Foreach($nic in $nics){
  Write-Host $adapter
  $nic.enablewins($false,$false)
  }
  ";

  PowerShell powerShell = PowerShell.Create();
  powerShell.AddScript(script);
  powerShell.Invoke();

For those who don't know, this code will disable LMhosts Lookup and Disable NetBios over TCP/IP; remember to run it with administrative rights.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.