-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGet-MyNewHelp.ps1
105 lines (98 loc) · 5.6 KB
/
Get-MyNewHelp.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
<#
Author: Bryan Dady
Version: 0.0.3
Purpose: Confirm permissions, and update PowerShell help files from the internet
Version History:
0.0.2 - Exclude PSLogger module dependency. Exclude dysfunctional $Global:PSState logic, and update some comments
0.0.3 - Migrate related script block from Profile; name new function Get-MyNewHelp. Rename prior Get-MyNewHelp as Get-MyModuleHelp
#>
[cmdletbinding(SupportsShouldProcess)]
param()
Write-Verbose -Message 'Declaring function Get-MyModuleHelp'
Function Get-MyModuleHelp {
[cmdletbinding(SupportsShouldProcess)]
param()
# Check for prerequisite local admin role/rights, then try to update PowerShell help files for installed modules, from the internet
Write-Verbose -Message 'Checking for admin permissions to update Help files'
if (([security.principal.windowsprincipal] [security.principal.windowsidentity]::GetCurrent()).isinrole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
# if (-not ($Global:PSState)) {
# Write-Warning -Message "Fatal Error loading PowerShell saved state info from custom object: $PSState"
# }
# Check $PSHelpUpdatedDate, from previously saved state, should be loaded from json file into variable by Sperry
# $PSHelpUpdatedDate = Get-Date -Date ($Global:PSState.HelpUpdatedDate -as [DateTime])
# Write-Verbose -Message "PS Help (Last) Updated Date: $PSHelpUpdatedDate"
# $NextUpdateDate = $PSHelpUpdatedDate.AddDays(10)
# Write-Debug -Message "PS Help Next Update Date: $NextUpdateDate"
# # Is today on or after $NextUpdateDate ?
# if ($NextUpdateDate -ge (Get-Date)) {
# # We DON'T need to Update Help right now
# $updateNow = $false
# Write-Debug -Message "We DON'T need to Update Help right now"
# } else {
Write-Debug -Message 'Iterate through current modules that have a HelpInfoUri defined, and attempt to update their help'
# Iterate through current modules that have a HelpInfoUri defined, and attempt to update their help
Get-Module -ListAvailable |
Where-Object -Property HelpInfoUri |
Sort-Object -Property Name -Unique |
ForEach-Object -Process {
"Update-Help -Module $($PSItem.Name)"
Update-Help -Module $($PSItem.Name)
}
# Write-Debug -Message "Update `$PSHelpUpdatedDate to today"
# $PSHelpUpdatedDate = (Get-Date -DisplayHint Date -Format d)
# # Update custom object property, and write to settings state file
# Write-Debug -Message "`$PSHelpUpdatedDate is $PSHelpUpdatedDate"
# $Global:PSState.HelpUpdatedDate = $PSHelpUpdatedDate
# Write-Debug -Message "`$Global:PSState is $Global:PSState"
# Set-Content -Path $PSProgramsDir\Microsoft.PowerShell_state.json -Value ($Global:PSState | ConvertTo-Json) -Confirm
# }
} else {
Write-Log -Message "Skipping update-help, because we're either not on Windows, or do not have admin permissions" -Verbose # `nConsider using get-help [term] -Online"
}
}
Write-Verbose -Message 'Declaring function Get-MyNewHelp'
Function Get-MyNewHelp {
[cmdletbinding(SupportsShouldProcess)]
param()
# Preset variable
$UpdateHelp = $false
# Define constant: UNC path of previously saved PowerShell Help files
$HelpSource = '\\hcdata\apps\IT\PowerShell-Help'
# Check if Write-Log function is available
if (Get-Command -Name Write-Log -CommandType Function -ErrorAction Ignore) {
$UpdateHelp = $true
Write-Debug -Message "`$UpdateHelp: $UpdateHelp"
} else {
# This PowerShell session does not know about the Write-Log function, so we try to get a copy from the repository
Get-Module -ListAvailable -Name PSLogger | Format-List -Property Name,Path,Version
Write-Warning -Message 'Failed to locate Write-Log function locally. Attempting to load PSLogger module remotely'
try {
Import-Module -Name \\hcdata\apps\IT\PowerShell-Modules\PSLogger -ErrorAction Stop
# double-check if Write-Log function is available
if (Get-Command -Name Write-Log -CommandType Function -ErrorAction Stop) {
$UpdateHelp = $true
Write-Debug -Message "`$UpdateHelp: $UpdateHelp"
}
}
catch {
'No R: drive mapped. Get a copy of the PSLogger module installed, e.g. from R:\IT\PowerShell-Modules\PSLogger,'
'then re-try Update-Help -SourcePath $HelpSource -Recurse -Module Microsoft.PowerShell.'
}
}
Write-Verbose -Message "`$UpdateHelp: $UpdateHelp"
# Check admin rights / role; same approach as Test-LocalAdmin function in Sperry module
$IsAdmin = (([security.principal.windowsprincipal] [security.principal.windowsidentity]::GetCurrent()).isinrole([Security.Principal.WindowsBuiltInRole] 'Administrator'))
# Try to update PS help files, if we have local admin rights
if ($UpdateHelp -and $IsAdmin) {
if (Test-Path -Path $HelpSource) {
Write-Log -Message "Preparing to update PowerShell Help from $HelpSource" -Verbose
Update-Help -SourcePath $HelpSource -Recurse -Module Microsoft.PowerShell.*
Write-Log -Message "PowerShell Help updated. To update help for all additional, available, modules, run Update-Help -SourcePath `$HelpSource -Recurse" -Verbose
} else {
Write-Log -Message "Failed to access PowerShell Help path: $HelpSource" -Verbose
}
} else {
Write-Log -Message "Skipping update-help, because we're either not on Windows, or do not have admin permissions" -Verbose # `nConsider using get-help [term] -Online"
}
}