-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMicrosoftStoreAppUpdater.ps1
More file actions
86 lines (69 loc) · 3.1 KB
/
MicrosoftStoreAppUpdater.ps1
File metadata and controls
86 lines (69 loc) · 3.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
###################################################################################################################
# Name: MicrosoftStoreAppUpdater.ps1
# Author: Thomas Marcussen, Thomas@ThomasMarcussen.com
# Date: February,2022
###################################################################################################################
<#
### Notes:
This script will inititae update for Microsoft Store Apps and applications available using winget.
By default it will delete the logfile if older then 30 days.
### Checklist:
- Should run with local administrative rights or as system
### Solves the following issues:
- When deploying new computers, there might be modern apps without processed updates.
- Can also update on regular machines in use.
###
- Script is still in development
#>
function Write-log {
[CmdletBinding()]
Param(
[parameter(Mandatory=$true)]
[String]$Path,
[parameter(Mandatory=$true)]
[String]$Message,
[parameter(Mandatory=$true)]
[String]$Component,
[Parameter(Mandatory=$true)]
[ValidateSet("Info", "Warning", "Error")]
[String]$Type
)
switch ($Type) {
"Info" { [int]$Type = 1 }
"Warning" { [int]$Type = 2 }
"Error" { [int]$Type = 3 }
}
# Create a log entry
$Content = "<![LOG[$Message]LOG]!>" +`
"<time=`"$(Get-Date -Format "HH:mm:ss.ffffff")`" " +`
"date=`"$(Get-Date -Format "M-d-yyyy")`" " +`
"component=`"$Component`" " +`
"context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " +`
"type=`"$Type`" " +`
"thread=`"$([Threading.Thread]::CurrentThread.ManagedThreadId)`" " +`
"file=`"`">"
# Write the line to the log file
Add-Content -Path $Path -Value $Content
}
$namespaceName = "root\cimv2\mdm\dmmap"
$className = "MDM_EnterpriseModernAppManagement_AppManagement01"
$wmiObj = Get-WmiObject -Namespace $namespaceName -Class $className
$result = $wmiObj.UpdateScanMethod()
$LogCycle = 30
$LogFilePath = Join-Path $PSScriptRoot "$(Get-Date -Format yyyy-M-dd) $($MyInvocation.MyCommand.Name).log"
Write-Warning "Delete log files older than $LogCycle days"
Get-ChildItem -Path $PSScriptRoot | Where-Object {($Now - $_.LastWriteTime).Days -gt $LogCycle -and $_.extension -eq ".log"} | Remove-Item
Write-Host "Update process has been started"
Write-Host "Following apps have updates available"
winget upgrade #Lists all apps which have updates available
$Output= winget upgrade -h --all #Apps Which are being updated and will be listed in log after the update install finishes
Write-Host "Apps which have updates available"
Write-Host $Output
try {
throw "Updates were Succesfully installed"
}
catch {
Write-Information ($_ | Out-String) #It will write log in information when script runs as update will be started
Write-Log -Path $LogFilePath -Message ($_ | Out-String) -Component $MyInvocation.MyCommand.Name -Type Info
}
Write-Host "Updates were installed and log file has been created"