Install-SoftwareRemotely.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<#PSScriptInfo
 
.VERSION 1.0.1
 
.GUID 9d73a2b5-1329-42c0-b0ec-328198e3392d
 
.AUTHOR Juan Granados
 
.COPYRIGHT 2021 Juan Granados
 
.TAGS Install Software Remote Remotely
 
.LICENSEURI https://raw.githubusercontent.com/juangranados/powershell-scripts/main/LICENSE
 
.PROJECTURI https://github.com/juangranados/powershell-scripts/tree/main/Install%20Software%20Remotely
 
.RELEASENOTES
    Update examples
#>


<#
.SYNOPSIS
    Install software remotely in a group of computers and retry the installation in case of error.
.DESCRIPTION
    Install software remotely in a group of computers and retry the installation in case of error.
    It uses PowerShell to perform the installation. Target computer must allow Windows PowerShell Remoting.
    Script can try to enable Windows PowerShell Remoting using Microsoft Sysinternals Psexec with the paramenter -EnablePSRemoting.
    If PSExec is not found on computer, script asks to the user for download it and extract in system folder.
.PARAMETER AppPath
    Path to the application executable, It can be a network or local path because entire folder will be copied to remote computer before installing and deleted after installation.
    Example: 'C:\Software\TeamViewer\TeamvieverHost.msi' (Folder TeamViewer will be copied to remote computer before run ejecutable)
.PARAMETER AppArgs
    Application arguments to perform silent installation.
    Example: '/S /R settings.reg'
.PARAMETER LocalPath
    Local path of the remote computer where copy application directory.
    Default: 'C:\temp'
.PARAMETER Retries
    Number of times to retry failed installations.
    Default: 5
.PARAMETER TimeBetweenRetries
    Seconds to wait before retrying failed installations.
    Default: 60
.PARAMETER ComputerList
    List of computers in install software. You can only use one source of target computers: ComputerList, OU or CSV.
    Example: Computer001,Computer002,Computer003 (Without quotation marks)
.PARAMETER OU
    OU containing computers in which install software.
    RSAT for AD module for PowerShell must be installed in order to query AD.
    If you run script from a Domain Controller, AD module for PowerShell is already enabled.
    You can only use one source of target computers: ComputerList, OU or CSV.
    Example: 'OU=Test,OU=Computers,DC=CONTOSO,DC=COM'
.PARAMETER CSV
    CSV file containing computers in which install software. You can only use one source of target computers: ComputerList, OU or CSV.
    Example: 'C:\Scripts\Computers.csv'
    CSV Format:
                Name
                Computer001
                Computer002
                Computer003
.PARAMETER LogPath
    Path where save log file.
    Default: My Documents
.PARAMETER Credential
    Script will ask for an account to perform remote installation.
.PARAMETER EnablePSRemoting
    Try to enable PSRemoting on failed computers using Psexec. Psexec has to be on system path.
    If PSExec is not found. Script ask to download automatically PSTools and copy them to C:\Windows\System32.
.PARAMETER AppName
    App name as shown in registry to check if app is installed on remote computer and not reinstall it.
    You can check app name on a computer with it installed looking at:
    'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\'
    'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\'
    Example: 'TightVNC'
    Default: None
.PARAMETER AppVersion
    App name as shown in registry to check if app is installed on remote computer and not reinstall it.
    If not specified and AppName has a value, version will be ignored.
    You can check app version on a computer with it installed looking at:
    'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\'
    'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\'
    Example: '2.0.8.1'
    Default: all
.PARAMETER WMIQuery
    WMI Query to execute in remote computers. Software will be installed if query returns values.
    Example: 'select * from Win32_Processor where DeviceID="CPU0" and AddressWidth="64"' (64 bit computers)
    Example: 'select * from Win32_Processor where DeviceID="CPU0" and AddressWidth="32"' (32 bit computers)
    Default: None
