2017-01-25 20 views
1

私はpowershellで実行しているスクリプトにいくつかのパラメータを解析しようとしています。 しかし、スクリプトを実行中にエラーが発生して、空であると言います。このようにプロセスを開始するパラメータをpowershellに渡すときに "ヌル値の式でメソッドを呼び出すことはできません"

イム:

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は、私はすぐにCMDプロセスを実行するために作成する方法です。 それは私が* PowerShellのインスタンスでそれを試みたが、あなたはそれを実行した場合、それは正常に実行されている間それは本当に速いコンピュータ(上の不明な理由でクラッシュしたので、私はPowerShellを実行するためにcmdを使う理由は、この

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"); 
    } 

のように動作します手動で)。 * System.Management.Automation.Powershell

私がこれを書いたスクリプトで

Param([string]$toggle, [string]$par1, [string]$par2, [string]$connectionInterface) 

(スクリプトの先頭で)しかし、それはエラーを与えるparamsが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 

私はエラーのようなを取得しますこれはすべてのparamsで。 $toggleまたは$par1だけではありません。


Powershell.exe-File .....をseperatlyで実行しようとしましたが、スレッドがハングする原因となりました。
また、私は、私はまた、スクリプトで params[CmdletBinding()]を追加しようとしたが、それは何もしなかった
自分自身を、 toggle 100%確信している par1及び(C#のコードで)残りはヌルアレント:

[CmdletBinding()]Param([string]$toggle, [string]$par1, [string]$par2, [string]$connectionInterface) 
ここで


は、PowerShellスクリプトです:

[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) 

このスクリプトは、有効/無効にします$connectionInterfaceのternet共有は、イーサネットや無線LANのようなもので、無効にする必要がある場合はpublic($par1)に設定します。 と同じものをmicrosoft hostednetworkに対して実行し、プライベート($par2)に設定します。

答えて

1

powershell.exeで新しいプロセスをforkしないでください - 代わりにthe APIを使用します。

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(); 
} 

は、私はちょうどそれを試してみました

+0

プロジェクトにSystem.Management.Automation.dllアセンブリへの参照を追加することを忘れないでください、私はまだ取得しますそれらがnullであるというエラー。私は 'toggle'と' par1'と 'par2'がヌルではないのかどうか再度調べましたが、うまくいきました。 – Jeremy

+0

@Jeremy PowerShellスクリプト自体を質問に投稿できますか? –

+2

@Jeremyあなたの問題は、 '$ config'が' $ null'であると思われます。 '$ m.INetSharingConfigurationForINetConnection.Invoke($ c)'は何も返しません –

関連する問題