-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-BestDefaults.ps1
More file actions
123 lines (114 loc) · 5.55 KB
/
Set-BestDefaults.ps1
File metadata and controls
123 lines (114 loc) · 5.55 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
SupportsShouldProcess=$false,
PositionalBinding=$false,
HelpUri = 'https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx and https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-5.1',
ConfirmImpact='Medium')]
[Alias('SBD')]
[OutputType([String])]
Param
(
# Param1 help description
[Parameter(Mandatory=$false,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false,
ValueFromRemainingArguments=$false,
Position=0,
ParameterSetName='Parameter Set 1')]
[Alias('PHP')]
$PwshHomePath = $env:HOMEPATH
)
<#
.Synopsis
Setup PowerShell Profile for current user and enalbe "Start-Transcript" as defaulf for powershell console.
.DESCRIPTION
Meant to be an example script for people new to PowerShell will setup PowerShell Profiles and Enable "Start-Trnscript". Also to be used as an example script to help teach novice script some baisc best practices and scripting. If you notice some best practices messsing within the script, please fill free to updated.
.EXAMPLE
Set-BestDefaults
.NOTES
This is a communety project, fill free to make it even better.
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
function Set-BestDefaults
{
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
SupportsShouldProcess=$false,
PositionalBinding=$false,
HelpUri = 'https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx and https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-5.1',
ConfirmImpact='Medium')]
[Alias('sbd')]
[OutputType([String])]
Param
(
# Param1 help description
[Parameter(Mandatory=$false,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false,
ValueFromRemainingArguments=$false,
Position=0,
ParameterSetName='Parameter Set 1')]
[Alias('PHP')]
$PwshHomePath = $env:HOMEPATH
)
Begin
{
########################
# Set Script variables #
########################
#Check to see if "PowerShell Profile" is already created (True/False) Value
$PowerShellProfile = (Test-path $profile)
#Select the Profile location Default Script
$ShellProfileObject = $PROFILE.Split('\')[-1]
#Check to see if "DailyLog" folder is created under PowerShell Profile is already created (True/False) Value
$DailyLogFolder = (Test-Path ($profile.TrimEnd($ShellProfileObject) + 'DailyLog'))
#Create daily log name to be used with setting up Powershell transcripting
$DailyLogName = "PowerShellDailyTranscript_" + "$(Get-Date -UFormat %Y-%m-%d)" + ".log"
############################
# Display Script Variables #
############################
Write-Verbose "PowerShellProfile Value equals $($PowerShellProfile)"
Write-Verbose "DailyLogFolder Value equals $($DailyLogFolder)"
Write-Verbose "DailyLogName Value equals $($DailyLogName)"
Write-Verbose "Shell Profile Object equals $($ShellProfileObject)"
}
Process
{
if ($pscmdlet.ShouldProcess("$($env:COMPUTERNAME)", "Setup PowerShell Profile & Transcripting"))
{
######################################
# Setup Best Defualts for PowerShell #
######################################
#Creates PowerShell Profile if it does not already exist
if ($PowerShellProfile -ne $true) {
Write-Verbose "Attempting to create PowerShell profile for User: $($env:USERNAME)"
New-item -path $profile -ItemType File -Force
}
#Creates DailyLog folder if it does not already exist
if ($DailyLogFolder -ne $true) {
Write-Verbose "Attempting to create DailyLog folder for User: $($env:USERNAME)"
New-Item -Path ($profile.TrimEnd($ShellProfileObject) + 'DailyLog') -ItemType Directory -Force
}
#Enabled Transcripting within user PowerShell Profile if it does not already exist
Write-Verbose "Attempting to enable Transcripting for User: $($env:USERNAME)"
if ((Get-Content $profile) -ne "Start-Transcript -Path $($profile.TrimEnd($ShellProfileObject) + 'DailyLog\' + $DailyLogName + ' -Append -IncludeInvocationHeader')")
{
Write-Verbose "Attempting to add transcript command to $($profile)"
Add-Content -Path $profile -Value "Start-Transcript -Path $($profile.TrimEnd($ShellProfileObject) + 'DailyLog\' + $DailyLogName + ' -Append -IncludeInvocationHeader')"
}
}
}
End
{
Write-Verbose "PowerShell Profile Enabled: $(Test-Path ($profile))"
Write-Verbose "Created DailyLog Folder: $(Test-Path ($profile.TrimEnd($ShellProfileObject) + 'DailyLog'))"
Write-Verbose "PowerShell Transcript Enabled: $((Get-Content $profile) -eq "Start-Transcript -Path $($profile.TrimEnd($ShellProfileObject) + 'DailyLog\' + $DailyLogName + ' -Append -IncludeInvocationHeader')")"
}
}
##############
# Run Script #
##############
Set-BestDefaults -PwshHomePath $PwshHomePath