-
Notifications
You must be signed in to change notification settings - Fork 79
/
send_popup_message_to_multiple_systems.ps1
147 lines (97 loc) · 3.86 KB
/
send_popup_message_to_multiple_systems.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<#
Purpose: Trigger a pop-up message window on a list of systems
Requirements: Admin rights on the target machines
Author: S-6
History: 1.0.0 Initial write
Usage: Edit the variables below to target the OU you want, and specify where psexec is
#>
#############
# VARIABLES # (set these to your values)
#############
# OU containing desktops to target
$DesktopOU = "OU=MB,OU=Desktops,OU=DPTMS,OU=IMCOM,OU=Bliss,OU=Installations,DC=nasw,DC=ds,DC=army,DC=mil"
# OU containing laptops to target
$LaptopOU = "OU=MB,OU=Laptops,OU=DPTMS,OU=IMCOM,OU=Bliss,OU=Installations,DC=nasw,DC=ds,DC=army,DC=mil"
# Message to send
#$message = "Please save your work and restart your computer ASAP. Thanks, S-6."
$MessageToSend = "This is a test of the S-6 notification system, please disregard this message."
# Location of psexec (if using the psexec method vs. the Powershell Invoke-Command method) $psexec = c:\windows\system32\psexec.exe
########
# PREP #
########
$SCRIPT_VERSION = "1.0.0"
$SCRIPT_UPDATED = "2020-02-21"
###########
# EXECUTE #
###########
# The wrest of the script is wrapped in the "main" function, just so we can put the logging function at the bottom
function main() {
# Pull in the list of computers we're targeting
$DesktopList=(get-adcomputer -searchbase "$DesktopOU" -filter * | select -expand Name)
$LaptopList=(get-adcomputer -searchbase "$LaptopOU" -filter * | select -expand Name)
# for testing against specific systems
#$DesktopList = 'BLISW6CLAAWKPT2',
#'BLISWKMAD1HQ604',
#'BLISWkmad1hq602'
#$LaptopList = 'BLISW6CLAAWKPT2',
#'BLISWKMAD1HQ604',
#'BLISWkmad1hq602'
# Count how many in each OU
$DesktopCount = $DesktopList.Count
$LaptopCount = $LaptopList.Count
$TotalCount = $DesktopCount + $LaptopCount
# Notify what we're doing
log " Message sending script v$SCRIPT_VERSION ($SCRIPT_UPDATED)" green
log " Message to send: '$MessageToSend'" white
log " Desktop OU: ($DesktopCount hosts) '$DesktopOU'"
log " Laptop OU: ($LaptopCount hosts) '$LaptopOU'"
# Prompt to launch
""
write-host " Message will be sent to a total of $TotalCount hosts." -f white
""
write-host " Are you sure?" -f red
""
pause
""
# Loop through and send the message (Desktop OU)
log " Sending to hosts in the Desktop OU..." green
foreach ( $computer in $DesktopList ) {
# Check to see if the system is online before sending
if (test-Connection -count 1 -Cn $computer -quiet) {
# Using Psexec to connect
# & $psexec \\$computer -i msg * "$MessageToSend"
# Using Powershell to connect
Invoke-Command -ComputerName $computer -ScriptBlock { msg * "$using:MessageToSend" } -ea SilentlyContinue # the "$using:" method only works on PS v3 and up
log " $computer sent."
} else {
log " ! $computer not online, skipping." yellow
}
}
log " Done." darkgreen
# Loop through and send the message (Laptop OU)
log " Sending to hosts in the Laptop OU..." green
foreach ( $computer in $LaptopList ) {
# Check to see if the system is online before sending
if (test-Connection -count 1 -Cn $computer -quiet) {
# Using Psexec to connect
# & $psexec \\$computer -i msg * "$MessageToSend"
# Using Powershell to connect
Invoke-Command -ComputerName $computer -ScriptBlock { msg * "$using:MessageToSend" } -ea SilentlyContinue # the "$using:" method only works on PS v3 and up
log " $computer sent."
} else {
log " ! $computer not online, skipping." yellow
}
}
log " Done." darkgreen
} # Close out the main function. End of script
#############
# FUNCTIONS #
#############
function log($message, $color)
{
if ($color -eq $null) {$color = "gray"}
# log to console
write-host (get-date -f "yyyy-MM-dd hh:mm:ss") -n -f darkgray; write-host "$message" -f $color
}
# Call the main function (script). Script won't function without this line
main