########################## # Writer: Davide Conforti # Company: PAL s.r.l. #------------------------- # Date: 2021/12/01 # v: 1.0 # Reason: Emission #------------------------- # Date: 2023/02/20 # v: 1.1 # Reason: Added winget functions #------------------------- ########################## #region Functions function Initialize-Graylog { Param ([string]$Facility) if(!(Test-Path -PathType Container -Path "C:\temp\")){ New-Item -ItemType Directory -Force -Path "C:\temp\" | Out-Null } $logFile = "C:\temp\$($Facility).log" if (Test-Path $logFile) { Remove-Item $logFile } try{ # Installing Graylog Module if(!(Get-InstalledModule -Name "PSGELF")){ Add-Content -Path $logFile -Value "Installing PSGELF module" Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted Install-Module -Name PSGELF Add-Content -Path $logFile -Value "PSGELF module installed!" } if(!(Get-Module -Name PSGELF)){ Add-Content -Path $logFile -Value "Importing PSGELF module" Import-Module -Name PSGELF Add-Content -Path $logFile -Value "PSGELF module imported!" } } catch{ Add-Content -Path $logFile -Value $_ } } function Initialize-Winget { $logString = ""; try{ if(!(Get-InstalledModule -Name winget)){ $logString += "Installing winget module...`r`n" $logString += Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force $logString += Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted $logString += Install-Module -Name winget $logString += "winget module installed!`r`n" } if(!(Get-Module -Name winget)){ $logString += "Importing winget module...`r`n" $logString += Import-Module -Name winget $logString += "winget module imported!`r`n" } } catch{ $logString += "$_`r`n" } return $logString } # New function Initialize-Icinga { $logString = ""; $module = "icinga-powershell-framework"; try{ if(!(Get-InstalledModule -Name $module)){ $logString += "Installing icinga module...`r`n" #$logString += Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force $logString += Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted $logString += Install-Module -Name $module $logString += "Icinga module installed!`r`n" } if(!(Get-Module -Name $module)){ $logString += "Importing icinga module...`r`n" $logString += Import-Module -Name $module $logString += "Icinga module imported!`r`n" } } catch{ $logString += "$_`r`n" } return $logString } function Write-Log { Param ([string]$Facility, [string]$LogString, [int32]$Level = 5) Initialize-Graylog $Facility Send-PSGelfUDP -GelfServer "palgraylog01.pal.local" -Port 12204 -ShortMessage $LogString -Level $Level -Facility $Facility } function Install-Software { Param ([string]$Facility, ` [string]$software, ` [string]$version, ` [string]$installPath, ` [string]$installArgument, ` [int32] $timeoutMinutes = 40, ` [bool]$mustUninstallOldVersions = $False, ` [bool]$forceInstallation = $False) try{ $upToDate = $False $logString = "`r`n" $logString += "Checking current installed version for $($software)... (needing $($version))`r`n" if(!$forceInstallation){ Get-Package -Name "$($software)" | Foreach-Object { if([System.Version]$_.Version -ge [System.Version]$version) { $logString += "-- Up to date with $($_.Name) v. $($_.Version)`r`n" $upToDate = $True } else { if($_) { $logString += "-- Found old version: $($_.Name) v. $($_.Version)" if($mustUninstallOldVersions){ # foreach ($uninstallPath in $uninstallPathArray) { $logString += " - Uninstalling it..." Write-Log $Facility $logString Uninstall-Package $_ #Start-Process -NoNewWindow -Wait -FilePath $uninstallPath -ArgumentList $uninstallArgument Write-Log $Facility "$($_.Name) v. $($_.Version) successfully uninstalled!" $logString = "" # } } else{ $logString += " `r `n" } } } } } if($logString){ Write-Log $Facility $logString } if(!$upToDate){ Write-Log $Facility "Installing $($software) v. $version" $timeoutMinutes = $timeoutMinutes * 60 $proc = Start-Process -WindowStyle Hidden -FilePath $installPath -ArgumentList $installArgument -Passthru $proc | Wait-Process -Timeout $timeoutMinutes -ErrorAction SilentlyContinue Write-Log $Facility "$($software) installation succesfull!`r`n" } } catch{ Write-Log $Facility $_ 3 } } function Set-WingetLocation { $wingetdir = (Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe" | Sort-Object -Property Path | Select-Object -Last 1) Set-Location $wingetdir } function Get-WingetInstalledSoftware { Set-WingetLocation $softwareList = .\winget.exe list --accept-source-agreements | Out-String return $softwareList } function Install-WingetSoftware { Param ([string]$SoftwareName, ` [string]$SoftwareId, ` [string[]] $InstalledSoftwareList) Set-WingetLocation $logString = "" try{ $logString += "Checking for $($softwareName) installation...`r`n" if($InstalledSoftwareList -like "*$($softwareId)*") { $logString += "$($softwareName) already installed!`r`n" } else { $logString += .\winget.exe install -h --accept-package-agreements --accept-source-agreements --id $softwareId | Out-String $logString += "`r`n$($softwareName) installed!`r`n" } } catch{ $logString += "$_`r`n" } return $logString } function Install-WingetUpdates { Set-WingetLocation $logString = "Checking for updates...`r`n" try{ $logString += .\winget.exe upgrade -h --all --accept-source-agreements | Out-String $logString += "Updates installed!`r`n" } catch{ $logString += "$_`r`n" } return $logString } function Add-Machine-EnvVar-PSModulePath($Path) { $Path = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine") + [IO.Path]::PathSeparator + $Path [Environment]::SetEnvironmentVariable( "PSModulePath", $Path, "Machine" ) } function Get-IsComputerMemberOf { Param ([string]$Group) try { #Get Computer's DN $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry $objSearcher.Filter = "(&(objectCategory=Computer)(SamAccountname=$($env:COMPUTERNAME)`$))" $obj = $objSearcher.FindOne() $Computer = $obj.Properties["distinguishedname"] #Now get the members of the group $objSearcher.Filter = "(&(objectCategory=group)(SamAccountname=$Group))" $obj = $objSearcher.FindOne() [String[]]$Members = $obj.Properties["member"] return $Members -contains $Computer } catch { return $False; } } #endregion