-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathStart-MSRA.ps1
224 lines (187 loc) · 7.79 KB
/
Start-MSRA.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#requires -version 2
<#
.SYNOPSIS
<None>
.DESCRIPTION
<None>
.INPUTS
<None>
.OUTPUTS
Fill a log file similar to $ScriptDir\[SCRIPTNAME]_[YYYY_MM_DD]_[HHhMMmSSs].log
.NOTES
Version: 1.2
Author: ALBERT Jean-Marc
Creation Date: 22/02/2017 (DD/MM/YYYY)
Purpose/Change: 1.0 - 2017.02.22 - ALBERT Jean-Marc - Initial script development
1.1 - 2017.02.27 - ALBERT Jean-Marc - Add combobox for Computer and User, thanks to request on Active Directory objects
Modify logged information thanks to modification somewhat above
Move buttons
Apply templace to this script
1.2 - 2017.02.27 - ALBERT Jean-Marc - Verify log's first line
Add #region to [Execution] region
.SOURCES
<None>
.EXAMPLE
<None>
#>
#region ---------------------------------------------------------[Initialisations]--------------------------------------------------------
Set-StrictMode -version Latest
#Set Error Action to Silently Continue
$ErrorActionPreference = "SilentlyContinue"
$launchDate = get-date -f "dd/MM/yyyy"
$launchHour = get-date -f "HH:mm:ss"
Import-Module ActiveDirectory
Add-Type -AssemblyName System.Windows.Forms
#endregion
#region ----------------------------------------------------------[Declarations]----------------------------------------------------------
$scriptName = [System.IO.Path]::GetFileName($scriptFile)
$scriptVersion = "1.2"
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$logFileName = "pmad.log"
$logPathName = "C:\temp\$logFileName"
$logFileFirstLine = "Date,Heure,PosteAdmin,Admin,Utilisateur,PosteUser,Ticket"
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$LDAPUserArray = Get-ADuser -SearchBase "OU=Users,DC=contorso,DC=dom" -Filter * | Select Name,SamAccountName | Sort Name
$LDAPComputerArray = Get-ADComputer -SearchBase "OU=Computers,DC=contorso,DC=dom" -Filter * | Select Name | Sort Name
#endregion
#region -----------------------------------------------------------[Functions]------------------------------------------------------------
function Set-FirstLine {
param (
[string]$Path,
[string]$Value
)
$oldcontent = Get-Content $Path
Set-Content -Path $Path -Value $Value
Add-Content -Path $Path -Value $oldcontent
}
#endregion
#region -----------------------------------------------------------[Execution]----------------------------------------------------------
#region Verify log's first line
$logFileFirstLineContent = Get-content $logPathName -First 1
if($logFileFirstLineContent -ne $logFileFirstLine){
Set-FirstLine -Path $logPathName -Value $logFileFirstLine
}
#endregion
#region Generate & show form
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Contrôle à distance"
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$form.StartPosition = "CenterScreen"
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$Form.TopMost = $false
$Form.Width = 460
$Form.Height = 305
$labelDescription = New-Object system.windows.Forms.Label
$labelDescription.Text = "Merci de remplir les champs ci-dessous afin de prendre la main
sur un poste CONTORSO.
Tous sont obligatoires."
$labelDescription.AutoSize = $true
$labelDescription.Width = 25
$labelDescription.Height = 10
$labelDescription.location = new-object system.drawing.point(10,20)
$labelDescription.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($labelDescription)
$LabelProd = New-Object system.windows.Forms.Label
$LabelProd.Text = "Administrateur"
$LabelProd.AutoSize = $true
$LabelProd.Width = 25
$LabelProd.Height = 10
$LabelProd.location = new-object system.drawing.point(24,101)
$LabelProd.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($LabelProd)
$textBoxProd = New-Object system.windows.Forms.TextBox
$textBoxProd.Text = $env:USERNAME
$textBoxProd.Enabled = $false
$textBoxProd.Width = 100
$textBoxProd.Height = 20
$textBoxProd.location = new-object system.drawing.point(125,101)
$textBoxProd.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($textBoxProd)
$textBoxProdComputer = New-Object system.windows.Forms.TextBox
$textBoxProdComputer.Text = $env:COMPUTERNAME
$textBoxProdComputer.Enabled = $false
$textBoxProdComputer.Width = 100
$textBoxProdComputer.Height = 20
$textBoxProdComputer.location = new-object system.drawing.point(265,101)
$textBoxProdComputer.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($textBoxProdComputer)
$LabelUtilisateur = New-Object system.windows.Forms.Label
$LabelUtilisateur.Text = "Utilisateur"
$LabelUtilisateur.AutoSize = $true
$LabelUtilisateur.Width = 25
$LabelUtilisateur.Height = 10
$LabelUtilisateur.location = new-object system.drawing.point(24,151)
$LabelUtilisateur.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($LabelUtilisateur)
$comboBoxUtilisateur = New-Object system.windows.Forms.ComboBox
$comboBoxUtilisateur.Text = "xxxN"
$comboBoxUtilisateur.Width = 220
$comboBoxUtilisateur.Height = 20
$comboBoxUtilisateur.location = new-object system.drawing.point(125,151)
$comboBoxUtilisateur.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($comboBoxUtilisateur)
foreach ($LDAPUser in $LDAPUserArray) {
$LDAPUserResult = "{0} ({1})" -f $LDAPUser.Name, $LDAPUser.SamAccountName
[void] $comboBoxUtilisateur.Items.Add($LDAPUserResult)
}
$LabelComputerName = New-Object system.windows.Forms.Label
$LabelComputerName.Text = "Poste"
$LabelComputerName.AutoSize = $true
$LabelComputerName.Width = 25
$LabelComputerName.Height = 10
$LabelComputerName.location = new-object system.drawing.point(23,188)
$LabelComputerName.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($LabelComputerName)
$comboBoxComputerName = New-Object system.windows.Forms.ComboBox
$comboBoxComputerName.Text = "Uxnnnnnnnnn"
$comboBoxComputerName.Width = 110
$comboBoxComputerName.Height = 20
$comboBoxComputerName.location = new-object system.drawing.point(125,189)
$comboBoxComputerName.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($comboBoxComputerName)
foreach ($LDAPComputer in $LDAPComputerArray) {
[void] $comboBoxComputerName.Items.Add($LDAPComputer.Name)
}
$labelTicket = New-Object system.windows.Forms.Label
$labelTicket.Text = "Ticket"
$labelTicket.AutoSize = $true
$labelTicket.Width = 25
$labelTicket.Height = 10
$labelTicket.location = new-object system.drawing.point(22,230)
$labelTicket.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($labelTicket)
$textBoxTicket = New-Object system.windows.Forms.TextBox
$textBoxTicket.Text = "xxxxx"
$textBoxTicket.Width = 110
$textBoxTicket.Height = 20
$textBoxTicket.location = new-object system.drawing.point(125,230)
$textBoxTicket.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($textBoxTicket)
$buttonStartRC = New-Object system.windows.Forms.Button
$buttonStartRC.Text = "Lancer contrôle à distance"
$buttonStartRC.Width = 140
$buttonStartRC.Height = 50
$buttonStartRC.Add_MouseClick({
$Result = "$env:COMPUTERNAME,$env:USERNAME,$($comboBoxUtilisateur.Text),$($textBoxComputerName.Text),$($textBoxTicket.Text)"
$launchDate + ',' + $launchHour + ',' +$env:COMPUTERNAME + ',' + $env:USERNAME + ',' + $comboBoxUtilisateur.Text + ',' + $comboBoxComputerName.Text + ',' + $textBoxTicket.Text | Out-File -filepath $logPathName -Append -Encoding UTF8
msra.exe /offerRA $comboBoxComputerName.Text
})
$buttonStartRC.location = new-object system.drawing.point(240,195)
$buttonStartRC.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonStartRC)
$buttonLog = New-Object system.windows.Forms.Button
$buttonLog.Text = "Log"
$buttonLog.Width = 60
$buttonLog.Height = 30
$buttonLog.location = new-object system.drawing.point(385,205)
$buttonLog.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonLog)
$buttonLog.Add_MouseClick({
Invoke-Item $logPathName
})
[void]$Form.ShowDialog()
$Form.Dispose()
#endregion
#endregion
#endregion