Skip to content

Commit c9ba76f

Browse files
standarized strings
1 parent 953347d commit c9ba76f

File tree

3 files changed

+778
-742
lines changed

3 files changed

+778
-742
lines changed

AppOptimizeAndConfig.ps1

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
https://github.com/cluberti/VDI/blob/master/ConfigAsVDI.ps1
5050
5151
.LOG
52-
1.1.4 - May 28, 2019 - fixed FOD issue and messages
52+
1.1.4 - May 29, 2019 - fixed FOD issue and messages. fixed set-usersettings default users; fixed office detection
53+
resolved all VSC problems
5354
1.1.3 - May 28, 2019 - fixed Get-SMSTSENV log path
5455
1.1.2 - May 24, 2019 - Removed IE customized settings
5556
1.1.1 - May 15, 2019 - Added Get-ScriptPpath function to support VScode and ISE; fixed Set-UserSettings
@@ -223,7 +224,7 @@ Function Write-LogEntry{
223224
}
224225
}
225226
End{
226-
If($Outhost){
227+
If($Outhost -or $Global:OutTohost){
227228
If($Source){
228229
$OutputMsg = ("[{0}] [{1}] :: {2}" -f $LogTimePlusBias,$Source,$Message)
229230
}
@@ -452,7 +453,15 @@ Function Set-SystemSetting {
452453

453454
# build a unique output file
454455
$LGPOfile = ($RegKeyHive + '-' + $RegKeyPath.replace('\','-').replace(' ','') + '-' + $RegKeyName.replace(' ','') + '.lgpo')
455-
456+
457+
#Remove the Username or SID from Registry key path
458+
If($LGPOHive -eq 'User'){
459+
$UserID = $RegKeyPath.Split('\')[0]
460+
If($UserID -match "DEFAULT|S-1-5-21-(\d+-?){4}$"){
461+
$RegKeyPath = $RegKeyPath.Replace($UserID+"\","")
462+
}
463+
}
464+
456465
#complete LGPO file
457466
Write-LogEntry ("LGPO applying [{3}] to registry: [{0}\{1}\{2}] as a Group Policy item" -f $RegHive,$RegKeyPath,$RegKeyName,$RegKeyName) -Severity 4 -Source ${CmdletName}
458467
$lgpoout += "$LGPOHive`r`n"
@@ -519,7 +528,7 @@ Function Set-SystemSetting {
519528

520529

521530
Function Set-UserSetting {
522-
[CmdletBinding()]
531+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='Medium')]
523532
Param (
524533
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
525534
[Alias("Path")]
@@ -575,6 +584,13 @@ Function Set-UserSetting {
575584
$VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference')
576585
}
577586

587+
if (-not $PSBoundParameters.ContainsKey('Confirm')) {
588+
$ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
589+
}
590+
if (-not $PSBoundParameters.ContainsKey('WhatIf')) {
591+
$WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
592+
}
593+
578594
#If user profile variable doesn't exist, build one
579595
If(!$Global:UserProfiles){
580596
# Get each user profile SID and Path to the profile
@@ -606,7 +622,7 @@ Function Set-UserSetting {
606622
'AllUsers' {$RegHive = 'HKEY_USERS'; $ProfileList = $Global:UserProfiles}
607623
'CurrentUser' {$RegHive = 'HKCU' ; $ProfileList = ($Global:UserProfiles | Where-Object{$_.SID -eq $CurrentSID})}
608624
'DefaultUser' {$RegHive = 'HKU' ; $ProfileList = $DefaultProfile}
609-
default {$RegHive = $RegKeyHive ; $ProfileList = $null}
625+
default {$RegHive = $RegKeyHive ; $ProfileList = ($Global:UserProfiles | Where-Object{$_.SID -eq $CurrentSID})}
610626
}
611627

612628
#check if hive is local machine.
@@ -651,13 +667,13 @@ Function Set-UserSetting {
651667

652668
Try{
653669
$objSID = New-Object System.Security.Principal.SecurityIdentifier($UserProfile.SID)
654-
$UserID = $objSID.Translate([System.Security.Principal.NTAccount])
670+
$UserName = $objSID.Translate([System.Security.Principal.NTAccount])
655671
}
656672
Catch{
657-
$UserID = $UserProfile.SID
673+
$UserName = $UserProfile.UserName
658674
}
659675

660-
If($Message){Show-ProgressStatus -Message $Message -SubMessage ("for user profile ({0} of {1})" -f $p,$ProfileList.count) -Step $p -MaxStep $ProfileList.count}
676+
If($Message){Show-ProgressStatus -Message $Message -SubMessage ("(Users: {0} of {1})" -f $p,$ProfileList.count) -Step $p -MaxStep $ProfileList.count}
661677

662678
#loadhive if not mounted
663679
If (($HiveLoaded = Test-Path Registry::HKEY_USERS\$($UserProfile.SID)) -eq $false) {
@@ -666,12 +682,12 @@ Function Set-UserSetting {
666682
}
667683

668684
If ($HiveLoaded -eq $true) {
669-
If($Message){Write-LogEntry ("{0} for User [{1}]..." -f $Message,$UserID)}
685+
If($Message){Write-LogEntry ("{0} for User [{1}]..." -f $Message,$UserName)}
670686
If($Remove){
671-
Remove-ItemProperty "$RegHive\$($UserProfile.SID)\$RegKeyPath" -Name $RegKeyName -ErrorAction SilentlyContinue | Out-Null
687+
Remove-ItemProperty "$RegHive\$($UserProfile.SID)\$RegKeyPath" -Name $RegKeyName -Force:$Force -WhatIf:$WhatIfPreference -ErrorAction SilentlyContinue | Out-Null
672688
}
673689
Else{
674-
Set-SystemSetting -Path "$RegHive\$($UserProfile.SID)\$RegKeyPath" -Name $RegKeyName -Type $Type -Value $Value -Force:$Force -TryLGPO:$TryLGPO
690+
Set-SystemSetting -Path "$RegHive\$($UserProfile.SID)\$RegKeyPath" -Name $RegKeyName -Type $Type -Value $Value -Force:$Force -WhatIf:$WhatIfPreference -TryLGPO:$TryLGPO
675691
}
676692
}
677693

@@ -685,12 +701,12 @@ Function Set-UserSetting {
685701
}
686702
}
687703
Else{
688-
If($Message){Write-LogEntry ("{0} for [{1}]..." -f $Message,$ApplyTo)}
704+
If($Message){Write-LogEntry ("{0} for [{1}]..." -f $Message,$ProfileList.UserName)}
689705
If($Remove){
690-
Remove-ItemProperty "$RegHive\$RegKeyPath\$RegKeyPath" -Name $RegKeyName -ErrorAction SilentlyContinue | Out-Null
706+
Remove-ItemProperty "$RegHive\$RegKeyPath\$RegKeyPath" -Name $RegKeyName -Force:$Force -WhatIf:$WhatIfPreference -ErrorAction SilentlyContinue | Out-Null
691707
}
692708
Else{
693-
Set-SystemSetting -Path "$RegHive\$RegKeyPath" -Name $RegKeyName -Type $Type -Value $Value -Force:$Force -TryLGPO:$TryLGPO
709+
Set-SystemSetting -Path "$RegHive\$RegKeyPath" -Name $RegKeyName -Type $Type -Value $Value -Force:$Force -WhatIf:$WhatIfPreference -TryLGPO:$TryLGPO
694710
}
695711
}
696712

@@ -747,15 +763,18 @@ Function Get-InstalledApplication {
747763
)
748764

749765
Begin {
750-
## Get the name of this function and write header
751-
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
766+
## Get the name of this function
767+
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
768+
769+
# Registry keys for native and WOW64 applications
770+
[string[]]$regKeyApplications = 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
752771
}
753772
Process {
754773
If ($name) {
755-
Write-LogEntry -Message "Get information for installed Application Name(s) [$($name -join ', ')]..." -Source ${CmdletName} -Outhost:$Outhost
774+
Write-LogEntry -Message "Get information for installed Application Name(s) [$($name -join ', ')]..." -Severity 4 -Source ${CmdletName} -Outhost:$Global:Verbose
756775
}
757776
If ($productCode) {
758-
Write-LogEntry -Message "Get information for installed Product Code [$ProductCode]..." -Source ${CmdletName} -Outhost:$Outhost
777+
Write-LogEntry -Message "Get information for installed Product Code [$ProductCode]..." -Severity 4 -Source ${CmdletName} -Outhost:$Global:Verbose
759778
}
760779

761780
## Enumerate the installed applications from the registry for applications that have the "DisplayName" property
@@ -769,14 +788,14 @@ Function Get-InstalledApplication {
769788
If ($regKeyApplicationProps.DisplayName) { [psobject[]]$regKeyApplication += $regKeyApplicationProps }
770789
}
771790
Catch{
772-
Write-LogEntry -Message "Unable to enumerate properties from registry key path [$($UninstallKeyApp.PSPath)]. `n$(Resolve-Error)" -Severity 2 -Source ${CmdletName} -Outhost:$Outhost
791+
Write-LogEntry -Message "Unable to enumerate properties from registry key path [$($UninstallKeyApp.PSPath)]. `n$(Resolve-Error)" -Severity 2 -Source ${CmdletName} -Outhost:$Global:OutTohost
773792
Continue
774793
}
775794
}
776795
}
777796
}
778797
If ($ErrorUninstallKeyPath) {
779-
Write-LogEntry -Message "The following error(s) took place while enumerating installed applications from the registry. `n$(Resolve-Error -ErrorRecord $ErrorUninstallKeyPath)" -Severity 2 -Source ${CmdletName} -Outhost:$Outhost
798+
Write-LogEntry -Message "The following error(s) took place while enumerating installed applications from the registry. `n$(Resolve-Error -ErrorRecord $ErrorUninstallKeyPath)" -Severity 2 -Source ${CmdletName} -Outhost:$Global:OutTohost
780799
}
781800

782801
## Create a custom object with the desired properties for the installed applications and sanitize property details
@@ -806,7 +825,7 @@ Function Get-InstalledApplication {
806825
If ($ProductCode) {
807826
## Verify if there is a match with the product code passed to the script
808827
If ($regKeyApp.PSChildName -match [regex]::Escape($productCode)) {
809-
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] matching product code [$productCode]." -Source ${CmdletName} -Outhost:$Outhost
828+
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] matching product code [$productCode]." -Source ${CmdletName} -Outhost
810829
$installedApplication += New-Object -TypeName 'PSObject' -Property @{
811830
UninstallSubkey = $regKeyApp.PSChildName
812831
ProductCode = If ($regKeyApp.PSChildName -match $MSIProductCodeRegExPattern) { $regKeyApp.PSChildName } Else { [string]::Empty }
@@ -830,27 +849,27 @@ Function Get-InstalledApplication {
830849
# Check for an exact application name match
831850
If ($regKeyApp.DisplayName -eq $application) {
832851
$applicationMatched = $true
833-
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] using exact name matching for search term [$application]." -Source ${CmdletName}
852+
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] using exact name matching for search term [$application]." -Source ${CmdletName} -Outhost
834853
}
835854
}
836855
ElseIf ($WildCard) {
837856
# Check for wildcard application name match
838857
If ($regKeyApp.DisplayName -like $application) {
839858
$applicationMatched = $true
840-
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] using wildcard matching for search term [$application]." -Source ${CmdletName}
859+
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] using wildcard matching for search term [$application]." -Source ${CmdletName} -Outhost
841860
}
842861
}
843862
ElseIf ($RegEx) {
844863
# Check for a regex application name match
845864
If ($regKeyApp.DisplayName -match $application) {
846865
$applicationMatched = $true
847-
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] using regex matching for search term [$application]." -Source ${CmdletName}
866+
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] using regex matching for search term [$application]." -Source ${CmdletName} -Outhost
848867
}
849868
}
850869
# Check for a contains application name match
851870
ElseIf ($regKeyApp.DisplayName -match [regex]::Escape($application)) {
852871
$applicationMatched = $true
853-
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] using contains matching for search term [$application]." -Source ${CmdletName}
872+
Write-LogEntry -Message "Found installed application [$appDisplayName] version [$appDisplayVersion] using contains matching for search term [$application]." -Source ${CmdletName} -Outhost
854873
}
855874

856875
If ($applicationMatched) {
@@ -871,7 +890,7 @@ Function Get-InstalledApplication {
871890
}
872891
}
873892
Catch {
874-
Write-LogEntry -Message "Failed to resolve application details from registry for [$appDisplayName]. `n$(Resolve-Error)" -Severity 3 -Source ${CmdletName} -Outhost:$Outhost
893+
Write-LogEntry -Message "Failed to resolve application details from registry for [$appDisplayName]. `n$(Resolve-Error)" -Severity 3 -Source ${CmdletName} -Outhost
875894
Continue
876895
}
877896
}
@@ -893,8 +912,6 @@ $scriptPath = Get-ScriptPath
893912
[string]$scriptName = Split-Path $scriptPath -Leaf
894913
[string]$scriptBaseName = [System.IO.Path]::GetFileNameWithoutExtension($scriptName)
895914

896-
[int]$OSBuildNumber = (Get-WmiObject -Class Win32_OperatingSystem).BuildNumber
897-
898915
#Create Paths
899916
$ToolsPath = Join-Path $scriptDirectory -ChildPath 'Tools'
900917

@@ -964,62 +981,57 @@ Else{
964981
$UseLGPO = $false
965982
}
966983

967-
# Get Onenote paths
968-
$OneNotePathx86 = Get-ChildItem "${env:ProgramFiles(x86)}" -Recurse -Filter "ONENOTE.EXE"
969-
$OneNotePathx64 = Get-ChildItem "$env:ProgramFiles" -Recurse -Filter "ONENOTE.EXE"
970-
If($OneNotePathx86){$OneNotePath = $OneNotePathx86}
971-
If($OneNotePathx64){$OneNotePath = $OneNotePathx64}
972-
973-
$OfficeInstalled = Get-InstalledApplication "Microsoft Office" | Select -First 1
984+
$OfficeInstalled = Get-InstalledApplication "Microsoft Office Professional Plus" | Select-Object -First 1
974985
If($OfficeInstalled){
975986
If( $OfficeInstalled.Is64BitApplication ) {$OfficeLocation = $env:ProgramFiles} Else {$OfficeLocation = ${env:ProgramFiles(x86)}}
976987
$OfficeVersion = [string]([version]$OfficeInstalled.DisplayVersion).Major + '.' + [string]([version]$OfficeInstalled.DisplayVersion).Minor
977988
$OfficeFolder = 'Office' + [string]([version]$OfficeInstalled.DisplayVersion).Major
989+
$OfficeTitle = [string]$OfficeInstalled.DisplayName
978990
}
979991

980992
#if running in a tasksequence; apply user settings to all user profiles (use ApplyTo param cmdlet Set-UserSettings )
981993
If(Get-SMSTSENV -NoWarning){$Global:ApplyToProfiles = 'AllUsers'}Else{$Global:ApplyToProfiles = 'CurrentUser'}
982994
If((Get-SMSTSENV -NoWarning) -and -not($psISE)){$Global:OutToHost = $false}Else{$Global:OutToHost = $true}
983995

984996
#grab all Show-ProgressStatus commands in script and count them
985-
$script:Maxsteps = ([System.Management.Automation.PsParser]::Tokenize((gc $scriptPath), [ref]$null) | where { $_.Type -eq 'Command' -and $_.Content -eq 'Show-ProgressStatus' }).Count
997+
$script:Maxsteps = ([System.Management.Automation.PsParser]::Tokenize((Get-Content $scriptPath), [ref]$null) | Where-Object { $_.Type -eq 'Command' -and $_.Content -eq 'Show-ProgressStatus' }).Count
986998
#set counter to one
987999
$stepCounter = 1
9881000
##*===========================================================================
9891001
##* MAIN
9901002
##*===========================================================================
9911003

9921004
If($EnableIESoftwareRender){
993-
Set-UserSetting -Message "Enabling Software Rendering For IE" -Path "SOFTWARE\Microsoft\Internet Explorer\Main" -Name 'UseSWRender' -Type DWord -Value 1 -Force
1005+
Set-UserSetting -Message "Enabling Software Rendering For IE" -Path 'SOFTWARE\Microsoft\Internet Explorer\Main' -Name 'UseSWRender' -Type DWord -Value 1 -Force
9941006
}
9951007
Else{$stepCounter++}
9961008

9971009

9981010
If ($DisableOfficeAnimation -and $OfficeInstalled){
999-
Set-UserSetting -Message "Disabling OST Cache mode for Office 2016" -Path "SOFTWARE\Policies\Microsoft\Office\$OfficeVersion\Outlook\ost" -Name 'NoOST' -Type DWord -Value 2 -Force
1000-
Set-UserSetting -Message "Disabling Exchange cache mode for Office 2016" -Path "SOFTWARE\Policies\Microsoft\Office\$OfficeVersion\Outlook\cache mode" -Name 'Enable' -Type DWord -Value 0 -Force
1011+
Set-UserSetting -Message "Disabling OST Cache mode for $OfficeTitle" -Path 'SOFTWARE\Policies\Microsoft\Office\$OfficeVersion\Outlook\ost' -Name 'NoOST' -Type DWord -Value 2 -Force
1012+
Set-UserSetting -Message "Disabling Exchange cache mode for $OfficeTitle" -Path 'SOFTWARE\Policies\Microsoft\Office\$OfficeVersion\Outlook\cache mode' -Name 'Enable' -Type DWord -Value 0 -Force
10011013
}
10021014
Else{$stepCounter++}
10031015

10041016

10051017
If ($DisableOfficeAnimation -and $OfficeInstalled){
1006-
Set-UserSetting -Message "Disabling Hardware Acceleration for Office 2016" -Path "SOFTWARE\Microsoft\Office\$OfficeVersion\Common\Graphics" -Name 'DisableHardwareAcceleration' -Type DWord -Value 1 -Force
1007-
Set-UserSetting -Message "Disabling Animation for Office 2016" -Path "SOFTWARE\Microsoft\Office\$OfficeVersion\Common\Graphics" -Name 'DisableAnimation' -Type DWord -Value 1 -Force
1008-
Set-UserSetting -Message "Disabling First Run Boot for Office 2016" -Path "SOFTWARE\Microsoft\Office\$OfficeVersion\FirstRun" -Name 'BootRTM' -Type DWord -Value 1 -Force
1009-
Set-UserSetting -Message "Disabling First Run Movie for Office 2016" -Path "SOFTWARE\Microsoft\Office\$OfficeVersion\FirstRun" -Name 'DisableMovie' -Type DWord -Value 1 -Force
1010-
Set-UserSetting -Message "Disabling First Run Optin for Office 2016" -Path "SOFTWARE\Microsoft\Office\$OfficeVersion\Common\General" -Name 'showfirstrunoptin' -Type DWord -Value 1 -Force
1011-
Set-UserSetting -Message "Disabling First Run Optin for Office 2016" -Path "SOFTWARE\Microsoft\Office\$OfficeVersion\Common\PTWatson" -Name 'PTWOption' -Type DWord -Value 1 -Force
1012-
Set-UserSetting -Message "Disabling CEIP for Office 2016" -Path "SOFTWARE\Microsoft\Office\$OfficeVersion\Common" -Name 'qmenable' -Type DWord -Value 1 -Force
1013-
Set-UserSetting -Message "Accepting Eulas for Office 2016" -Path "SOFTWARE\Microsoft\Office\$OfficeVersion\Registration" -Name 'AcceptAllEulas' -Type DWord -Value 1 -Force
1014-
Set-UserSetting -Message "Disabling Default File Types for Office 2016" -Path "SOFTWARE\Microsoft\Office\$OfficeVersion\Common\General" -Name 'ShownFileFmtPrompt' -Type DWord -Value 1 -Force
1018+
Set-UserSetting -Message "Disabling Hardware Acceleration for $OfficeTitle" -Path 'SOFTWARE\Microsoft\Office\$OfficeVersion\Common\Graphics' -Name 'DisableHardwareAcceleration' -Type DWord -Value 1 -Force
1019+
Set-UserSetting -Message "Disabling Animation for $OfficeTitle" -Path 'SOFTWARE\Microsoft\Office\$OfficeVersion\Common\Graphics' -Name 'DisableAnimation' -Type DWord -Value 1 -Force
1020+
Set-UserSetting -Message "Disabling First Run Boot for $OfficeTitle" -Path 'SOFTWARE\Microsoft\Office\$OfficeVersion\FirstRun' -Name 'BootRTM' -Type DWord -Value 1 -Force
1021+
Set-UserSetting -Message "Disabling First Run Movie for $OfficeTitle" -Path 'SOFTWARE\Microsoft\Office\$OfficeVersion\FirstRun' -Name 'DisableMovie' -Type DWord -Value 1 -Force
1022+
Set-UserSetting -Message "Disabling First Run Optin for $OfficeTitle" -Path 'SOFTWARE\Microsoft\Office\$OfficeVersion\Common\General' -Name 'showfirstrunoptin' -Type DWord -Value 1 -Force
1023+
Set-UserSetting -Message "Disabling First Run Optin for $OfficeTitle" -Path 'SOFTWARE\Microsoft\Office\$OfficeVersion\Common\PTWatson' -Name 'PTWOption' -Type DWord -Value 1 -Force
1024+
Set-UserSetting -Message "Disabling CEIP for $OfficeTitle" -Path 'SOFTWARE\Microsoft\Office\$OfficeVersion\Common' -Name 'qmenable' -Type DWord -Value 1 -Force
1025+
Set-UserSetting -Message "Accepting Eulas for $OfficeTitle" -Path 'SOFTWARE\Microsoft\Office\$OfficeVersion\Registration' -Name 'AcceptAllEulas' -Type DWord -Value 1 -Force
1026+
Set-UserSetting -Message "Disabling Default File Types for $OfficeTitle" -Path 'SOFTWARE\Microsoft\Office\$OfficeVersion\Common\General' -Name 'ShownFileFmtPrompt' -Type DWord -Value 1 -Force
10151027
}
10161028
Else{$stepCounter++}
10171029

10181030

10191031

10201032
If($EnableLyncStartup -and $OfficeInstalled)
10211033
{
1022-
Set-UserSetting -Message "Enabling Skype for Business Startup" -Path "SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "Lync" -Type String -Value """$OfficeLocation\Microsoft Office\Office16\lync.exe"" /fromrunkey" -Force
1034+
Set-UserSetting -Message "Enabling Skype for Business Startup" -Path 'SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name "Lync" -Type String -Value """$OfficeLocation\Microsoft Office\$OfficeFolder\lync.exe"" /fromrunkey" -Force
10231035
}
10241036
Else{$stepCounter++}
10251037

0 commit comments

Comments
 (0)