📜 ⬆️ ⬇️

PowerShell and GUI. It is not difficult

Some time ago, a terminal farm was deployed in the company.
First of all, users of a certain yellow program were evicted into it.
After that, the yellow program support department asked me if I could send messages to farm users all at once. XaocCPS advised me to play in the direction of WPF. The necessary script was written, but I was not satisfied with its work:
1. You need to install the external component PowerShellPack.
2. The component is placed on the server farm (x64) refused.
3. To distribute such a solution for paragraph 1 to everyone is not very convenient.

Xaegr suggested that I can get rid of the WPF interlayer.
You can write, you can even write beautifully. The script is executed wherever there is. Net Framework - XP, Win7 and most likely will go even on x64 servers of the farm.
How to write - under the cut.

UPD on request script posted on SkyDrive, link at the end

And so - the TZ received from the support department was “sending messages to all users of the terminal farm”.
My TK has been expanded:
1. Select servers.
2. Selection of users.
3. Enter the message text.
4. Insert the signature - the identifier of the user who sent the message - is visible in the title bar of the window, but few people pay attention to it, choose an option from several preset options.
5. Protection against inadvertent dispatch.
')
Previously it was found that in Win7 and 2008 * there is the msg command, with the help of which these messages will be sent, but the reception of messages should be allowed, which was done by a simple regedit file:

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server]
"AllowRemoteRPC"=dword:00000001


