1

I am wanting the ability to send an "Alert Styled Message" to a specific computer on the domain.

Problem 1:

Currently, I have the following as the Alert and If I run it from within Powershell, I get the popup exactly how I want. If I run it outside POwershell )right click, run with POwershell ISE) it does nothing.

Here is the code I have for this part:

$ALERT = {Please contact the I.T. Help Desk @EXT 777.  
Reference ERROR CODE: 910978
}

$MYALERT = [System.Windows.Forms.MessageBox]::Show("$ALERT ","I.T. 
DEPARTMENT ALERT !", "ok","Warning”)
$MYALERT

Problem 2:

I want to be able to prompt the user for a computer name, and send the alert to that specific computer. I can not get this to work at all... although it did work once, from within powershell but after running it outside powershell it never worked again. Nonetheless, running outside powershell did not ever work at all.

Here is the code I have for this part:

cls
echo ""
echo ""
$name = read-host "Enter computer name "
$ALERT = {Please contact the I.T. Help Desk @EXT 777.  
Reference ERROR CODE: 910978
}
$MYALERT = {
[System.Windows.Forms.MessageBox]::Show("$ALERT ","I.T. DEPARTMENT ALERT !", 
"ok","Warning”)
}

Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList $MYALERT - 
ComputerName $name
pause
cls
echo ""
echo "  Message Sent.  Returning to Main Menu"
cls
EXIT

I do, however get the following output on the console window:

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     : 
__DYNASTY        : __PARAMETERS
__RELPATH        : 
__PROPERTY_COUNT : 2
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
ProcessId        : 
ReturnValue      : 9
PSComputerName   : 

NOTE: I am not actually wanting to send error codes in this fashion, was just typing something in as an example. I want to use this to send a message to a specific computer on the domain, so that it shows up on top of any open windows and shows up as an alert as opposed to a message from a user.

Also, It seems that for PROBLEM #1, Adding "Add-Type -AssemblyName System.Windows.Forms" at the top allows this to work outside of Powershell. However, after adding the same for PROBLEM #2, it still does not work.

I suspect this might have something to do with it working on my local login because I am logged in, but something is stopping me from execting this on the remote machine.

Any idea on what I am doing wrong?

Thanks in advance!

Joseph Sanders
  • 133
  • 1
  • 2
  • 8
  • 3
    I would suspect that the `System.Windows.Forms` assembly is already loaded in your PowerShell environment, but doesn't get loaded in your script. Add the line `Add-Type -Assembly 'System.Windows.Forms'` to the script. As for your second issue: you cannot create a PowerShell scriptblock as a Windows process. That simply won't work. Also, it's not that simple to spawn a process remotely in the context of a different user (which you need if you want to display a message on their desktop). That would be a huge security hole. – Ansgar Wiechers Dec 03 '18 at 19:05
  • 2
    With that said: please focus on one problem at a time. If you're facing two different problems: post two questions. – Ansgar Wiechers Dec 03 '18 at 19:11
  • Related: https://stackoverflow.com/questions/20022976/difference-between-powershell-console-and-powershell-ise – Eris Dec 03 '18 at 19:13
  • Thank you. That makes sense. I didnt even think about the security vulnerability if it were possible. So given this information, what would your recommendation be (perhaps point me in the right direction) to accomplish what I am trying to do. I know I can send a message using something like {Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg * $msg" -ComputerName $name} But I am wanting to "Spruce" up the message box with a title, icon, etc and would rather it not show my username in the title of the message box. – Joseph Sanders Dec 03 '18 at 19:16
  • 2
    `msg` uses messaging infrastructure that is already built into Windows. If that doesn't suffice for you you need to use something that will provide this infrastruture for you (like chat or instant messengers), or build your own messaging infrastructure (which I generally wouldn't recommend). – Ansgar Wiechers Dec 03 '18 at 19:35
  • 1
    @AnsgarWiechers: All good points - how about writing them up as an answer? – mklement0 Dec 04 '18 at 03:20

1 Answers1

0

Problem 1:

The assembly that provides the MessageBox class isn't automatically loaded by your script. You need to do that by adding a statement

Add-Type -Assembly 'System.Windows.Forms'

at the beginning.

Problem 2:

You cannot create a PowerShell scriptblock as a Windows process. That simply won't work. Also, it's not that simple to spawn a process remotely in the context of a different user (which you need if you want to display a message on their desktop). That would be a huge security hole.

Using the msg commandline utility works, because it uses a messaging infrastructure that's already built into Windows. If that doesn't suffice for your needs you either need to use something that will provide this infrastructure for you (like IRC, instant messengers, Slack, etc.), or build that infrastructure yourself (which I generally wouldn't recommend due to security considerations).

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328