Reputation: 95
I'm trying to parse some parameters to a script I'm running in powershell. But I'm getting errors while executing the script saying they are empty.
Im starting the process like this:
string input1 = "Powershell.exe -File " + Config.Directory + "/ICS.ps1" + " -toggle '" + toggle + "' -par1 '" +
par1 + "' -par2 '" + par2 + "' -connectionInterface '" + connectionInterface + "'";
//(for readability) input1 will look something like this:
//Powershell.exe -File map/to/ICS.ps1 -toggle 'EnableSharing' -par1 '0' -par2 '1' -connectionInterface 'Ethernet'
string[] output = CmdProcess.Exec(input1);
CmdProcess.Exec
is a method i created to quickly run a cmd process.
it works like this
public static string[] Exec(params string[] parameters)
{
Process cmd = new Process
{
StartInfo =
{
UseShellExecute = false,
Verb = "runas",
FileName = @"cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
}
};
cmd.Start();
foreach (string parameter in parameters)
{
cmd.StandardInput.WriteLine(parameter);
}
cmd.StandardInput.WriteLine("exit");
return Regex.Split(cmd.StandardOutput.ReadToEnd(), "\r\n");
}
the reason i use cmd to execute powershell is because i tried it with a powershell instance* but that crashed for an unknown reason on really fast computers (while it runs normally if you run it manually).
*System.Management.Automation.Powershell
in the script i wrote this: (At the beginning of the script)
Param( [string]$toggle, [string]$par1, [string]$par2, [string]$connectionInterface )
But it gives an error the params are null :
You cannot call a method on a null-valued expression.
At C:\Users\Jeremy\AppData\Roaming\HotspotLauncher\ICS.ps1:10 char:1
+ $config.$toggle($par1)
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I get error's like this with all the params. not just $toggle
or $par1
.
Powershell.exe
and -File .....
seperatly but that caused my thread to hang.
toggle
, par1
and the rest (In the C# code) arent null themselves
[CmdletBinding()]
before the params
in the script but that did nothing :
[CmdletBinding()]Param( [string]$toggle, [string]$par1, [string]$par2, [string]$connectionInterface )
[CmdletBinding()]Param( [string]$toggle, [string]$par1, [string]$par2, [string]$connectionInterface )
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
$config2.$toggle($par2)
This script enables/disables Internet sharing for the $connectionInterface
which could be something like Ethernet or Wifi, and set it to public ($par1
) / or nothing if it has to be disabled.
and does the same for microsoft hostednetwork and set it to private ($par2
).
Upvotes: 1
Views: 1113
Reputation: 174990
Don't fork a new process with powershell.exe
- use the API instead:
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;
//...
using(PowerShell ps = PowerShell.Create())
{
ps.AddScript(Path.Combine(Config.Directory, "ICS.ps1"))
.AddParameter("toggle", toggle)
.AddParameter("par1", par1)
.AddParameter("par2", par2)
.AddParameter("connectionInterface", connectionInterface);
Collection<PSObject> results = ps.Invoke();
}
Remember to add a reference to the System.Management.Automation.dll
assembly to your project
Upvotes: 1