-
Notifications
You must be signed in to change notification settings - Fork 9
/
Password Changer.ps1
204 lines (135 loc) · 7.34 KB
/
Password Changer.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<#
.SYNOPSIS
Connects to Crestron Devices in IP.txt of local script directory via SSH. Changes already set password to new password (AUTH ALREADY ENABLED)
.DESCRIPTION
Connects to Crestron Devices in IP.txt of local script directory via SSH. If default password is set, script will set password to user specified password (Line 104)
Script reports results out to root directory with whether the password was changed[New], previously set, or error connecting
.PARAMETER <Parameter_Name>
none
.INPUTS
- $username = 'ENTER YOUR USERNAME'
- $password = 'ENTER PASSWORD'
- IP.txt text file containing IP addresses of devices (One per line)
.OUTPUTS
C:\Desktop\Superscript Results.csv
.NOTES
Version: 1.0
Author: Anthony Tippy
Creation Date: 11/29/2021
Purpose/Change: initial dev
.EXAMPLE
Modify username/password variables --> enter IP addresses into IP.txt file --> script--> script will output device info $home\Password Change Results.csv"
#>
write-host -f cyan @"
██████╗ █████╗ ███████╗███████╗██╗ ██╗ ██████╗ ██████╗ ██████╗
██╔══██╗██╔══██╗██╔════╝██╔════╝██║ ██║██╔═══██╗██╔══██╗██╔══██╗
██████╔╝███████║███████╗███████╗██║ █╗ ██║██║ ██║██████╔╝██║ ██║
██╔═══╝ ██╔══██║╚════██║╚════██║██║███╗██║██║ ██║██╔══██╗██║ ██║
██║ ██║ ██║███████║███████║╚███╔███╔╝╚██████╔╝██║ ██║██████╔╝
╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝ ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝
██╗ ██╗██████╗ ██████╗ █████╗ ████████╗███████╗██████╗
██║ ██║██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔══██╗
██║ ██║██████╔╝██║ ██║███████║ ██║ █████╗ ██████╔╝
██║ ██║██╔═══╝ ██║ ██║██╔══██║ ██║ ██╔══╝ ██╔══██╗
╚██████╔╝██║ ██████╔╝██║ ██║ ██║ ███████╗██║ ██║
╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
Written By: Anthony Tippy
"@
# import libraries
Import-Module PSCrestron
$passchanged = " "
#Stopwatch feature
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
#create a new object to hold the restults data
$DeviceResultsData =@()
#Initilize the table
$DeviceResultsData | Out-GridView -Title "Device Status Results"
#clear out device error count
$devicerror = " "
#Delete Old Results from previous script run
remove-item -Path "$home\Password Update Results.csv" -ErrorAction SilentlyContinue
# import device file
try
{
$devs = @(Get-Content -Path (Join-Path $PSScriptRoot 'IP.txt'))
write-host " "
}
catch
{
Write-Host 'Error Obtaining list of devices. Make sure device list.txt is in same directory as script!!!'
}
# loop for each device
foreach ($d in $devs){
#### ENTER CREDENTIALS HERE #####
$username = "EXISTING USERNAME"
$password = "EXISTING PASSWORD"
$newpassword = "NEW PASSWORD TO BE SET"
#### ENTER CREDENTIALS HERE #####
try {
#Clear Connect Error
$Errors = " "
$AuthMethod = " "
#New Data Object
$DeviceResultItem = New-Object PSObject
Write-Host -f green "`n`nConnecting to : $d"
try{
#Connect to Device via SSH with old password
$session = Open-CrestronSession -Device $d -Secure -Username $username -Password $password
#Grab device hostname
$hostnameResponce = Invoke-CrestronSession $session "hostname"
$deviceHostname = [regex]::Match($hostnameResponce, "(?<=Host\sName:\s)[\w-]+").value
Write-host -f green "Working on => $deviceHostname`n`n`n`n"
}
catch{"$d : Error Opening Crestron Session"
$deviceHostname = " "
$ConnectError = "Error Opening Crestron Session,"
$Errors += $ConnectError
Stop}
try{
#Set Username/Password
Invoke-CrestronSession $session 'UPDATEPassword'
#Enter Current Password
Invoke-CrestronSession $session "$password"
#Enter Updated Pass x2
Invoke-CrestronSession $session "$newpassword"
Invoke-CrestronSession $session "$newpassword"
#Reboot Device to confirm changes
Invoke-CrestronSession $session 'reboot'
#Close-CrestronSession $session
Write-host -f Green "$deviceHostname : Password Successfully Changed`n`n`n`n"
$AuthMethod = 'Password [New]'
}
catch{"$d - $hostname : Error Changing Password`n"
$AuthMethod = 'Password Change Fail'
$ConnectError = "Error Changing password,"
$Errors += $ConnectError
stop}
}
catch {
Write-host -f red "$d : Unable to Connect!`n"
}
#Current Date/Time
$time = (get-date)
#Build Table
# Table Coulumn 1 - Time
$DeviceResultItem | Add-Member -Name "Time" -MemberType NoteProperty -Value $time
# Table Coulumn 2 - IP Address
$DeviceResultItem | Add-Member -Name "IP Address" -MemberType NoteProperty -Value $d
# Table Coulumn 3 - Hostname
$DeviceResultItem | Add-Member -Name "Hostname" -MemberType NoteProperty -Value $deviceHostname
# Table Coulumn 4 - Authentication
$DeviceResultItem | Add-Member -Name "Authentication" -MemberType NoteProperty -Value $AuthMethod
# Table Coulumn 5 - Error
$DeviceResultItem | Add-Member -Name "Error" -MemberType NoteProperty -Value $Errors
#Add line to the report
$DeviceResultsData += $DeviceResultItem
}
#OutGrid Preview of Results
$DeviceResultsData | Out-GridView -Title "Password Update Results"
#Append results to Password Change Results Document + Log
$DeviceResultsData | Export-Csv -Path "$home\Password Update Results.csv" -NoTypeInformation -append
$DeviceResultsData | Export-Csv -Path "$home\Password Update Results Log.csv" -NoTypeInformation -append
#Open Password Change Results CSV
Invoke-Item -Path "$home\Password Update Results.csv"
#Total time of script
$stopwatch