Unfortunately, if the user from the list is not connected to the server - the script is a lot and strongly curses. I have not yet figured out how to get the msg output, and process it.
Script to the maximum commented.
~ 15kb

 #  ,        Add-Type -assembly System.Windows.Forms #  ,    ,  ,    -      . $Server = @("rdp1-1, rdp1-2, rdp1-3, rdp1-4", "rdp2-1, rdp2-2, rdp2-3, rdp2-4") $User = @("*", "1c-admin1, 1c-admin2") $Message = " " #  ,    ,            ,    . #        ,         #  $Sign = @(" ,  1", " ,  ") # ,       . #   .  , ,    (    ),       ,     ,                ( : "1, 2, 3"    ),   msg               . Function SendMessage { param ($Server, $User, $Message, $Sign) # Write-Host $Server, $User, $Message, $Sign If ($TestRunCheckBox.Checked -eq 1 ) { Write-Host $TestRunCheckBox.Checked; $Server="localhost"; $User = "Console" } ForEach ($Item in $Server) { ForEach ($UserX in $User) { $UserTrim = $UserX.Trim() $ServerTrim = $Item.Trim() $MsgTrim = $Message.Trim() $SignTrim = $SignX.Trim() #  ,    . # Write-Host "User: $UserTrim; Server: $ServerTrim; Message: $MsgTrim; Signature: $SignTrim" c:\windows\system32\msg.exe $UserTrim /Server:$ServerTrim $MsgTrim $SignTrim } } Confirm } #     Function Confirm { $ConfirmWin = New-Object System.Windows.Forms.Form $ConfirmWin.StartPosition = "CenterScreen" $ConfirmWin.Text = " " $ConfirmWin.Width = 200 $ConfirmWin.Height = 120 $ConfirmWin.ControlBox = 0 $ConfirmWinOKButton = New-Object System.Windows.Forms.Button $ConfirmWinOKButton.add_click({ $MainSendWindow.Close(); $ConfirmWin.Close() }) $ConfirmWinOKButton.Text = "" $ConfirmWinOKButton.AutoSize = 1 $ConfirmWinOKButton.Location = New-Object System.Drawing.Point(50,50) $ConfirmLabel = New-Object System.Windows.Forms.Label $ConfirmLabel.Text = "  " $ConfirmLabel.AutoSize = 1 $ConfirmLabel.Location = New-Object System.Drawing.Point(10,10) $ConfirmWin.Controls.Add($ConfirmLabel) $ConfirmWin.Controls.Add($ConfirmWinOKButton) $ConfirmWin.ShowDialog() | Out-Null } #  ,          $MainSendWindow = New-Object System.Windows.Forms.Form $ToolTip = New-Object System.Windows.Forms.ToolTip $ToolTip.BackColor = [System.Drawing.Color]::LightGoldenrodYellow $ToolTip.IsBalloon = $true # $ToolTip.InitialDelay = 500 # $ToolTip.ReshowDelay = 500 #    #    $SendButton = New-Object System.Windows.Forms.Button $CloseButton = New-Object System.Windows.Forms.Button $TestRunCheckBox = New-Object System.Windows.Forms.CheckBox #     $ServerTextBox = New-Object System.Windows.Forms.ComboBox $UserTextBox = New-Object System.Windows.Forms.ComboBox $MessageTextBox = New-Object System.Windows.Forms.TextBox $SignTextBox = New-Object System.Windows.Forms.ComboBox #  $ServerTextBoxLabel = New-Object System.Windows.Forms.Label $UserTextBoxLabel = New-Object System.Windows.Forms.Label $MessageTextBoxLabel = New-Object System.Windows.Forms.Label $SignTextBoxLabel = New-Object System.Windows.Forms.Label #   (      #           ;)) #   $MainSendWindow.StartPosition = "CenterScreen" $MainSendWindow.Text = "  " $MainSendWindow.Width = 470 $MainSendWindow.Height = 220 #      #$Win.ControlBox = 0 #   ,   . # $Win.ShowIcon = 0 # $Win.ShowInTaskbar = 0 # $Win.HelpButton = 1 #       -     " " # $Win.Autosize = 1 # $Win.AutoSizeMode = "GrowAndShrink" #    . # $Win.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D # $Win.Font = New-Object System.Drawing.Font("Verdana",32) #     $ServerTextBoxLabel.Location = New-Object System.Drawing.Point(10,12) $ServerTextBoxLabel.Text = " " $ServerTextBoxLabel.Autosize = 1 $UserTextBoxLabel.Location = New-Object System.Drawing.Point(10,42) $UserTextBoxLabel.Text = " " $UserTextBoxLabel.Autosize = 1 $MessageTextBoxLabel.Location = New-Object System.Drawing.Point(10,73) $MessageTextBoxLabel.Text = "" $MessageTextBoxLabel.Autosize = 1 #     ,    ,       ,   . $ToolTip.SetToolTip($MessageTextBoxLabel, " ,         ") $SignTextBoxLabel.Location = New-Object System.Drawing.Point(10,103) $SignTextBoxLabel.Text = "" $SignTextBoxLabel.Autosize = 1 $ToolTip.SetToolTip($SignTextBoxLabel, " ,         ") #   #  $ServerTextBox.Location = New-Object System.Drawing.Point(140,10) #   $ServerTextBox.DataSource = $Server #  $ServerTextBox.Width = 300 #   -     ,     . $ServerTextBox.add_TextChanged({ $Server = $ServerTextBox.Text }) #     Tab $ServerTextBox.TabIndex = 1 $ToolTip.SetToolTip($ServerTextBox, "  ") $UserTextBox.Location = New-Object System.Drawing.Point(140,40) $UserTextBox.DataSource = $User #     $UserTextBox.Text = $User[1] $UserTextBox.add_TextChanged({ $User = $UserTextBox.Text }) $UserTextBox.Width = 300 $UserTextBox.TabIndex = 2 $ToolTip.SetToolTip($UserTextBox, "  ? (*  ** ,  )") #   $MessageTextBox.Location = New-Object System.Drawing.Point(140,70) $MessageTextBox.Text = $Message #      -    ,     #   $MessageTextBox.add_click({ $MessageTextBox.SelectAll() }) $MessageTextBox.add_TextChanged( { $Message = $MessageTextBox.Text }) $MessageTextBox.Width = 300 $MessageTextBox.TabIndex = 3 $ToolTip.SetToolTip($MessageTextBox, "     ?") #   -     $SignTextBox.Location = New-Object System.Drawing.Point(140,103) #     $SignTextBox.DataSource = $Sign #   ,   ?:) $SignTextBox.Text = $Sign[1] $SignTextBox.add_TextChanged({ $SignX = $SignTextBox.Text }) $SignTextBox.Width = 300 $SignTextBox.TabIndex = 4 $ToolTip.SetToolTip($SignTextBox, "    ") #  . $SendButton.Location = New-Object System.Drawing.Point(10,150) $SendButton.Text = " " #        ,     $SendButton.add_click({ $User = $UserTextBox.Text.Split(","); $Server = $ServerTextBox.Text.Split(","); $SignX = $SignTextBox.Text; SendMessage $Server $User $Message $SignX} ) $SendButton.Autosize = 1 $SendButton.TabIndex = 5 $ToolTip.SetToolTip($SendButton, " ") #    $TestRunCheckBox.Location = New-Object System.Drawing.Point(200,150) $TestRunCheckBox.Text = "" $TestRunCheckBox.Checked = 1 $TestRunCheckBox.AutoSize = 1 $TestRunCheckBox.TabIndex = 6 $ToolTip.SetToolTip($TestRunCheckBox, " ,     ") #  ,      $CloseButton.Location = New-Object System.Drawing.Point(315,150) $CloseButton.Text = "  " $CloseButton.add_click({ $MainSendWindow.Close() }) $CloseButton.Autosize = 1 $CloseButton.TabIndex = 7 $ToolTip.SetToolTip($CloseButton, "  ") #         $MainSendWindow.Controls.Add($SendButton) $MainSendWindow.Controls.Add($TestRunCheckBox) $MainSendWindow.Controls.Add($CloseButton) $MainSendWindow.Controls.Add($ServerTextBox) $MainSendWindow.Controls.Add($UserTextBox) $MainSendWindow.Controls.Add($MessageTextBox) $MainSendWindow.Controls.Add($SignTextBox) $MainSendWindow.Controls.Add($ServerTextBoxLabel) $MainSendWindow.Controls.Add($UserTextBoxLabel) $MainSendWindow.Controls.Add($MessageTextBoxLabel) $MainSendWindow.Controls.Add($SignTextBoxLabel) $MainSendWindow.ShowDialog() | Out-Null 


The result is such a beauty
image

The script can lay out, if desired, readers, somewhere.

I'm not sure that there is enough karma to publish to the profile, so I posted it here.

Script source on GitHub

Source: https://habr.com/ru/post/138008/


All Articles