我在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
好,太好了。可悲的是,你還不能投票。 – wp78de