我有以下PowerShell腳本,我想從我的C#應用程序中運行。如何從C#運行PowerShell代碼?
$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)
}
這是我試過到目前爲止,使用「使用System.Management.Automation ;,」,但劇本不工作。有人能指引我朝着正確的方向嗎?
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)}");
是的,請參閱下面的例外。 –
「System.Management.Automation.ParameterBindingException」類型的未處理異常可能發生在System.Management.Automation.dll –
「Write-Host」中,並且ps.Invoke – Mardoxx