PSUtilities.psm1
Function Update-LocalModules { $Startscript = Get-Date Start-Transcript -Path .\UpdateModules$("{0:yyMMddHHmm}"-f (get-date)).log -Force <# .DESCRIPTION This Function updates local PowerShell modules with the last version online and deletes all the older local versions It registers all the activity in .\UpdateModules.log #> #Checks Internet Conectivity if (-not( [Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]'{DCB00C01-570F-4A9B-8D69-199FDBA5723B}')).IsConnectedToInternet )){Throw "The machine needs to be connected to the internet"} Write-Host "Checking online modules..." $OnlineModules = Find-Module $currentfreespace = [math]::Round( (Get-Ciminstance -ClassName win32_logicaldisk | Where DeviceId -eq "C:").FreeSpace / 1mb,2) #Gets current latest module version installed Write-Host "Looking for Installed Modules..." -Foregroundcolor Green $Latest = Get-InstalledModule foreach ($module in $Latest) { if ($module.name -in ($OnlineModules).name) { $OnlineModule = $OnlineModules | where name -EQ $module.name Write-Host "`nOnline module: " -ForegroundColor Green -NoNewline Write-Host "$(($OnlineModule).Version) $($OnlineModule.name)" -ForegroundColor Green Write-Host "Local Module: " -ForegroundColor Gray -NoNewline write-host "$(($module).Version) $($module.name)" -ForegroundColor Gray -NoNewline if ($module.Version -lt $OnlineModule.Version) { Write-Host "`nLocal version outdated, installing new online version... " -ForegroundColor Yellow -NoNewline Write-Host "$(($OnlineModule).Version) $($OnlineModule.name)" -ForegroundColor Green Install-Module -Name $OnlineModule.name -Force $module.version = $OnlineModule.Version } else {Write-Host " Ok" -ForegroundColor White} } $Oldlocalmodules = Get-InstalledModule $module.Name -AllVersions | Where-Object {$_.Version -ne $module.Version} if ($Oldlocalmodules -ne $null) {Write-Host "Old modules found. Unninstalling older modules..." -ForegroundColor Gray; $Oldlocalmodules | Uninstall-Module -Force -Verbose } } $cleanedDreespace = [math]::Round( (Get-Ciminstance -ClassName win32_logicaldisk | Where DeviceId -eq "C:").FreeSpace / 1mb,2) $freedspace = $cleanedDreespace - $currentfreespace Write-Host "`nFreed Space in C: $([math]::round($freedspace,2)) MB" -ForegroundColor Green Write-Host "Current Space in C: $([math]::round($cleanedDreespace,2)) MB" -ForegroundColor Green Write-Host "Time elapsed: $(((Get-Date) - $Startscript).Minutes) Minute(s)" -ForegroundColor Green Stop-Transcript } Export-ModuleMember -Function Update-LocalModules |