Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to send Toast from SYSTEM to logged in user? #35

Closed
bcurran3 opened this issue Sep 3, 2018 · 16 comments
Closed

Possible to send Toast from SYSTEM to logged in user? #35

bcurran3 opened this issue Sep 3, 2018 · 16 comments

Comments

@bcurran3
Copy link
Contributor

bcurran3 commented Sep 3, 2018

(I was going to make a feature request, but based on #33, I have a feeling you might already have a solution and provide it faster than me figuring it out! :)

USE CASE:
I have schedule task that runs as /RU SYSTEM. I would like to use BurntToast to send a notification from the task to the currently logged in user. Is this possible? (Yeah, I know. EVERYTHING is possible in PowerShell!) How do you recommend I go about it?

REF:
https://chocolatey.org/packages/chocolatey-toast-notifications.extension
https://chocolatey.org/packages/choco-upgrade-all-at

@juststur
Copy link

juststur commented Sep 3, 2018

I would make use of such a feature.

@jholl016
Copy link

jholl016 commented Sep 3, 2018

One possible solution: configure a second scheduled task that launches the toast under the logged in user context, then have your System task trigger the notification task.

@juststur
Copy link

juststur commented Sep 3, 2018

Basically how I have it setup now...just messy for documentation/support for others on my team trying to indicate what parts of the process are setup/triggered in what way.

@Windos
Copy link
Owner

Windos commented Sep 3, 2018

Unfortunately I don't have a "clean" method that I've extensively tested. I need to work on some "official" guidance and am open to suggestions/requests.

The main option I've been playing with are scheduled jobs/tasks running as the user (only when logged in, so you don't need to plug in a password).

It wouldn't work in your use case (and I'm not 100% sure it'd work at all), but you could also drop a script into the user's startup folder... though this then completely hinges on the user logging in fresh at some stage.

@bcurran3
Copy link
Contributor Author

bcurran3 commented Sep 4, 2018

I did some preliminary testing and it seems:

Invoke-Command -ComputerName RemotePC -Credential Joe -ScriptBlock {Toast -Text 'Test'}

changed to simply:

Invoke-Command -ComputerName $env:COMPUTERNAME -ScriptBlock {Toast -Text 'Test'}

works.

I first verified by opening up a Command Prompt as SYSTEM. (PsExec.exe -i -s cmd.exe) and used the Invoke-Command above - toast messages appeared to me logged in (obviously not as SYSTEM).

Not completely trusting that, I made a quick scheduled task to run and test:

schtasks /create /TN ToastTest /RU SYSTEM /RL HIGHEST /TR "powershell.exe Invoke-Command -ComputerName $env:COMPUTERNAME -ScriptBlock {Toast -Text 'ToastTest'}" /SC ONCE /ST 20:33 /V1 /Z

..and the toast appeared!

I'm going to implement and test this methodology in my actual scripts (need to do some adjusting to local variables) and will report back.

Joy!

@nmcspadden
Copy link

@bcurran3 did your fix get implemented? I'm in a similar situation now where our config management creates the task as SYSTEM but the execution doesn't trigger a notification as-is.

@bcurran3
Copy link
Contributor Author

bcurran3 commented Nov 27, 2018

@nmcspadden No, I haven't revisited it yet.

My test above worked but ran into problems when trying to use a button with it. I couldn't get a $button=New-BTButton -Content "blahblah" to work sending it that way to SYSTEM. It would gave an error. I think I just didn't pass the variable correctly. I put it on hold because trying to debug it was driving me nuts. (It was probably the wee hours of the morning).

I plan to split it in two phases now 1> make it work from SYSTEM minus the button which should be super easy as proof of concept has occurred and will revisit very soon. 2> Figure out my error in passing the button variable around. (Hmmmm..... might "cheat" and just assign it as global then remove it. That way I don't have to "pass" it. Hmmmmm) Now I might have to revisit it today!

@nmcspadden
Copy link

I'm happy to help test anything.

@bcurran3
Copy link
Contributor Author

OK. I'm still failing. Same problem. Maybe someone smarter than me can tell me what I'm doing wrong...

image

@bcurran3
Copy link
Contributor Author

As far as the proof of concept, works fine when NOT trying to use the -Button parameter.

I'm guessing I need to define or pass it along differently, but I'm too ignorant.

(Keep in mind that below is missing a lot of $env variables and thus displaying blanks.)

image

@bcurran3
Copy link
Contributor Author

bcurran3 commented Nov 27, 2018

Feeling a little smarter now....

SUCCESS!

Invoke-Command -ComputerName $env:COMPUTERNAME -ScriptBlock {New-BurntToastNotification -Text "Chocolatey ($env:chocolateyPackageName)", "$chocolateySoftwareName v$env:chocolateyPackageVersion", "Uninstalled." -Button (New-BTButton -Content 'Package Webpage' -Arguments "https://chocolatey.org/packages/$env:chocolateyPackageName") -AppLogo "$env:ChocolateyInstall\extensions\chocolatey-toast-notifications\choco.ico"}

Moving the New-BTButton creation into a result of part of the full command instead of assigning a variable to it and then passing the variable worked.

@nmcspadden, I retract my "No' answer and now say Yes. :)

...and a hint on use:

COMMENT: check for system account
if ($env:USERNAME -eq "$env:COMPUTERNAME$"){
COMMENT: WinRM needs to be running
If ((Get-Service $WinRM).Status -eq 'Running') {
Invoke-Command -ComputerName $env:COMPUTERNAME -ScriptBlock {blahblah}
}
}
COMMENT: else skip or create toast message without Invoke-Command here...

@bcurran3
Copy link
Contributor Author

bcurran3 commented Dec 14, 2018

Actually I still failed previously as none of my variables would pass along. I'd get a toast message across to the logged in user (me!) from the SYSTEM account, but the message would not include the variable info I tried to pass along.

I searched and I searched and found a few Q&As about using Invoke-Command and passing variables. The suggested -ArgumentList (,$arrayname) and using $args[x] failed me as I'd get all the arguments instead of just the one element I wanted. The examples I tried worked, but when I tried them myself... no workie. Finally found one that suggestion using and defining with param().

SUCCESS!

So the short of it for anyone who wants to send toast messages from the system account or other to all users, you can use this example:

(The following was tested by running PsExec.exe -i -s cmd.exe to get a Command Prompt as SYSTEM and then I ran PowerShell.)

powershellexample

toastresult

So my little pet project of https://chocolatey.org/packages/chocolatey-toast-notifications.extension is now where I want it to be. I feel triumphant and happy. So I'll close this issue as it's all working without the need for an enhancement of BurntToast!

@ludovicbernard
Copy link

if we want to send for a specific user in an RDS collection, how can we do, thanks for your remarks

@bcurran3
Copy link
Contributor Author

bcurran3 commented Feb 4, 2019

@ludovicbernard Sorry, I have not tried that and not sure. I'm guessing you'd want to figure out the list of computers in the collection and then loop through them sending each the message.

@Chirishman
Copy link

In case anyone was wondering this still works for normal toasts, but if you try it with New-BurntToastShoulderTap you just get an empty toast from the People app, no dancing Alfonso Ribeiro.

@KlausSGS
Copy link

I wonder if this is a feature or a bug that might be fixed by Microsoft at some time.

Starting the command in the SYSTEM context and ending up with a toast in the user context without even selecting the user or session. Seeing this working is a real surprise to me. Other processes will just launch in the SYSTEM context when started that way.

Is anybody aware of some piece of information concerning this behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants