2017-09-13 185 views
2

我在VS(C#)中的Powershell腳本有問題。在C#中執行Powershell腳本問題

摘要:我爲Microsoft System Center的特定客戶端操作構建了一個小工具。

下完美運行:

if (MachPolBox.IsChecked ?? true) 
{ 
    using (PowerShell PowerShellInstance = PowerShell.Create()) 
    { 
     PowerShellInstance.AddScript("Invoke-WMIMethod -ComputerName " + ComputerBox.Text + " -Namespace root\\ccm -Class SMS_CLIENT -Name TriggerSchedule " + MachinePolicy); 
     PowerShellInstance.Invoke(); 
     MessageBlock.Foreground = Brushes.White; 
     MessageBlock.Text = "running..."; 

     if (PowerShellInstance.HadErrors) 
     { 
      MessageBlock.Foreground = Brushes.Red; 
      MessageBlock.Text = "Fehler... Programm als Administrator ausgeführt? Computername richtig?"; 
     } 
     else 
     { 
      MessageBlock.Foreground = Brushes.White; 
      MessageBlock.Text = "Erfolgreich"; 
     } 
    } 
} 

一個動作時會觸發用戶策略的評估。問題是:遠程運行腳本不會觸發客戶端登錄用戶的操作。

這是一種變通方法我在PowerShell中發現:

$sid = (get-wmiobject -query "SELECT UserSID FROM CCM_UserLogonEvents WHERE LogoffTime = NULL" -namespace "ROOT\ccm").UserSID.replace('-','_'); 
$sched=([wmi]"root\ccm\Policy\$sid\ActualConfig:CCM_Scheduler_ScheduledMessage.ScheduledMessageID='{00000000-0000-0000-0000-000000000026}'"); 
[email protected]('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0'); 
$sched.Put() 

現在我有問題解析腳本。當告訴Powershell直接運行腳本時,使用Invoke,它運行完美(本地)。但我不想讓腳本堅持在應用程序目錄中。

所以我嘗試運行腳本像第一個:

PowerShellInstance.AddScript("$sid = (get-wmiobject -query \"SELECT UserSID FROM CCM_UserLogonEvents WHERE LogoffTime = NULL\" -namespace \"ROOT\\ccm\").UserSID.replace('-','_'); $sched=([wmi]\"root\\ccm\\Policy\\$sid\\ActualConfig: CCM_Scheduler_ScheduledMessage.ScheduledMessageID = '{00000000-0000-0000-0000-000000000026}'\"); [email protected]('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0'); $sched.Put()"); 

,但它不會運行(可能是語法錯誤)。

我對VS和C#很陌生;大概你訓練有素的眼睛看到更多:)

在此先感謝,克里斯

PS:here is the tutorial that I used

回答

0

嘗試使用,而不是逐字字符串:

PowerShellInstance.AddScript(@"$sid = (get-wmiobject -query "SELECT UserSID FROM CCM_UserLogonEvents WHERE LogoffTime = NULL" -namespace "ROOT\ccm").UserSID.replace('-','_'); 
$sched=([wmi]"root\ccm\Policy\$sid\ActualConfig:CCM_Scheduler_ScheduledMessage.ScheduledMessageID='{00000000-0000-0000-0000-000000000026}'"); 
[email protected]('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0'); 
$sched.Put()"); 
0

@ wp78de:謝謝您的回答。但僅此而已,並沒有訣竅。但是,逐字字符串對我有幫助。

我已經完成了。現在我可以觸發當前登錄用戶的UserPolicy。

代碼是:

if (UserPolBox.IsChecked ?? true) 
 
      { 
 
       using (PowerShell PowerShellInstance = PowerShell.Create()) 
 
       { 
 
        var com   = @"$sid = (Get-WmiObject -Computername '" + ComputerBox.Text + @"' -query ""SELECT UserSID FROM CCM_UserLogonEvents WHERE LogoffTime = NULL"" -namespace ""ROOT\ccm"").UserSID.replace('-','_');"; 
 
        var sched  = @"$sched = ([wmi]""\\" + ComputerBox.Text + @"\root\ccm\Policy\$sid\ActualConfig:CCM_Scheduler_ScheduledMessage.ScheduledMessageID='{00000000-0000-0000-0000-000000000026}'"");"; 
 
        var triggers = @"[email protected]('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0');"; 
 
        var put   = @"$sched.Put()"; 
 

 
        PowerShellInstance.AddScript(com + sched + triggers + put); 
 

 
        PowerShellInstance.Streams.Error.Clear(); 
 
        PowerShellInstance.Streams.Warning.Clear(); 
 
        var result = PowerShellInstance.Invoke(); 
 
        MessageBox.Show(PowerShellInstance.Streams.Error.Count().ToString() + " error counts"); 
 

 
        foreach (var errorRecord in PowerShellInstance.Streams.Error) 
 
        { 
 
         MessageBox.Show(errorRecord.ToString() + "first - error"); 
 
        } 
 
       } 
 
      }

+0

好,太好了。可悲的是,你還不能投票。 – wp78de