第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 C# 中獲取 PowerShell.Invoke() 輸出

在 C# 中獲取 PowerShell.Invoke() 輸出

C#
人到中年有點甜 2021-11-07 20:40:08
我有一個應用程序,它允許用戶在 Win 10 IoT 機器上配置基本的 WMI 設置。我目前正在努力閱讀所有啟用的 WEKF_PredefinedKey 設置。我只是在運行一個 skript,我將它作為字符串添加到名為 ReadEnabledKeys 的項目設置中:$CommonParams = @{"namespace"="root\standardcimv2\embedded"}$CommonParams += $PSBoundParameters    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned;     $keys = Get-WMIObject -class WEKF_PredefinedKey @CommonParams        foreach($k in $keys)         {            if($k.Enabled -eq $false)            {             "$k";            }        }我在 C# 代碼中的調用如下所示(注意:使用 System.Management.Automation): using (PowerShell PowerShellInstance = PowerShell.Create())        {          PowerShellInstance.AddScript(Properties.Settings.Default.ReadEnabledKeys);                    var result = PowerShellInstance.Invoke();        } 我的變量結果將始終為空。如果我直接在 Powershell 中運行 skript,輸出就好了(所有快捷方式,目前都沒有被禁用)。我使用統(tǒng)一寫入過濾器進行了類似的編程,我在其中啟用和禁用它:$COMPUTER = "localhost"$NAMESPACE = "root\standardcimv2\embedded"Set-ExecutionPolicy -ExecutionPolicy RemoteSigned;$objUWFInstance = Get-WMIObject -namespace $NAMESPACE -class UWF_Filter;$retval = $objUWFInstance.Enable();if ($retval.ReturnValue -eq 0) {"Unified Write Filter will be enabled after the next system restart."}else {"Unknown Error: " + "{0:x0}" -f $retval.ReturnValue}和 C# 調用: using (PowerShell PowerShellInstance = PowerShell.Create())        {          PowerShellInstance.AddScript(Properties.Settings.Default.EnableUWF);          // [0] = result or error          var result = PowerShellInstance.Invoke();          if (result[0].ToString().ToLower().Contains("enabled"))            MessageBox.Show(result[0].ToString(), "", MessageBoxButton.OK, MessageBoxImage.Information);          else            MessageBox.Show("Error when enabling the filter!  " + Environment.NewLine + result[0].ToString(), "",                            MessageBoxButton.OK, MessageBoxImage.Error);        }在這里,我的結果變量將填充預期的字符串。誰能告訴我,這是什么問題?
查看完整描述

1 回答

?
LEATH

TA貢獻1936條經(jīng)驗 獲得超7個贊

問題似乎出在您的腳本上。設置ExecutionPolicy中游不會做任何事情,并且您沒有編寫函數(shù),因此添加$PSBoundParameters也不會做任何事情。這是一個應該有效的示例(我將來會指定 PS 版本。由于鍵盤過濾,我知道您使用的是 v5.1/win10)


$collection = [System.Collections.Generic.List[string]]::new()

foreach ($key in (Get-CimInstance -Namespace 'root\standardcimv2\embedded' -ClassName WEKF_PredefinedKey)) {

    if (-not $key.Enabled) {

        $collection.Add($key.ToString())

    }

}

return $collection

(簡化)


@(Get-CimInstance -Namespace root\standardcimv2\embedded -ClassName WEKF_PredefinedKey).

    Where{-not $_.Enabled}.

    ForEach('ToString')

例子:


using (PowerShell ps = PowerShell.Create())

{

    string script = @"Import-Module -Name C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1 -ErrorAction Stop; @(Get-WmiObject -Namespace root\standardcimv2\embedded -Class WEKF_PredefinedKey -ErrorAction Stop).Where{-not $_.Enabled}.ForEach('ToString')";

    ps.AddScript(script);

    var result = ps.Invoke();

}


查看完整回答
反對 回復 2021-11-07
  • 1 回答
  • 0 關注
  • 585 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號