c# Create Windows Scheduled task for Any logged on user (Built-In\Users) or "S-1-5-32-545"
I have a manually created scheduled task that works well. I would like to replicate the scheduled task when my solution is installed. It must run for any user that logs in and not run at a SYSTEM level. I have tried multiple versions of the items below with no luck.
// Create a new task definition and assign properties
TaskDefinition taskDefinition = taskService.NewTask();
taskDefinition.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
taskDefinition.RegistrationInfo.Description = "Ensures the Agent is running";
taskDefinition.RegistrationInfo.Documentation = "Documentation at website";
taskDefinition.Principal.UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
//taskDefinition.Principal.GroupId = "S-1-5-32-545";
//taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
//taskDefinition.Principal.GroupId = "Users";
//taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
[1]: [Scheduled Task]https://i.stack.imgur.com/uuGmn.png
Related
Anyone would know if it is possible to get from the history task schedules, the command and arguments executed of all the tasks ?
I have a .ps1 script that obtains from the task scheduler history, three values: data of execution, taskname and result code.
$EventFilter = #{
LogName = 'Microsoft-Windows-TaskScheduler/Operational'
Id = 201 #action completed
StartTime = [datetime]::Now.AddDays(-10)
}
# PropertySelector for the Correlation id (the InstanceId) and task name
[string[]]$PropertyQueries = #(
'Event/EventData/Data[#Name="InstanceId"]'
'Event/EventData/Data[#Name="TaskName"]'
'Event/EventData/Data[#Name="ResultCode"]'
)
$PropertySelector = New-Object System.Diagnostics.Eventing.Reader.EventLogPropertySelector #(,$PropertyQueries)
# Loop through the start events
$TaskInvocations = foreach($StartEvent in Get-WinEvent -FilterHashtable $EventFilter){
# Grab the InstanceId and Task Name from the start event
$InstanceId,$TaskName,$ResultCode = $StartEvent.GetPropertyValues($PropertySelector)
# Create custom object with the name and start event, query end event by InstanceId
[pscustomobject]#{
TaskName = $TaskName
StartTime = $StartEvent.TimeCreated
ResultCode= $ResultCode
}
}
$TaskInvocations
Now I need to know what was the command and arguments executed in each task...
I don't find anything... :-( I'm beggining to think that this is not possible...
any idea, please?
Thanks in advance.
I'm writing a command line program and want the terminal to automatically open whenever I run my program is this possible in IntelliJ?
Yes, you can.
Use this code (you'll need a Project instance, you can get it from ActionEvent.getProject):
GeneralCommandLine commandLine = new GeneralCommandLine([List of your command line arguments]);
OSProcessHandler handler = new OSProcessHandler(commandLine);
TextConsoleBuilder consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(project);
ConsoleView console = consoleBuilder.getConsole();
Executor executor = DefaultRunExecutor.getRunExecutorInstance();
DefaultActionGroup actionGroup = new DefaultActionGroup();
BorderLayoutPanel component = JBUI.Panels.simplePanel();
component = component.addToCenter(console.getComponent());
ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("MainToolbar", actionGroup, false);
RunContentDescriptor descriptor = new RunContentDescriptor(console,
handler,
component.addToLeft(toolbar.getComponent()),
[Your title],
[Your icon]);
ExecutionManager manager = ExecutionManager.getInstance(project);
manager.getContentManager().showRunContent(executor, descriptor);
AnAction[] consoleActions = console.createConsoleActions();
for (AnAction action : consoleActions) actionGroup.add(action);
actionGroup.add(new PinActiveTabAction());
actionGroup.add(new CloseAction(executor, descriptor, project));
console.allowHeavyFilters();
console.attachToProcess(handler);
ProcessTerminatedListener.attach(handler);
handler.startNotify();
I am creating task scheduler through PowerShell deployment, I want to create One Time Task, I am using below script -
xScheduledTask SetupEmployeeProject
{
TaskName = "Mobile Employee Pay"
ActionExecutable = "$($Node.TargetRootFolder)\xxx\bin\Release\xxx.exe"
ActionArguments = "employee"
ScheduleType = "Once"
RepeatInterval = 1
StartTime ="7:00 PM"
Ensure = "Present"
#ExecuteAsCredential = $serviceCreds
}
But when my deployment is done, I am seeing an error
Failed to execute the powershell script.
The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: At least one of the values 'Once' is not supported or valid for property 'ScheduleType' on class 'xScheduledTask'. Please specify only supported values:
What should be the ScheduleType for one time job??
I have been trying to uninstall applications on devices or users using SCCM. I have been successful in creating an application assignment that would install applications, but I haven't been able to get it to uninstall. The code I have been using is:
IResultObject assignment = this.manager.CreateInstance("SMS_ApplicationAssignment");
IResultObject application =
this.manager.GetInstance("SMS_Application.CI_ID=16777339");
assignment["ApplicationName"].StringValue = application["LocalizedDisplayName"].StringValue;
assignment["AssignedCI_UniqueID"].StringValue = application["CI_UniqueID"].StringValue;
assignment["AssignedCIs"].IntegerArrayValue = new[] { application["CI_ID"].IntegerValue};
assignment["AssignmentName"].StringValue = "Deepak's deployment";
assignment["CollectionName"].StringValue = "Deepak's Collection of Devices";
assignment["DisableMomAlerts"].BooleanValue = true;
assignment["NotifyUser"].BooleanValue = false;
assignment["OfferFlags"].IntegerValue = 0;
assignment["DesiredConfigType"].IntegerValue = 1;
assignment["OverrideServiceWindows"].BooleanValue = false;
assignment["RebootOutsideOfServiceWindows"].BooleanValue = false;
assignment["SuppressReboot"].IntegerValue = 0;
assignment["TargetCollectionID"].StringValue = "UKN0000F";
assignment["EnforcementDeadline"].DateTimeValue = DateTime.Now.AddDays(1);
assignment["StartTime"].DateTimeValue = DateTime.Now;
assignment["UseGMTTimes"].BooleanValue = false;
assignment["UserUIExperience"].BooleanValue = false;
assignment["WoLEnabled"].BooleanValue = false;
assignment["RequireApproval"].BooleanValue = true;
assignment["OfferTypeId"].IntegerValue = 2;
assignment.Put();
This code will put up the application as an install deployment in SCCM. How do I get it as an uninstall deployment?
There is an AppAction enumeration, which I suspect is used by the client and not on the server.
typedef enum AppAction
{
appDiscovery = 0,
appInstall = 1,
appUninstall = 2
} AppAction;
Any help would be appreciated!
The setting that needs to be changed is DesiredConfigType.
For your code add the following before put():
assignment["DesiredConfigType"].IntegerValue = 2;
A value of 1 represents install (required) and 2 will uninstall (not allowed).
https://msdn.microsoft.com/en-us/library/hh949014.aspx
The way I do it is first use uninstall.exe to determine the guid of the program, and then create a program for the package I wish to uninstall, and just call uninstall.exe /whatever as the command. This works for most apps that show up in Add/Remove, and if it doesn't show up there then it'll have to be a hack (or script) anyway to uninstall. I believe the reason you're falling short is because if there is no command to uninstall the deployment in sccm, then it has nothing to run.
After you create an uninstall program, you could just call that deployment from your code, and voila.
As long as the target program that you are trying to use was installed via an MSI (Microsoft Installer) then you can loop through the registry to find your product (Registry Location: "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall") And just look at each DisplayName value.
In our environment, I accomplish this task by using Powershell, and we setup a program that specifically uninstalls whatever we are after.
Hope this helps...
Raged.
say, this is the way to create reminder:
Reminder reminder = new Reminder(name);
reminder.Title = titleTextBox.Text;
reminder.Content = contentTextBox.Text;
reminder.BeginTime = beginTime;
reminder.ExpirationTime = expirationTime;
reminder.RecurrenceType = recurrence;
reminder.NavigationUri = navigationUri;
// Register the reminder with the system.
ScheduledActionService.Add(reminder);
I can not see the result as I use emulator and I have these questions:
1) If I create a reminder today 25/Jul : Begin time 25-jul and ExpirationTime : 25-jul, after 25-jul, will the reminder created on 25-jul will still be in the system or scheduler?
2) If the expirationTime is 28-Jul, will it shows the BeginTime when this reminder got activated on 28-jul?
3) How to I retrieve all the reminders have not be activated.
Thanks
--- Updated :
var reminders = ScheduledActionService.GetActions (ScheduledAction)();
.Where(a => a.IsScheduled);
1) Yes, it will be there. Reminders associated with an application are still available, even if those are dismissed by the user.
2) The BeginTime will be set according to the class property that is set by the application, not when the reminder was activated.
3) You can retrieve all reminders registered for your application through:
var n = ScheduledActionService.GetActions<Reminder>();
foreach (Reminder r in n)
// Action here
You can check the IsScheduled property to make sure that the reminder is scheduled to be triggered or is already out of the queue.