$Script:NeedModules = @( @{ Name = '7Zip4Powershell' version = '2.12.0' FindStr = '7Zip' } @{ Name = 'Microsoft.WinGet.Client' version = '1.29.280' FindStr = 'winget' } @{ Name = 'PSScriptAnalyzer' version = '1.25.0' FindStr = 'ScriptAnalyzer' } @{ Name = 'Pester' version = '6.0.1' FindStr = 'ScriptAnalyzer' } ) $Script:NeedSoft = @( @{ Name = '7-zip' ExeName = '7z.exe' WingetId = "7zip.7zip" Paths = @("$Env:ProgramFiles\*","${Env:ProgramFiles(x86)}\*") ExePath = '' } @{ Name = 'Aria' ExeName = 'aria2c.exe' WingetId = "aria2.aria2" Paths = @("$Env:ProgramFiles\*","${Env:ProgramFiles(x86)}\*") ExePath = '' } ) function Find-InstalledSoft { param( [Parameter(Mandatory)] [PsCustomObject]$Soft, [Parameter(Mandatory = $false)] [Boolean]$IsDebug = $false ) [PsCustomObject]$Result = @{ Code = -1 Msg = "Не удалось найти/установить $($Soft.Name)" } $WingetId = $($Soft.WingetId) if ($WingetId) { if ($IsDebug) { Write-Host "Запрашиваю $($Soft.Name) по WingetId: " -NoNewline -ForegroundColor Gray Write-Host "$WingetId ..." -ForegroundColor Cyan } $IsInstalleg = Get-WinGetPackage -Id $WingetId if (!($IsInstalleg)) { if ($IsDebug) { Write-Host "По WingetId: " -NoNewline -ForegroundColor Gray Write-Host "$WingetId " -NoNewline -ForegroundColor Cyan Write-Host "нет установленного" -ForegroundColor Red Write-Host "Ищу в сети по WingetId: " -NoNewline -ForegroundColor Gray Write-Host "$WingetId ..." -ForegroundColor Cyan } $IsFind = Find-WinGetPackage -Id aria2.aria2 | Select-Object -Property * -First 1 if ($IsFind) { if ($IsDebug) { Write-Host "По WingetId: " -NoNewline -ForegroundColor Gray Write-Host "$WingetId " -NoNewline -ForegroundColor Cyan Write-Host "найдено: "-ForegroundColor Cyan Write-Host "Имя: " -NoNewline -ForegroundColor Gray Write-Host "$( $IsFind.Name )" -ForegroundColor Green Write-Host "Версия: " -NoNewline -ForegroundColor Gray Write-Host "$( $IsFind.Version )" -ForegroundColor Green Write-Host "Источник: " -NoNewline -ForegroundColor Gray Write-Host "$( $IsFind.Source )" -ForegroundColor Green Write-Host "Устанавливаю"-ForegroundColor Cyan } Install-WinGetPackage -Id $WingetId -Force Start-Sleep -Seconds 4 $IsInstalleg = Get-WinGetPackage -Id $WingetId if (!($IsInstalleg)) { Write-Host "Почему-то $($Soft.Name) не установилось!" -ForegroundColor Red Start-Sleep -Seconds 10 } else { [PsCustomObject]$Result = @{ Code = 0 Msg = "Софт $($Soft.Name) установлен" OutPut = @() } } } } else { if ($IsDebug) { Write-Host "Софт: " -NoNewline -ForegroundColor Gray Write-Host " $( $SOFT.Name )" -NoNewline -ForegroundColor Cyan Write-Host " установлен!" -ForegroundColor Green } [PsCustomObject]$Result = @{ Code = 0 Msg = "Софт $($Soft.Name) установлен" OutPut = @() } } } else { foreach ($Path in $Soft.Paths) { if ($IsDebug) { Write-Host "Ищу $( $SOFT.Name ) в " -ForegroundColor Cyan } $SOFT.ExePath = Get-ChildItem -Path "$( $Path )" -Recurse -Filter "$( $Soft.ExeName )" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName -First 1 if ($SOFT.ExePath -and (Test-Path $( $SOFT.ExePath ))) { if ($IsDebug) { Write-Host "Софт: " -ForegroundColor Gray -NoNewline Write-Host "$( $SOFT.Name ) " -ForegroundColor Cyan -NoNewline Write-Host "найден: " -ForegroundColor Green -NoNewline Write-Host "$( $SOFT.ExePath )" -ForegroundColor Cyan } break } } } return $RESULT } function Find-InstalledModule { param( [Parameter(Mandatory)] [PsCustomObject]$Module, [Parameter(Mandatory = $false)] [Boolean]$IsDebug = $false ) if ($IsDebug) { Write-Host "Функция: " -ForegroundColor Gray -NoNewline Write-Host "Find-InstalledModule" -ForegroundColor Cyan Write-Host "Модуль: " -ForegroundColor Gray -NoNewline Write-Host "$( $Module.Name )" -ForegroundColor Cyan } $Result = @{ Code = -1 Msg = "Не удалось установить модуль $($Module.Name)" } try { if ($IsDebug) { Write-Host "Запрашиваю модуль $( $Module.Name ) версии $( $Module.Version ) ..." -ForegroundColor Cyan } $IsInstalled = Get-Module -ListAvailable | Where-Object { $_ -match $( $Module.Name ) } | Select-Object -First 1 if (!($IsInstalled)) { if ($IsDebug) { Write-Host "Модуль $( $Module.Name ) версии $( $Module.Version ) не установлен, ищу..." -ForegroundColor Yellow } $IsFind = Find-Module $( $Module.Name ) -MinimumVersion $( $Module.Version ) | Select-Object -First 1 if (!($IsFind)) { return @{ Code = -1 Msg = "Не удалось найти модуль $($Module.Name) версии $( $Module.Version )" } } else { if ($IsDebug) { Write-Host "Модуль $( $Module.Name ) версии $( $Module.Version ) найден, устанавливаю..." -ForegroundColor Yellow } $FPARAMS = @{ Name = $( $Module.Name ) MinimumVersion = $( $Module.Version ) Force = $true AllowClobber = $true AcceptLicense = $PSVersionTable.PSVersion.Major = 7 Verbose = $IsDebug } Install-Module @FPARAMS if ($IsDebug){ Write-Host "Модуль $( $Module.Name ) версии $( $Module.Version ) найден, импортирую..." -ForegroundColor Yellow } $FPARAMS = @{ Name = $( $Module.Name ) Force = $true DisableNameChecking = $true Verbose = $IsDebug } Import-Module @FPARAMS $IsInstalled = Get-Module -ListAvailable | Where-Object { $_ -match $( $Module.Name ) } | Select-Object -First 1 if (!($IsInstalled)) { return @{ Code = -1 Msg = "Модуль $($Module.Name) версии $( $Module.Version ) почему-то не установился" } } else { if ($IsDebug) { Write-Host "Модуль $( $Module.Name ) версии $( $Module.Version ) успешно установлен..." -ForegroundColor Green } return @{ Сode = 0 Msg = "Модуль $( $Module.Name ) версии $( $Module.Version ) успешно установлен..." } } } } else { #Write-Host "" -ForegroundColor Green return @{ Code = 0 Msg = "Модуль $( $Module.Name ) версии $( $Module.Version ) найден!" } } } catch { return @{ Code = -1 Msg = "Ошибка: $_" } } } function Initialize-Dependencies { param( [Parameter(Mandatory = $false)] [Boolean]$IsDebug = $false ) $Result = @{ Code = -1 Msg = 'Ок' OutPut = @() } $OUTPUT = @() try { foreach ($Module in $Script:NeedModules) { $Result = Find-InstalledModule -Module $Module -IsDebug $IsDebug #$Result if ($IsDebug) { Read-Host "`$IsDebug = $IsDebug" Write-Host "$( $Module.Name ): " -ForegroundColor Cyan $FPROBEL = " " * 9 Write-Host "$FPROBEL `$Result.Code: " -NoNewline -ForegroundColor Gray Write-Host "$( $Result.Code )" -ForegroundColor Cyan Write-Host "$FPROBEL `$Result.Msg: " -NoNewline -ForegroundColor Gray Write-Host "$( $Result.Msg )" -ForegroundColor Cyan } if (!($Result)) { return @{ Code = -1 Msg = "Неверный результат работы функции Find-InstalledModule, вызванной из Initialize-Dependencies" } } else { if ($($Result.Code) -ne 0 ) { return $Result } else { $OUT = "Модуль $( $Module.Name ) версии $( $Module.version ) успешно установлен" $OUTPUT += $OUT $Result = @{ Code = 0 Msg = 'Ok' OutPut = $OUTPUT } } } } foreach ($SOFT in $Script:NeedSoft) { $Result = Find-InstalledSoft $SOFT -IsDebug $IsDebug if (!($Result)) { return @{ Code = -1 Msg = "Неверный результат работы функции Find-InstalledSoft, вызванной из Initialize-Dependencies" } } else { if ($($Result.Code) -ne 0 ) { return $Result } else { $OUT = "Софт $( $SOFT.Name ) успешно установлен" $OUTPUT += $OUT $Result = @{ Code = 0 Msg = 'Ok' OutPut = $OUTPUT } } } } return $Result } catch { [PsCustomObject]$Result = @{ Code = -1 Msg = "Ошибка: $_" OutPut = @() } return $Result } } function Invoke-EndProg { Write-Host "Конец" -ForegroundColor Magenta $FRAVNO = "=" * 50 Write-Host $FRAVNO -ForegroundColor Magenta }