.EXAMPLE
    TightVNC -> Install-SoftwareRemotely.ps1 -AppPath 'C:\Scripts\TightVNC\tightvnc-2.8.8-gpl-setup-64bit.msi' -AppArgs '/quiet /norestart ADDLOCAL="Server" SERVER_REGISTER_AS_SERVICE=1 SERVER_ADD_FIREWALL_EXCEPTION=1 SERVER_ALLOW_SAS=1 SET_USEVNCAUTHENTICATION=1 VALUE_OF_USEVNCAUTHENTICATION=1 SET_PASSWORD=1 VALUE_OF_PASSWORD=Password.01 SET_USECONTROLAUTHENTICATION=1 VALUE_OF_USECONTROLAUTHENTICATION=1 SET_CONTROLPASSWORD=1 VALUE_OF_CONTROLPASSWORD=3digits.01' -OU 'OU=Central,OU=Computers,DC=Contoso,DC=local' -Retries 2 -AppName 'TightVNC' -AppVersion '2.8.8.0' -EnablePSRemoting -WMIQuery 'select * from Win32_Processor where DeviceID="CPU0" and AddressWidth="64"'
.EXAMPLE
    TightVNC Mirage Driver -> Install-SoftwareRemotely.ps1 -AppPath 'C:\Scripts\TightVNC\dfmirage-setup-2.0.301.exe' -AppArgs '/verysilent /norestart' -OU 'OU=Central,OU=Computers,OU=MyBusiness,DC=Contoso,DC=local' -Retries 2 -AppName 'DemoForge Mirage Driver for TightVNC 2.0' -AppVersion '2.0' -EnablePSRemoting -WMIQuery 'select * from Win32_Processor where DeviceID="CPU0" and AddressWidth="64"'
.EXAMPLE
    Install-SoftwareRemotely.ps1 -AppPath "C:\Temp\Software\Miranda\miranda-im-v0.10.75-unicode.exe" -AppArgs "/S" -ComputerList Computer001,Computer002,Computer003 -AppName "Miranda IM 0.10.75" -AppVersion "0.10.75"
.EXAMPLE
    Install-SoftwareRemotely.ps1 -AppPath "C:\Temp\Software\Miranda\miranda-im-v0.10.75-unicode.exe" -AppArgs "/S" -CSV "C:\Computers.csv" -Credential -EnablePSRemoting
.EXAMPLE
    Install-SoftwareRemotely.ps1 -AppPath "\\Server01\Software\Miranda\miranda-im-v0.10.75-unicode.exe" -AppArgs "/S" -OU "OU=Test,OU=Computers,DC=CONTOSO,DC=COM"
.NOTES
    Author: Juan Granados
    Date: July 2021
#>


Param(
        [Parameter(Mandatory=$true,Position=0)] 
        [ValidateNotNullOrEmpty()]
        [string]$AppPath,
        [Parameter(Mandatory=$false,Position=1)] 
        [ValidateNotNullOrEmpty()]
        [string]$AppArgs="None",
        [Parameter(Mandatory=$false,Position=2)] 
        [ValidateNotNullOrEmpty()]
        [string]$LocalPath="C:\temp",
        [Parameter(Mandatory=$false,Position=3)] 
        [ValidateNotNullOrEmpty()]
        [int]$Retries=5,
        [Parameter(Mandatory=$false,Position=4)] 
        [ValidateNotNullOrEmpty()]
        [int]$TimeBetweenRetries=60,
        [Parameter(Mandatory=$false,Position=5)] 
        [ValidateNotNullOrEmpty()]
        [string[]]$ComputerList,
        [Parameter(Mandatory=$false,Position=6)] 
        [ValidateNotNullOrEmpty()]
        [string]$OU,
        [Parameter(Mandatory=$false,Position=7)] 
        [ValidateNotNullOrEmpty()]
        [string]$CSV,
        [Parameter(Mandatory=$false,Position=7)] 
        [ValidateNotNullOrEmpty()]
        [string]$LogPath=[Environment]::GetFolderPath("MyDocuments"),
        [Parameter(Position=9)] 
        [switch]$EnablePSRemoting,
        [Parameter(Position=10)] 
        [switch]$Credential,
        [Parameter(Mandatory=$false,Position=11)] 
        [ValidateNotNullOrEmpty()]
        [string]$AppName="None",
        [Parameter(Mandatory=$false,Position=12)] 
        [ValidateNotNullOrEmpty()]
        [string]$AppVersion="all",
        [Parameter(Mandatory=$false,Position=13)] 
        [ValidateNotNullOrEmpty()]
        [string]$WMIQuery="None"
    )

