forked from byteben/OneDrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set_KFM_Attribute.ps1
105 lines (79 loc) · 3.68 KB
/
Set_KFM_Attribute.ps1
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<#
===========================================================================
Created on: 26/01/2020 10:45
Created by: Ben Whitmore
Organization:
Filename: Set_KFM_Attribute.ps1
-------------------------------------------------------------------------
Script Name: Set_KFM_Attribute
===========================================================================
Version:
1.1.26.2 26/01/2020 Ben Whitmore
Added logging to HKCU for MEMCM Detection Method
HKEY_CURRENT_USER\ScriptStatus\Set_KFM_Attributes
> Attribute_Set
> Folders_Specified
> LastRun
1.1.26.1 26/01/2020 Ben Whitmore
Added "Favorites" as Known Folder Location
1.1.26.0 26/01/2020 Ben Whitmore
.DESCRIPTION
Script to set KnownFolder Pinned status
.PARAMETER KnownFolder
Specify which KnownFolders to process. Choice of "Desktop", "Documents", "Pictures", "Favorites".
.PARAMETER PinStatus
Specify the Pinned atribute to pass. Choice of "Pin", "Unpin".
.EXAMPLE
Set_KFM_Attribute.ps1 -KnownFolder "Desktop", "Documents" -PinStatus "Pin"
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateSet("Desktop", "Documents", "Pictures", "Favorites")]
[String[]]$KnownFolder,
[Parameter(Mandatory)]
[ValidateSet("Pin", "UnPin")]
[String]$PinStatus
)
#Set attributes to pass to attrib.exe
If ($PinStatus -eq 'Pin') { $AttributeState1 = '+P'; $AttributeState2 = '-U' }
If ($PinStatus -eq 'UnPin') { $AttributeState1 = '-P'; $AttributeState2 = '+U' }
#Set variable for OneDrive Commercial
$OneDriveLoc = $ENV:OneDriveCommercial
#Update attributes for all files and folders specified, including toplevel KnownFolder
Foreach ($Folder in $KnownFolder) {
#Set KnownFolder
$OneDriveKnownFolder = Join-Path $OneDriveLoc $Folder
#Set the KnownFolder attribute so new items match the pinned status passed in the script
attrib.exe $OneDriveKnownFolder $AttributeState1 $AttributeState2 /s /d
#Process child items in the KnownFolder
Get-ChildItem $OneDriveKnownFolder -Recurse | Select-Object Fullname | ForEach-Object { attrib.exe $_.FullName $AttributeState1 $AttributeState2 }
}
#Prepare to write registry key for MEMCM Detection Method
$RegistryPath = "HKCU:\ScriptStatus\Set_KFM_Attributes"
#Check If Script Registry Key Exists
If (!(Test-Path $RegistryPath)) {
#If Registry Key doesn't exist, create it
New-Item -Path $RegistryPath -Force | Out-Null
#Dealing with "Value Exists" logic in MEMCM Detection Method so will force the value here to say script has run
New-ItemProperty -Path $RegistryPath -Name "Last_Run" -Value (Get-Date) -PropertyType String -Force | Out-Null
#Create Registry Item for Known Folder the script has run against
New-ItemProperty -Path $RegistryPath -Name "Attribute_Set" -Value $PinStatus -PropertyType String -Force | Out-Null
}
else {
#Dealing with "Value Exists" logic in MEMCM Detection Method so will force the value here to say script has run
New-ItemProperty -Path $RegistryPath -Name "Last_Run" -Value (Get-Date) -PropertyType String -Force | Out-Null
#Create Registry Item for Known Folder the script has run against
New-ItemProperty -Path $RegistryPath -Name "Attribute_Set" -Value $PinStatus -PropertyType String -Force | Out-Null
}
#Reset Variables
$KnownFoldersProcessed = $Null
$Count = 0
Foreach ($Folder in $KnownFolder) {
#Build string for Registry
If ($Count -gt 0) { $LineBreak = ' | ' } else { $LineBreak = $Null }
$KnownFoldersProcessed = $KnownFoldersProcessed + $LineBreak + $Folder
$Count ++
}
#Create Registry Item for Known Folder the script has run against
New-ItemProperty -Path $RegistryPath -Name "Folders_Specified" -Value $KnownFoldersProcessed -PropertyType String -Force | Out-Null