Compare commits

...

2 Commits

Author SHA1 Message Date
ce8db4db9a Created a template file for script bases 2025-01-28 12:14:23 -06:00
c014bc7ec5 Add additional function, require PS 6.2 requirement 2025-01-28 12:14:03 -06:00
2 changed files with 198 additions and 30 deletions

View File

@@ -0,0 +1,126 @@
<#
.SYNOPSIS
Provide a brief description of script purpose
.DESCRIPTION
Provide a more detailed description of script purpose
.EXAMPLE
Provide an example usage for the script
.NOTES
NAME:
AUTHOR:
LASTEDIT:
VERSION:
#>
#region Parameters
param (
# Allows modules required by the script to be predefined, and loaded or installed as needed
[Parameter(DontShow = $true)] [Array] $ModuleNames = @(),
# Sets the script storage directory into the user profile instead of in the ProgramData folder
[Parameter(Mandatory = $false)] [Switch] $UserScript = $false,
# Allows for the script logs to output to a nonstandard location. Overrides user/computer script paths
[Parameter(Mandatory = $false)] [String] $LogDir,
# Flag to disable log file output
[Parameter(Mandatory = $false)] [Switch] $NoLog,
# Allows for uninstallation of script
[Parameter(Mandatory = $false)] [Switch] $Uninstall
#TODO: Add additional parameters, if appropriate
)
#endregion Parameters
#region Architecture Check
# Verifies script is running in 64-bit PowerShell, if available. Allows for parameters to be passed to relaunched script
# Origin: https://z-nerd.com/blog/2020/03/31-intune-win32-apps-powershell-script-installer/
If ($Env:PROCESSOR_ARCHITECTURE -ne "AMD64") {
If (Test-Path -Path "$Env:WinDir\SysNative\WindowsPowerShell\v1.0\powershell.exe" -PathType Leaf) {
$ArgsString = ""
Try {
ForEach ($Key in $MyInvocation.BoundParameters.Keys) {
Switch ($MyInvocation.BoundParameters[$Key].GetType().Name) {
"SwitchParameter" { if ($MyInvocation.BoundParameters[$Key].IsPresent) { $ArgsString += "-$Key " } }
"String" { $ArgsString += "-$Key `"$($MyInvocation.BoundParameters[$Key])`" " }
"Int32" { $ArgsString += "-$Key $($MyInvocation.BoundParameters[$Key]) " }
"Boolean" { $ArgsString += "-$Key `$$($MyInvocation.BoundParameters[$Key]) " }
}
}
Start-Process -FilePath "$ENV:WINDIR\SysNative\WindowsPowershell\v1.0\PowerShell.exe" -ArgumentList `
"-ExecutionPolicy Bypass -NoProfile -File $PSCommandPath $ArgsString" -Wait -NoNewWindow
}
Catch {
Throw "Failed to start `"$PSCommandPath`" in 64-bit PowerShell"
}
Exit $LASTEXITCODE
}
}
#endregion Architecture Check
#region Imports
# Import Test-CommandExists, Add-LogEntry, Add-ListItem, Start-Logging, Import-JSONConfig
Invoke-WebRequest "https://gitea.taco.quest/Mindfang/ProjectTools/raw/branch/main/PSUtilities/ScriptTools.ps1" -OutFile "$Env:Temp\ScriptTools.ps1"
Import-Module -Name "$Env:Temp\ScriptTools.ps1" -Force
#endregion Imports
#region Functions
#endregion Functions
#region Prep
# Set the working directory for logs and additional files
$ThisScript = ([IO.FileInfo]$MyInvocation.MyCommand.Definition).BaseName
If ($UserScript) {
$AppDir = "$Env:AppData\Mindfang\$ThisScript"
}
Else {
$AppDir = "$Env:ProgramData\Mindfang\$ThisScript"
}
# Create specified appdir if it does not already exist
If (-Not (Test-Path $AppDir)) {
New-Item $AppDir -ItemType Directory
}
# Begin recording transcript file
If (-not $NoLog) {
If (-not $LogDir) {
$LogDir = $AppDir
}
Add-LogEntry (Start-Transcript "$AppDir\$ThisScript.log" -Append)
}
# Load any modules required by script
ForEach ($Module in $ModuleNames) {
If (-not(Get-Module -ListAvailable -Name $Module)) {
Try {
Add-LogEntry "Module `"$Module`" not found, installing"
Install-Module -Name $Module -Scope CurrentUser -Force -AllowClobber
}
Catch {
Add-LogEntry "Module failed to install automatically! Manaully install the module, then re-run the script." -As Error
Exit 1
}
}
Import-Module $Module
}
#endregion Prep
#region Execution
Try {
If ( -not $Uninstall) {
#TODO: "Install actions" for the script go here
}
Else {
#TODO: "Uninstall actions" for the script go here
}
}
Catch {
# Write error to logs, if an exception is caught
Write-Output "Script execution failed!"
Write-Output $_
Write-Output $_.InvocationInfo
Write-Output $_.ScriptStackTrace
Exit 1
}
Finally {
# Stop transcript, even if an error has occurred
Stop-Transcript
}
#endregion Execution

View File

@@ -1,9 +1,7 @@
#Requires -Version 6.2
Function Test-CommandExists {
<#
.SYNOPSIS
A brief description of the function or script.
Returns true if a command is present in PowerShell
.DESCRIPTION
A longer description.
@@ -93,29 +91,17 @@ Function Add-LogEntry {
[Parameter(Mandatory = $false)] [ValidateSet('Notify', 'Warning', 'Error')]$As,
[Parameter(Mandatory = $false)] [Switch]$NewLine
)
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Switch ($As) {
"Notify" {
$FGC = "Cyan"
}
"Warning" {
$FGC = "Yellow"
}
"Error" {
$FGC = "Red"
}
Default {
$FGC = "White"
}
"Notify" { $FGC = "Cyan" }
"Warning" { $FGC = "Yellow" }
"Error" { $FGC = "Red" }
Default { $FGC = "White" }
}
If ($Newline) { $Output += "`n" }
If ($Newline) {
Write-Host "`n[$Timestamp] $Message" -ForegroundColor $FGC
}
Else {
Write-Host "[$Timestamp] $Message" -ForegroundColor $FGC
}
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$Output += "[$Timestamp] $Message"
Write-Host $Output -ForegroundColor $FGC
}
Function Add-ListItem () {
@@ -154,9 +140,13 @@ Function Add-ListItem () {
#>
Param (
[Parameter(Mandatory = $true)] [ValidateNotNullorEmpty()]$Message
[Parameter(Mandatory = $true)] [ValidateNotNullorEmpty()]$Message,
[Parameter(Mandatory = $false)] [Int16] $Indent = 0
)
Write-Host " * $Message"
For ($Counter = 0; $Counter -le $Indent; $Counter++) { $Output += "`t" }
$Output += "* $Message"
Write-Host $Output
}
Function Start-Logging {
@@ -210,11 +200,63 @@ Function Start-Logging {
}
If ($Append) {
Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log" -UseMinimalHeader -Append)
Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log" -Append)
}
Else {
Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log" -UseMinimalHeader)
Add-LogEntry (Start-Transcript -Path "$LogDir\$ScriptName.log")
}
}
Function Import-JSONConfig () {
<#
.SYNOPSIS
A brief description of the function or script.
.DESCRIPTION
A longer description.
.PARAMETER FirstParameter
Description of each of the parameters.
Note:
To make it easier to keep the comments synchronized with changes to the parameters,
the preferred location for parameter documentation comments is not here,
but within the param block, directly above each parameter.
.PARAMETER SecondParameter
Description of each of the parameters.
.INPUTS
Description of objects that can be piped to the script.
.OUTPUTS
Description of objects that are output by the script.
.EXAMPLE
Example of how to run the script.
.LINK
Links to further documentation.
.NOTES
Detail on what the script does, if this is needed.
#>
Param (
[Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] $ConfigFile
)
If (Test-Path -Path $ConfigFile) {
Try {
Return (Get-Content -Raw -Path $ConfigFile | ConvertFrom-Json)
}
Catch {
Add-LogEntry "Error loading config file! Please make sure the correct path was provided, and that it is a properly formatted JSON file" -As Error
Write-Host $_
Exit 1
}
}
Else {
Add-LogEntry "Error loading config file! Please make sure the correct path was provided, and that it is a properly formatted JSON file" -As Error
Write-Host $_
Exit 1
}
}