wingetautoupdate/Winget-AutoUpdate/functions/Add-ScopeMachine.ps1

32 lines
1.3 KiB
PowerShell
Raw Permalink Normal View History

#Function to configure the prefered scope option as Machine
2023-11-20 16:31:38 +00:00
function Add-ScopeMachine {
2023-11-20 16:31:38 +00:00
#Get Settings path for system or current user
if ([System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem) {
$SettingsPath = "$Env:windir\System32\config\systemprofile\AppData\Local\Microsoft\WinGet\Settings\defaultState\settings.json"
2023-11-20 16:31:38 +00:00
}
else {
$SettingsPath = "$env:LOCALAPPDATA\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\settings.json"
}
$ConfigFile = @{}
#Check if setting file exist, if not create it
if (Test-Path $SettingsPath) {
$ConfigFile = Get-Content -Path $SettingsPath | Where-Object { $_ -notmatch '//' } | ConvertFrom-Json
}
2023-11-20 16:31:38 +00:00
else {
New-Item -Path $SettingsPath -Force | Out-Null
}
2023-11-20 16:31:38 +00:00
if ($ConfigFile.installBehavior.preferences) {
2023-11-21 11:52:47 +00:00
Add-Member -InputObject $ConfigFile.installBehavior.preferences -MemberType NoteProperty -Name "scope" -Value "Machine" -Force
}
else {
2023-11-21 11:52:47 +00:00
$Scope = New-Object PSObject -Property $(@{scope = "Machine" })
$Preference = New-Object PSObject -Property $(@{preferences = $Scope })
2023-11-21 11:52:47 +00:00
Add-Member -InputObject $ConfigFile -MemberType NoteProperty -Name "installBehavior" -Value $Preference -Force
}
2023-11-20 16:31:38 +00:00
$ConfigFile | ConvertTo-Json -Depth 100 | Out-File $SettingsPath -Encoding utf8 -Force
}