157 lines
5.5 KiB
PowerShell
157 lines
5.5 KiB
PowerShell
<#
|
|
.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
|
|
.\Script-Name.ps1 -Output "Example of how to run the script"
|
|
|
|
.LINK
|
|
https://example.com/docs
|
|
|
|
.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 = @(),
|
|
# Allows import of addon scripts
|
|
[Parameter(Dontshow = $true)] [Array] $AddonUris = @(
|
|
"https://gitea.taco.quest/Mindfang/ProjectTools/raw/branch/main/PSUtilities/ScriptTools.ps1"
|
|
),
|
|
# Sets the script storage location
|
|
[Parameter(DontShow = $true)] [ValidateSet('System', 'User', 'Custom')] [String] $ScriptType = "System",
|
|
# Allows adding verbose logging and what-if command simulation to the script
|
|
[Parameter(DontShow = $true)] [Switch] $Dev,
|
|
# .PARAMETER ScriptDir
|
|
# Allows for the script logs to output to a nonstandard location. Overrides user/computer script paths
|
|
[Parameter(Mandatory = $false)] [String] $ScriptDir,
|
|
# .PARAMETER NoLog
|
|
# Flag to disable log file output
|
|
[Parameter(Mandatory = $false)] [Switch] $NoLog
|
|
#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 script addons, provided by URI
|
|
ForEach ($Uri in $AddonUris) {
|
|
$UriStatusCode = (Invoke-WebRequest -Uri $Uri -UseBasicParsing -Method Head -SkipHttpErrorCheck).StatusCode
|
|
If ($UriStatusCode -eq 200) {
|
|
Try {
|
|
Invoke-WebRequest -Uri $Uri -OutFile "$([System.IO.Path]::GetTempPath())\$(Split-Path -Path $Uri -Leaf)" -ErrorAction Stop
|
|
Write-Host "Importing addon: $(Split-Path -Path $Uri -Leaf)"
|
|
Import-Module -Name "$([System.IO.Path]::GetTempPath())\$(Split-Path -Path $Uri -Leaf)" -Force
|
|
}
|
|
Catch {
|
|
Write-Host "Error encountered importing addon"
|
|
Exit $LASTEXITCODE
|
|
}
|
|
}
|
|
Else {
|
|
Write-Host "Unable to import addon `"$(Split-Path -Path $Uri -Leaf)`" - verify URI is correct and that this device has an internet connection"
|
|
Exit $UriStatusCode
|
|
}
|
|
}
|
|
|
|
Import-Modules $ModuleNames
|
|
#endregion Imports
|
|
|
|
#region Functions
|
|
#endregion Functions
|
|
|
|
#region Prep
|
|
# Set the working directory for logs and additional files
|
|
$ThisScript = ([IO.FileInfo]$MyInvocation.MyCommand.Definition).BaseName
|
|
Switch ($ScriptType) {
|
|
"System" { $AppDir = "$Env:ProgramData\Mindfang\$ThisScript" }
|
|
"User" { $AppDir = "$Env:AppData\Mindfang\$ThisScript" }
|
|
"Custom" {
|
|
If ($ScriptDir) { $AppDir = $ScriptDir }
|
|
Else { $AppDir = ([IO.FileInfo]$MyInvocation.MyCommand.Definition).Directory }
|
|
}
|
|
}
|
|
|
|
# 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 "$LogDir\$ThisScript.log" -Append)
|
|
}
|
|
#endregion Prep
|
|
|
|
#region Execution
|
|
Try {
|
|
#TODO: Add script actions here
|
|
}
|
|
Catch {
|
|
# Write error to logs, if an exception is caught
|
|
Write-Host "Script execution failed!"
|
|
Write-Host $_
|
|
Write-Host $_.InvocationInfo
|
|
Write-Host $_.ScriptStackTrace
|
|
Exit $LASTEXITCODE
|
|
}
|
|
Finally {
|
|
# Stop transcript, even if an error has occurred
|
|
If (-Not $NoLog) {
|
|
Stop-Transcript
|
|
}
|
|
#TODO: Perform any additional disconnects, if needed
|
|
}
|
|
#endregion Execution |