-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpspatch.ps1
More file actions
66 lines (61 loc) · 2.1 KB
/
Copy pathpspatch.ps1
File metadata and controls
66 lines (61 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Set execution policy globally to bypass
Set-ExecutionPolicy Bypass -Force
# Define paths
$sourceModulePath = "\\vm-acme-01\netlogon\PSWindowsUpdate"
$localModulePath = "C:\Program Files\WindowsPowerShell\Modules\PSWindowsUpdate"
$computername = $env:computername + ".log"
# Create PSDrive for logging
try {
New-PSDrive -Name dest -Root \\vm-bob-01\logs -PSProvider FileSystem -ErrorAction Stop
}
catch {
Write-Error "Failed to create PSDrive: $_"
exit 1
}
# Check if PSWindowsUpdate module exists locally, if not copy it
try {
if (-not (Test-Path $localModulePath)) {
Write-Host "Copying PSWindowsUpdate module to local computer..."
# Create the modules directory if it doesn't exist
$parentDir = Split-Path $localModulePath -Parent
if (-not (Test-Path $parentDir)) {
New-Item -ItemType Directory -Path $parentDir -Force | Out-Null
}
# Copy the module
Copy-Item -Path $sourceModulePath -Destination $localModulePath -Recurse -Force -ErrorAction Stop
Write-Host "Module copied successfully"
}
else {
Write-Host "PSWindowsUpdate module already exists locally"
}
}
catch {
Write-Error "Failed to copy PSWindowsUpdate module: $_"
exit 1
}
# Import the module from local path
try {
Write-Host "Importing PSWindowsUpdate module..."
Import-Module -Name PSWindowsUpdate -Force -ErrorAction Stop
}
catch {
Write-Error "Failed to import PSWindowsUpdate module: $_"
exit 1
}
# Run Windows Updates
try {
Write-Host "Starting Windows Update process..."
Install-WindowsUpdate -MicrosoftUpdate -Category 'Security Updates', 'Critical Updates' -NotKBArticleID KB890830 -AcceptAll -AutoReboot -Verbose |
Out-File "dest:\$computername" -Force -Append -ErrorAction Stop
Write-Host "Windows Update process completed"
}
catch {
Write-Error "Windows Update process failed: $_"
exit 1
}
finally {
# Clean up PSDrive
if (Get-PSDrive -Name dest -ErrorAction SilentlyContinue) {
Remove-PSDrive -Name dest -Force
}
}