diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae00c65 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +old/ +test/ \ No newline at end of file diff --git a/Install-updates.ps1 b/Install-updates.ps1 new file mode 100644 index 0000000..3f9f26e --- /dev/null +++ b/Install-updates.ps1 @@ -0,0 +1,2 @@ +Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot +shutdown.exe -r -t 0 \ No newline at end of file diff --git a/No-updates.ps1 b/No-updates.ps1 new file mode 100644 index 0000000..3a15e98 --- /dev/null +++ b/No-updates.ps1 @@ -0,0 +1,37 @@ +#Load variables +. "C:\scripts\automatic-updates-static\Variables.ps1" + +#set-location C:\scripts\automatic-updates\ +$KeyPath = "c:\scripts\automatic-updates-static\" + +#function to get credentials from a Saved file +Function Get-SavedCredential([string]$UserName,[string]$KeyPath) +{ + If(Test-Path "$($KeyPath)\$($Username).cred") { + $SecureString = Get-Content "$($KeyPath)\$($Username).cred" | ConvertTo-SecureString + $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecureString + } + Else { + Throw "Unable to locate a credential for $($Username)" + } + Return $Credential +} + + +#Get encrypted password from the file +$cred = Get-SavedCredential -UserName $mailusername -KeyPath "c:\scripts\automatic-updates-static" +#Get FQDN for subject +$myFQDN =(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain ; Write-Host $myFQDN +#$cred = Get-Credential backup@fourit.nl +$mailprops = @{ + To = $mailto + From = $mailfrom + Subject = $myFQDN + Port = 587 + SmtpServer = $smtpserver + Credential = $cred + UseSsl = $true + BodyAsHtml = $true +} +$htmlBody = 'There are no updates available' +Send-MailMessage @mailprops -body $htmlBody \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..08d59c6 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Automatic Updates + +Mail available updates and install updates + +Create folder c:\scripts\automatic-updates. Place there the files.
+Run the script setup.ps1. If needed run Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass to allow unsigned script + +If required for script variables are stored in c:\Scripts\atuomatic-updates-static\variables.ps1. To use these in script enter next lines:
+#Load variables
+. "C:\scripts\automatic-updates-static\Variables.ps1"
+ +https://gitlab.fourit.cloud/Ivo/automatic-updates-v2/-/archive/main/automatic-updates-v2-main.zip + +ToDo: +Mails automatiseren met Power BI +Research possibility to update dc first then others. Maybe run from domain controller. Benefit is scripts on one location. +https://social.technet.microsoft.com/Forums/en-US/41ad0e87-71f1-4728-a78b-cec7923cefac/run-windows-scheduled-task-on-a-remote-server?forum=winserverpowershell +Automatic-Updates-Remote +Reboot resume script (werkt niet meer) + +ToDo afgerond: +Geen resume script bij geen reboot benodigd +Cleanup mails send (create csv file with updates) \ No newline at end of file diff --git a/setup.ps1 b/setup.ps1 new file mode 100644 index 0000000..bfb8b7f --- /dev/null +++ b/setup.ps1 @@ -0,0 +1,43 @@ +#Only run this one time to setup server for automatic updates +New-Item -ItemType File -Path C:\Scripts\automatic-updates-static\variables.ps1 -Force +Copy-Item C:\Scripts\automatic-updates\variables.ps1 C:\Scripts\automatic-updates-static\variables.ps1 +New-Item -ItemType File -Path C:\Scripts\automatic-updates-static\updatescript.ps1 -Force +Copy-Item C:\Scripts\automatic-updates\updatescript.ps1 C:\Scripts\automatic-updates-static\updatescript.ps1 +New-Item -ItemType File -Path C:\Scripts\automatic-updates-static\Start-Remote-task-updatescript.ps1.ps1 -Force +Copy-Item C:\Scripts\automatic-updates\Start-Remote-task-updatescript.ps1 C:\Scripts\automatic-updates-static\Start-Remote-task-updatescript.ps1 +#Install required modules +C:\scripts\automatic-updates\Run_scripts_onetime\install_module.ps1 +#Create encrypted password file to be able to send mail +Write-Host "Enter password for mail" -ForegroundColor Yellow +Write-Host "Press a key to continue" -ForegroundColor Yellow +[void][System.Console]::ReadKey($true) +C:\scripts\automatic-updates\Run_scripts_onetime\email-password.ps1 +#Create encrypted password file to create and run tasks +Write-Host "Enter username and password of service account" -ForegroundColor Yellow +Write-Host "Press a key to continue" -ForegroundColor Yellow +[void][System.Console]::ReadKey($true) + +C:\scripts\automatic-updates\Run_scripts_onetime\localpassword.ps1 +#> +$shell = new-object -comobject "WScript.Shell" +$result = $shell.popup("Are you running this script from primary domain controller?",0,"Question",4+32) +if ($result -eq "6") { + Write-Host @" + You are running this from the domain controller. You have to create a task manualy on the server. + + Create a task manual with the name "Automatic-Updates" + Use as trigger montly on first Thursday. + As Actions Start a program. powershell.exe with arguments "-NoProfile -File C:\Scripts\automatic-updates\Automatic-updates.ps1". + Use the service account and choose to run whether user is logged on or not. + + Press a key to continue + +"@ -ForegroundColor Yellow +[void][System.Console]::ReadKey($true) +} else { + Write-Host "We will create a task that will not start automaticaly. This will be run from the domain controller." -ForegroundColor Yellow + enable-WUremoting + C:\scripts\automatic-updates\Run_scripts_onetime\Create-task-remote.ps1 + C:\scripts\automatic-updates\Run_scripts_onetime\Create-task-updatescript.ps1 +} +Write-Host "The variables.ps1 is copied to c:\scripts\automatic-updates-static. Please edit this file" -ForegroundColor Yellow \ No newline at end of file