diff --git a/MDT-WinUpdate.ps1 b/MDT-WinUpdate.ps1 new file mode 100644 index 0000000..a00d525 --- /dev/null +++ b/MDT-WinUpdate.ps1 @@ -0,0 +1,144 @@ +# Run Windows Update +# iRaven +# 2025-07-30 + +<# +.SYNOPSIS +Updates windows with PSWindowsUpdate +.PARAMETER MDT +Set this if being used with MDT so logs get put in the right place +.PARAMETER Reboot +Allow system reboots +#> + +## Initial Tasks +param( + [switch] $MDT, + [switch] $Reboot +) + +$ErrorActionPreference = "Continue" +$PSModulePathCU = $env:PSModulePath.split(';')[0] # CurrentUser PSModule path +$PSModulePathAU = $env:PSModulePath.split(';')[1] # All Users PSModule path +if ($MDT){ + $MDTPath = $Global:TSEnv.Value("DeployRoot") +} +$WUServer = (Get-ItemProperty HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate).WUServer + +## Functions + +Function ImportPSWindowsUpdate { + Write-Host -ForegroundColor Yellow "Importing PS Windows Update module..." + # Write-LogEntry -Value "Importing PS Windows Update Module..." -Severity 1 -Component "ImportPSWindowsUpdate" -FileName $ScriptLog + try { + if (!(Test-Path -Path $PSModulePathAU\PSWindowsUpdate -PathType Container)){ + # Get from PSGallery/NuGet + Write-Host -ForegroundColor Yellow "Installing PSWindowsUpdate from PSGallery" + if (!(Get-PackageProvider -ListAvailable -Name 'NuGet' -ErrorAction Ignore)) { + Write-Host -ForegroundColor Yellow 'Installing NuGet package provider...' + Install-PackageProvider -Name 'NuGet' -Force + Install-Module -Name PSWindowsUpdate -Scope AllUsers -Force + } else { + Install-Module -Name PSWindowsUpdate -Scope AllUsers -Force + } + } + Import-Module PSWindowsUpdate -Force + Write-Host -ForegroundColor Green "PSWindowsUpdate module imported!" + Write-LogEntry -Value "PSWindowsUpdate module imported!" -Severity 1 -Component "ImportPSWindowsUpdate" -FileName $ScriptLog + return $true + } catch { + Write-LogEntry -Value "Failed to import PSWindowsUpdate!" -Severity 3 -Component "ImportPSWindowsUpdate" -FileName $ScriptLog + Write-Host -ForegroundColor Red "Failed to import PSWindowsUpdate!" + return $false + } +} + +Function RunWinUpdate { # Run Windows Updates + if(ImportPSWindowsUpdate){ + try { + Write-LogEntry -Value "Installing all available Windows Updates..." -Severity 1 -Component "RunWinUpdate" -FileName $ScriptLog + Write-Host -ForegroundColor Yellow "Installing all available Windows Updates..." + if ($WUServer -and !($Reboot)){ + Install-WindowsUpdate -AcceptAll -IgnoreReboot + #Requires -RunAsAdministrator + } elseif ($WUServer -and $Reboot) { + Install-WindowsUpdate -AcceptAll -AutoReboot + #Requires -RunAsAdministrator + } elseif (($null -eq $WUServer) -and $Reboot){ + Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot + #Requires -RunAsAdministrator + } else { + Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot + #Requires -RunAsAdministrator + } + return $true + } catch { + Write-Host -ForegroundColor Red "Failed installing all available Windows updates!" + Write-LogEntry -Value "Failed installing all available Windows updates!" -Severity 3 -Component "RunWinUpdate" -FileName $ScriptLog + } + } else { + Write-LogEntry -Value "Failed to update Windows due to failed import of PSWindowsUpdate PS module." -Severity 3 -Component "RunWinUpdate" -FileName $ScriptLog + return $false + } +} + +Function Write-LogEntry +{ + #Write data to a CMTrace compatible log file. (Credit to SCConfigMgr - https://www.scconfigmgr.com/) + + param( + [parameter(Mandatory = $true, HelpMessage = "Value added to the log file.")] + [ValidateNotNullOrEmpty()] + [string]$Value, + [parameter(Mandatory = $true, HelpMessage = "Severity for the log entry. 1 for Informational, 2 for Warning and 3 for Error.")] + [ValidateNotNullOrEmpty()] + [ValidateSet("1", "2", "3")] + [string]$Severity, + [parameter(Mandatory = $true, HelpMessage = "Component of the log file.")] + [ValidateNotNullOrEmpty()] + [string]$Component, + [parameter(Mandatory = $false, HelpMessage = "Name of the log file that the entry will written to.")] + [ValidateNotNullOrEmpty()] + [string]$FileName + ) + #Determine log file location + $LogFilePath = $FileName + #Construct time stamp for log entry + if(-not(Test-Path -Path 'variable:global:TimezoneBias')) + { + [string]$global:TimezoneBias = [System.TimeZoneInfo]::Local.GetUtcOffset((Get-Date)).TotalMinutes + if($TimezoneBias -match "^-") + { + $TimezoneBias = $TimezoneBias.Replace('-', '+') + } + else + { + $TimezoneBias = '-' + $TimezoneBias + } + } + $Time = -join @((Get-Date -Format "HH:mm:ss.fff"), $TimezoneBias) + #Construct date for log entry + $Date = (Get-Date -Format "MM-dd-yyyy") + #Construct context for log entry + $Context = $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name) + #Construct final log entry + $LogText = "" + #Add value to log file + try + { + Out-File -InputObject $LogText -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop + } + catch [System.Exception] + { + Write-Warning -Message "Unable to append log entry to $FileName file. Error message at line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)" + } +} + + +# Main Program +if ($MDT){ + $ScriptLog = "$MDTPath\_Logs\$env:ComputerName-WindowsUpdate.log" +} else { + $ScriptLog = "$env:SystemDrive\irnh\PSWindowsUpdate.log" +} +RunWinUpdate \ No newline at end of file diff --git a/Nextcloud-Updater.ps1 b/Nextcloud-Updater.ps1 new file mode 100644 index 0000000..1af70ae --- /dev/null +++ b/Nextcloud-Updater.ps1 @@ -0,0 +1,68 @@ +# Script to install/update Nextcloud desktop client +# Technolog Networks - 2025-10-18 +# iRaven + +<# +.SYNOPSIS +Updates Nextcloud Client +.PARAMETER Install +Boolean to install the client if not installed +#> + +Start-Transcript -Path "$env:systemdrive\irnh\Nextcloud-Updater.log" -Append +$PkgInfo = Get-Package -Name "Nextcloud*" +$LatestVer = (irm https://api.github.com/repos/nextcloud-releases/desktop/releases/latest).tag_name +$LatestVerURL = (irm https://api.github.com/repos/nextcloud-releases/desktop/releases/latest).assets.browser_download_url -like "*.msi" + + +Function IsLatestVer { + if (($LatestVer.Substring(1)) -gt ($PkgInfo.Version.Substring(0,6))){ + return $false + } else { + return $true + } +} + +Function DownloadLatestVer { + try { + if (!(Test-Path "$env:systemdrive\irnh\Nextcloud-$LatestVer.msi")) { + cmd.exe /c "curl -L -o $env:systemdrive\irnh\Nextcloud-$LatestVer.msi $LatestVerURL" + } + } catch { + write-host -ForegroundColor red "Couldn't download the latest installer at URL $LatestVerURL !" + } +} + +Function InstallLatestVer { + try { + Write-Host -ForegroundColor Yellow "Installing Nextcloud Client $LatestVer" + $NCInstall = Start-Process msiexec -Wait -PassThru -ArgumentList "/passive /i $env:systemdrive\irnh\Nextcloud-$LatestVer.msi REBOOT=ReallySuppress" + write-host "returned exit code" $NCInstall.ExitCode + if (0 -in $NCInstall.ExitCode) { + Write-Host -ForegroundColor Green "Successfully installed!" + del "$env:SystemDrive\irnh\Nextcloud-$LatestVer.msi" + } + } catch { + write-host "it no worky" + } +} + +if (!($PkgInfo) -and !($Install)) { # Not installed, no install flag + Write-Host -ForegroundColor Yellow "Nextcloud not installed & not installing it." +} elseif (!($PkgInfo) -and $Install) { # Not installed, with install flag + Write-Host -ForegroundColor Yellow "Nextcloud not installed - installing latest version $LatestVer" + DownloadLatestVer + InstallLatestVer +} else { + Write-Host -ForegroundColor Yellow "Nextcloud installed with" $PkgInfo.Version + if (IsLatestVer) { + Write-Host -ForegroundColor Green "The currently installed version is the latest version" + } else { + Write-Host -ForegroundColor Yellow "The currently installed version is NOT the latest version - installing $LatestVer" + DownloadLatestVer + InstallLatestVer + } +} + +Write-Host "Exiting" +Stop-Transcript \ No newline at end of file diff --git a/testing/CMTraceLog.ps1 b/testing/CMTraceLog.ps1 new file mode 100644 index 0000000..c2fb956 --- /dev/null +++ b/testing/CMTraceLog.ps1 @@ -0,0 +1,51 @@ +Function Write-LogEntry +{ + #Write data to a CMTrace compatible log file. (Credit to SCConfigMgr - https://www.scconfigmgr.com/) + + param( + [parameter(Mandatory = $true, HelpMessage = "Value added to the log file.")] + [ValidateNotNullOrEmpty()] + [string]$Value, + [parameter(Mandatory = $true, HelpMessage = "Severity for the log entry. 1 for Informational, 2 for Warning and 3 for Error.")] + [ValidateNotNullOrEmpty()] + [ValidateSet("1", "2", "3")] + [string]$Severity, + [parameter(Mandatory = $true, HelpMessage = "Component of the log file.")] + [ValidateNotNullOrEmpty()] + [string]$Component, + [parameter(Mandatory = $false, HelpMessage = "Name of the log file that the entry will written to.")] + [ValidateNotNullOrEmpty()] + [string]$FileName + ) + #Determine log file location + $LogFilePath = $FileName + #Construct time stamp for log entry + if(-not(Test-Path -Path 'variable:global:TimezoneBias')) + { + [string]$global:TimezoneBias = [System.TimeZoneInfo]::Local.GetUtcOffset((Get-Date)).TotalMinutes + if($TimezoneBias -match "^-") + { + $TimezoneBias = $TimezoneBias.Replace('-', '+') + } + else + { + $TimezoneBias = '-' + $TimezoneBias + } + } + $Time = -join @((Get-Date -Format "HH:mm:ss.fff"), $TimezoneBias) + #Construct date for log entry + $Date = (Get-Date -Format "MM-dd-yyyy") + #Construct context for log entry + $Context = $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name) + #Construct final log entry + $LogText = "" + #Add value to log file + try + { + Out-File -InputObject $LogText -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop + } + catch [System.Exception] + { + Write-Warning -Message "Unable to append log entry to $FileName file. Error message at line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)" + } +} \ No newline at end of file