#Requires -RunAsAdministrator

#Functions

Add-Type -AssemblyName System.IO.Compression.FileSystem

function Unzip
{
    param([string]$zipfile, [string]$outpath)
    
    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}

Function Copy-WithProgress
{
  Param([string]$Source,[string]$Destination)

    $Source=$Source.tolower()
    $Filelist=Get-Childitem $Source â€“Recurse
    $Total=$Filelist.count
    $Position=0
    If(!(Test-Path $Destination)){
        New-Item $Destination -Type Directory | Out-Null
    }
    foreach ($File in $Filelist){
        $Filename=$File.Fullname.tolower().replace($Source,'')
        $DestinationFile=($Destination+$Filename)
        try{
  Copy-Item $File.FullName -Destination $DestinationFile -Force
        }catch{throw $_.Exception}
        $Position++
        Write-Progress -Activity "Copying data from $source to $Destination" -Status "Copying File $Filename" -PercentComplete (($Position/$Total)*100)
  }
}

Function Set-Message([string]$Text,[string]$ForegroundColor="White",[int]$Append=$True){

    if ($Append){
        $Text | Out-File $LogPath -Append
    }
    else {
        $Text | Out-File $LogPath
    }
    Write-Host $Text -ForegroundColor $ForegroundColor
}

function Get-InstalledApps
{
    if ([IntPtr]::Size -eq 4) {
        $regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    }
    else {
        $regpath = @(
            'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
            'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
        )
    }
    Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} | 
        Select DisplayName, Publisher, InstallDate, DisplayVersion, UninstallString | 
        Sort DisplayName
    #$result = Get-InstalledApps | where {$_.DisplayName -like $appToMatch}

}


Function CheckSoftwareInstalled([string]$Computer){
    If ($Cred){
        try{
            Return Invoke-Command -computername $Computer -ScriptBlock { 
                $AppName = $args[0]
                $AppVersion = $args[1]              
                if ([IntPtr]::Size -eq 4) {
                    $regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
                }
                else {
                    $regpath = @(
                        'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
                        'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
                    )
                }
                $InstalledApps = Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} | 
                    Select DisplayName, Publisher, InstallDate, DisplayVersion, UninstallString | 
                    Sort DisplayName
                If ($AppVersion -ne "all"){
                    Return $InstalledApps | where {$_.DisplayName -eq $AppName -and $_.DisplayVersion -eq  $AppVersion}
                }
                Else{
                    Return $InstalledApps | where {$_.DisplayName -eq $AppName}
                }
            } -ArgumentList $AppName, $AppVersion -Credential $Cred
        }catch{throw $_.Exception}
    }
    else{
        try{
            Return Invoke-Command -computername $Computer -ScriptBlock {
                $AppName = $args[0]
                $AppVersion = $args[1]              
                if ([IntPtr]::Size -eq 4) {
                    $regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
                }
                else {
                    $regpath = @(
                        'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
                        'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
                    )
                }
                $InstalledApps = Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} | 
                    Select DisplayName, Publisher, InstallDate, DisplayVersion, UninstallString | 
                    Sort DisplayName
                If ($AppVersion -ne "all"){
                    Return $InstalledApps | where {$_.DisplayName -eq $AppName -and $_.DisplayVersion -eq  $AppVersion}
                }
                Else{
                    Return $InstalledApps | where {$_.DisplayName -eq $AppName}
                }
            } -ArgumentList $AppName, $AppVersion
        }catch{throw $_.Exception}
    }
}

