I haven't been able to launch notepad under a specific user session using PowerShell. I still use psexec from sisinternals.
Powershell Script - run as logged on user from system run script
I have a scheduled task running as system which creates a device and user VPN. I have copied the functional aspect of the user VPN creation from elsewhere, and don't understand enough in detail, how to adapt it to use the same or new CIMSession to open a local file as the logged on user. It needs to be the logged on user, as the file opens in the system environment at the moment and can't be seen by the logged in user. The file is a text file I would like to open using notepad.
The code I have for creating the user VPN is:
$nodeCSPURI = "./Vendor/MSFT/VPNv2"
$namespaceName = "root\cimv2\mdm\dmmap"
$className = "MDM_VPNv2_01"
$username = Gwmi -Class Win32_ComputerSystem | select username
$objuser = New-Object System.Security.Principal.NTAccount($username.username)
$sid = $objuser.Translate([System.Security.Principal.SecurityIdentifier])
$SidValue = $sid.Value
$session = New-CimSession
$options = New-Object Microsoft.Management.Infrastructure.Options.CimOperationOptions
$options.SetCustomOption("PolicyPlatformContext_PrincipalContext_Type", "PolicyPlatform_UserContext", $false)
$options.SetCustomOption("PolicyPlatformContext_PrincipalContext_Id", "$SidValue", $false)
$newInstance = New-Object Microsoft.Management.Infrastructure.CimInstance $className, $namespaceName
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ParentID", "$nodeCSPURI", "String", "Key")
$newInstance.CimInstanceProperties.Add($property)
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("InstanceID", "$ProfileNameEscaped", "String", "Key")
$newInstance.CimInstanceProperties.Add($property)
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ProfileXML", "$ProfileXML", "String", "Property")
$newInstance.CimInstanceProperties.Add($property)
$session.CreateInstance($namespaceName, $newInstance, $options)
$Status = "Created $ProfileName profile."
If anyone can help me with this, would you also please tell me how you decide what CIMClassname, you use to do what with PowerShell.
PeteL
1 additional answer
Sort by: Most helpful
-
PeterL 86 Reputation points
2020-10-06T12:50:38.353+00:00 Another option, found here create-scheduled-task-run-as-logged-on-usewr Credit to Jonathan Walz. The code creates a scheduled task as the logged on user, runs the task, waits 5 seconds and deletes the task.
$action = New-ScheduledTaskAction -Execute “notepad.exe” -Argument "FileNameLocation" $trigger = New-ScheduledTaskTrigger -AtLogOn $principal = New-ScheduledTaskPrincipal -UserId (Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object -expand UserName) $task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal Register-ScheduledTask Notepad -InputObject $task Start-ScheduledTask -TaskName Notepad Start-Sleep -Seconds 5 Unregister-ScheduledTask -TaskName notepad -Confirm:$false