2

I have a problem with a Powershell script in VS (C#).

Summary: I build a little tool for specific client actions for Microsoft System Center.

The following runs perfectly:

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

One of the actions will trigger an evaluation of the user policy. Problem is: running the script remotely will not trigger the actions for the logged in user on the client.

Here is a workaround I found in 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}'");
$sched.Triggers=@('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0');
$sched.Put()

Now I have problems to parse the script. When tell Powershell to run the script directly, with Invoke, it runs perfect (locally). But I don't want to have the script to persist in the application directory.

So I try to run the script like the first one:

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}'\"); $sched.Triggers=@('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0'); $sched.Put()");

but it will not run (probably a syntax error).

I'm very new to VS and C# ; probably your trained eyes see more. :)

Thanks in advance, Chris

PS: here is the tutorial that I used

2 Answers 2

1

@wp78de: Thanks for your answer. But that alone doesn't did the trick. BUT, Verbatim String helps me significant.

I've done it now. Now I can trigger the UserPolicy for the current logged on user.

Code is:

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    = @"$sched.Triggers=@('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");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use a verbatim string instead:

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}'");
$sched.Triggers=@('SimpleInterval;Minutes=1;MaxRandomDelayMinutes=0');
$sched.Put()");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.