Function CheckWMIQuery([string]$Computer){
    If ($Cred){
        try{
            Return Invoke-Command -computername $Computer -ScriptBlock { 
                $WMIQuery = $args[0]
                Write-Host "Executing $($WMIQuery)"
                Return gwmi -Query $WMIQuery
            } -ArgumentList $WMIQuery -Credential $Cred
        }catch{throw $_.Exception}
    }
    else{
        try{
            Return Invoke-Command -computername $Computer -ScriptBlock { 
                $WMIQuery = $args[0]
                Write-Host "Executing $($WMIQuery)"
                Return gwmi -Query $WMIQuery
            } -ArgumentList $WMIQuery
        }catch{throw $_.Exception}   
    }
}

Function InstallRemoteSoftware([string]$Computer){
    If ($Cred){
        try{
            Return Invoke-Command -computername $Computer -ScriptBlock {
                $Application = $args[0]
                $AppArgs = $args[1]
                $ApplicationName = $Application.Substring($Application.LastIndexOf('\')+1)
                $ApplicationFolderPath = $Application.Substring(0,$Application.LastIndexOf('\'))
                $ApplicationExt = $Application.Substring($Application.LastIndexOf('.')+1)
                Write-Host "Installing $($ApplicationName) on $($env:COMPUTERNAME)"
                If($ApplicationExt -eq "msi"){
                    If ($AppArgs -ne "None"){
                         Write-Host "Installing as MSI: msiexec /i $($Application) $($AppArgs)"
                        $p = Start-Process "msiexec" -ArgumentList "/i $($Application) $($AppArgs)" -Wait -Passthru
                    }
                    else{
                        Write-Host "Installing as MSI: msiexec /i $($Application)"
                        $p = Start-Process "msiexec" -ArgumentList "/i $($Application) /quiet /norestart" -Wait -Passthru
                    }
                }
                ElseIf ($AppArgs -ne "None"){
                    Write-Host "Executing $Application $AppArgs"
                    $p = Start-Process $Application -ArgumentList $AppArgs -Wait -Passthru
                }
                Else{
                    Write-Host "Executing $Application"
                    $p = Start-Process $Application -Wait -Passthru
                }
                $p.WaitForExit()
                if ($p.ExitCode -ne 0) {
                    Write-Host "Failed installing with error code $($p.ExitCode)" -ForegroundColor Red
                    $Return = $($env:COMPUTERNAME)
                }
                else{
                    $Return = 0
                }
                Write-Host "Deleting $($ApplicationFolderPath)"
                Remove-Item $($ApplicationFolderPath) -Force -Recurse
                Return $Return
            } -ArgumentList "$($LocalPath)\$($ApplicationFolderName)\$($ApplicationName)", $AppArgs -Credential $Cred
        }catch{throw $_.Exception}
    }
    else{
        try{
            Return Invoke-Command -computername $Computer -ScriptBlock {
                $Application = $args[0]
                $AppArgs = $args[1]
                $ApplicationName = $Application.Substring($Application.LastIndexOf('\')+1)
                $ApplicationFolderPath = $Application.Substring(0,$Application.LastIndexOf('\'))
                $ApplicationExt = $Application.Substring($Application.LastIndexOf('.')+1)
                Write-Host "Installing $($ApplicationName) on $($env:COMPUTERNAME)"
                If($ApplicationExt -eq "msi"){
                    If ($AppArgs -ne "None"){
                         Write-Host "Installing as MSI: msiexec /i $($Application) $($AppArgs)"
                        $p = Start-Process "msiexec" -ArgumentList "/i $($Application) $($AppArgs)" -Wait -Passthru
                    }
                    else{
                        Write-Host "Installing as MSI: msiexec /i $($Application)"
                        $p = Start-Process "msiexec" -ArgumentList "/i $($Application) /quiet /norestart" -Wait -Passthru
                    }
                }
                ElseIf ($AppArgs -ne "None"){
                    Write-Host "Executing $Application $AppArgs"
                    $p = Start-Process $Application -ArgumentList $AppArgs -Wait -Passthru
                }
                Else{
                    Write-Host "Executing $Application"
                    $p = Start-Process $Application -Wait -Passthru
                }
                $p.WaitForExit()
                if ($p.ExitCode -ne 0) {
                    Write-Host "Failed installing with error code $($p.ExitCode)" -ForegroundColor Red
                    $Return = $($env:COMPUTERNAME)
                }
                else{
                    $Return = 0
                }
                Write-Host "Deleting $($ApplicationFolderPath)"
                Remove-Item $($ApplicationFolderPath) -Force -Recurse
                Return $Return
            } -ArgumentList "$($LocalPath)\$($ApplicationFolderName)\$($ApplicationName)", $AppArgs
        }catch{throw $_.Exception}
    }
}

Function CheckPSRemoting([string]$Computer){
    If ($EnablePSRemoting){
        Set-Message "Enabling PSRemoting on computer: psexec.exe /accepteula -h -d \\$($Computer) -s powershell Enable-PSRemoting"
        try{
            psexec.exe /accepteula -h -d "\\$($Computer)" -s powershell Enable-PSRemoting -Force 2>&1 | Out-Null
        }catch{
            Set-Message "PSExec running on background. Continue with next computer."
        }
    }
    Else{
        Set-Message "You can try to enable PowerShell Remoting on computer using parameter -EnablePSRemoting" -ForegroundColor DarkYellow
    }
}

$ErrorActionPreference = "Stop"

#Initialice log

$LogPath += "\InstallSoftwareRemotely_" + $(get-date -Format "yyyy-mm-dd_hh-mm-ss") + ".txt"
Set-Message "Start remote installation on $(get-date -Format "yyyy-mm-dd hh:mm:ss")" -Append $False

#Initial validations.

If (!(Test-Path $AppPath)){
    Set-Message "Error accessing $($AppPath). The script can not continue"
    Exit 1
}
If ($EnablePSRemoting){
    if (!(Get-Command "psexec.exe" -ErrorAction SilentlyContinue)){ 
        Set-Message "Error. Microsoft Psexec not found on system. Download it from https://download.sysinternals.com/files/PSTools.zip and extract all in C:\Windows\System32" -ForegroundColor Yellow
        $Answer=Read-Host "Do you want to download and install PSTools (y/n)?"
        if (($Answer -eq "y") -or ($Answer -eq "Y")){
            Set-Message "Downloading PSTools"
            If (Test-Path "$($env:temp)\PSTools.zip"){
                Remove-Item "$($env:temp)\PSTools.zip" -Force
            }
            (New-Object System.Net.WebClient).DownloadFile("https://download.sysinternals.com/files/PSTools.zip", "$($env:temp)\PSTools.zip")
            if (Test-Path "$($env:temp)\PSTools.zip"){
                Set-Message "Unzipping PSTools"
                If (Test-Path "$($env:temp)\PSTools"){
                    Remove-Item "$($env:temp)\PSTools" -Force -Recurse
                }
                Unzip "$($env:temp)\PSTools.zip" "$($env:temp)\PSTools" 
                Copy-Item "$($env:temp)\PSTools\*.exe" "$($env:SystemRoot)\System32" -Force
                if (Test-Path "$($env:SystemRoot)\System32\psexec.exe"){
                    Set-Message "PSTools installed" -ForegroundColor Green 
                }
                else{
                    Set-Message "Error unzipping PSTools" -ForegroundColor Red
                    Remove-Item "$($env:temp)\PSTools.zip" -Force
                    Exit 1
                }
            }
            else{
                Set-Message "Error downloading PSTools" -ForegroundColor Red
                Exit 1
            }
        }
        else{
            Exit 1
        }
    }
}
If ($OU){
    if (!(Get-Command "Get-ADComputer" -ErrorAction SilentlyContinue)){ 
        Set-Message "Error. Get-ADComputer not found on system. You have to install the PowerShell Active Directory module order to query Active Directory. https://4sysops.com/archives/how-to-install-the-powershell-active-directory-module/" -ForegroundColor Red
        Exit 1
    }
    try{
        $ComputerList = Get-ADComputer -Filter * -SearchBase "$OU" | Select-Object -Expand name
    }catch{
        Set-Message "Error querying AD: $($_.Exception.Message)" -ForegroundColor Red
        Exit 1
    }
}
ElseIf ($CSV){
    try{
        $ComputerList = Get-Content $CSV | where {$_ -notmatch 'Name'} | Foreach-Object {$_ -replace '"', ''}
    }catch{
        Set-Message "Error getting CSV content: $($_.Exception.Message)" -ForegroundColor Red
        Exit 1
    }
}
ElseIf(!$ComputerList){
    Set-Message "You have to set a list of computers, OU or CSV." -ForegroundColor Red
    Exit 1
}
If ($Credential){
    $Cred = Get-Credential
}
If(!$Cred -or !$Credential){
    Set-Message "No credential specified. Using logon account"
}
Else{
    Set-Message "Using user $($Cred.UserName)"
}

$ApplicationName = $AppPath.Substring($AppPath.LastIndexOf('\')+1)
$ApplicationFolderPath = $AppPath.Substring(0,$AppPath.LastIndexOf('\'))
$ApplicationFolderName = $ApplicationFolderPath.Substring($ApplicationFolderPath.LastIndexOf('\')+1)
$ComputerWithError = [System.Collections.ArrayList]@()
$ComputerWithSuccess = [System.Collections.ArrayList]@()
$ComputerSkipped = [System.Collections.ArrayList]@()
$TotalRetries = $Retries
$TotalComputers = $ComputerList.Count
Do{
    Set-Message "-----------------------------------------------------------------"
    Set-Message "Attempt $(($TotalRetries - $Retries) +1) of $($TotalRetries)" -ForegroundColor Cyan
    Set-Message "-----------------------------------------------------------------"
    $Count = 1
    ForEach ($Computer in $ComputerList){
        Set-Message "COMPUTER $($Computer.ToUpper()) ($($Count) of $($ComputerList.Count))" -ForegroundColor Yellow
        $Count++
        If($AppName -ne "None"){
            Set-Message "Checking if $($AppName) version $($AppVersion) is installed on remote computer."
            try{
                If(CheckSoftwareInstalled $Computer){
                    Set-Message "Software found on computer. Skipping installation." -ForegroundColor Green
                    $ComputerSkipped.Add($Computer) | Out-Null
                    Continue
                }
                Else{
                    Set-Message "Software not found on remote computer."
                }
            }catch{
                Set-Message "Error connecting: $($_.Exception.Message)" -ForegroundColor Red
                CheckPSRemoting $Computer
                $ComputerWithError.Add($Computer) | Out-Null
                Continue
            }
        }
        If($WMIQuery -ne "None"){
            Set-Message "Checking WMI Query on remote computer."
            try{
                If(!(CheckWMIQuery $Computer)){
                    Set-Message "WMI Query result is false. Skipping installation."
                    $ComputerSkipped.Add($Computer) | Out-Null
                    Continue
                }
                Else{
                    Set-Message "WMI Query result is true. Continue installation."
                }
            }catch{
                Set-Message "Error connecting: $($_.Exception.Message)" -ForegroundColor Red
                CheckPSRemoting $Computer
                $ComputerWithError.Add($Computer) | Out-Null
                Continue
            }
        }
        Set-Message "Coping $($ApplicationFolderPath) to \\$($Computer)\$($LocalPath -replace ':','$')"
        try{
            Copy-WithProgress "$ApplicationFolderPath" "\\$($Computer)\$("$($LocalPath)\$($ApplicationFolderName)" -replace ':','$')"
        }catch{
                Set-Message "Error copying folder: $($_.Exception.Message)" -ForegroundColor Red
                $ComputerWithError.Add($Computer) | Out-Null
                Continue;
            }
        try{
            $ExitCode = InstallRemoteSoftware $Computer
            If ($ExitCode){
                $ComputerWithError.Add($Computer) | Out-Null
                Set-Message "Error installing $($ApplicationName)." -ForegroundColor Red
            }
            else{
                Set-Message "$($ApplicationName) installed successfully." -ForegroundColor Green
                $ComputerWithSuccess.Add($Computer) | Out-Null
            }
            }catch{
                Set-Message "Error on remote execution: $($_.Exception.Message)" -ForegroundColor Red
                $ComputerWithError.Add($Computer) | Out-Null
                try{
                    Set-Message "Deleting \\$($Computer)\$($LocalPath -replace ':','$')\$($ApplicationFolderName)"
                }catch{
                    Set-Message "Error on remote deletion: $($_.Exception.Message)" -ForegroundColor Red
                }
                Remove-Item "\\$($Computer)\$($LocalPath -replace ':','$')\$($ApplicationFolderName)" -Force -Recurse
                CheckPSRemoting $Computer
            }
    }
    If ($ComputerWithError.Count -eq 0){
        break
    }
    $Retries--
    If ($Retries -gt 0){
        $ComputerList=$ComputerWithError
        $ComputerWithError = [System.Collections.ArrayList]@()
        If ($TimeBetweenRetries -gt 0){
            Set-Message "Waiting $($TimeBetweenRetries) seconds before next retry..."
            Sleep $TimeBetweenRetries
        }
    }
}While ($Retries -gt 0)

If($ComputerWithError.Count -gt 0){
    Set-Message "-----------------------------------------------------------------"
    Set-Message "Error installing $($ApplicationName) on $($ComputerWithError.Count) of $($TotalComputers) computers:"
    Set-Message $ComputerWithError
    $csvContents = @()
    ForEach($Computer in $ComputerWithError){
        $row = New-Object System.Object
        $row | Add-Member -MemberType NoteProperty -Name "Name" -Value $Computer
        $csvContents += $row
    }
    $CSV=(get-date).ToString('yyyyMMdd-HH_mm_ss') + "ComputerWithError.csv"
    $csvContents | Export-CSV -notype -Path "$([Environment]::GetFolderPath("MyDocuments"))\$($CSV)" -Encoding UTF8
    Set-Message "Computers with error exported to CSV file: $([Environment]::GetFolderPath("MyDocuments"))\$($CSV)" -ForegroundColor DarkYellow
    Set-Message "You can retry failed installation on this computers using parameter -CSV $([Environment]::GetFolderPath("MyDocuments"))\$($CSV)" -ForegroundColor DarkYellow
}
If ($ComputerWithSuccess.Count -gt 0){
    Set-Message "-----------------------------------------------------------------"
    Set-Message "$([math]::Round((($ComputerWithSuccess.Count * 100) / $TotalComputers), [System.MidpointRounding]::AwayFromZero) )% Success installing $($ApplicationName) on $($ComputerWithSuccess.Count) of $($TotalComputers) computers:"
    Set-Message $ComputerWithSuccess
}
Else{
    Set-Message "-----------------------------------------------------------------"
    Set-Message "Installation of $($ApplicationName) failed on all computers" -ForegroundColor Red
}
If ($ComputerSkipped.Count -gt 0){
    Set-Message "-----------------------------------------------------------------"
    Set-Message "$($ComputerSkipped.Count) skipped of $($TotalComputers) computers:"
    Set-Message $ComputerSkipped
}