update
This commit is contained in:
@@ -0,0 +1,158 @@
|
|||||||
|
#Requires -Version 2.0
|
||||||
|
|
||||||
|
## Set the name of this module
|
||||||
|
$ModuleName = "PSModule"
|
||||||
|
|
||||||
|
## Define base URL for updating modules and resources
|
||||||
|
$BaseURL = "https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/${ModuleName}"
|
||||||
|
|
||||||
|
## Set the path to this module
|
||||||
|
$ModulePath = "${Env:MSPDir}\Scripts\${ModuleName}"
|
||||||
|
|
||||||
|
## Define the paths for where we'll place stuff on the endpoint
|
||||||
|
$MSPRoot = $Env:MSPDir
|
||||||
|
$MSPScripts = "${MSPRoot}\Scripts"
|
||||||
|
$MSPTools = "${MSPRoot}\Tools"
|
||||||
|
$MSPLogs = "${MSPRoot}\Logs"
|
||||||
|
|
||||||
|
## Function to simply import a module and return an error message if it fails
|
||||||
|
Function Load-Module
|
||||||
|
{
|
||||||
|
param([string]$modulename)
|
||||||
|
|
||||||
|
## Get a new copy of the module
|
||||||
|
Get-Module $modulename
|
||||||
|
|
||||||
|
## Form the local file path to the specified module
|
||||||
|
$FilePath = "${ModulePath}\${modulename}.psm1"
|
||||||
|
|
||||||
|
## Import the module
|
||||||
|
If ( Test-Path $FilePath )
|
||||||
|
{
|
||||||
|
Try { Import-Module -Name $FilePath }
|
||||||
|
Catch { Write-Output $_.Exception.Message }
|
||||||
|
} Else { Write-Output "Module does not exist: ${FilePath}" }
|
||||||
|
}
|
||||||
|
|
||||||
|
## Function to update or install a module
|
||||||
|
Function Get-Module
|
||||||
|
{
|
||||||
|
param([string]$modulename)
|
||||||
|
|
||||||
|
## Form the URL to the specified module
|
||||||
|
$ModuleURL = "${BaseURL}/${modulename}.psm1"
|
||||||
|
|
||||||
|
## Form the local file path to the specified module
|
||||||
|
$FilePath = "${ModulePath}\${modulename}.psm1"
|
||||||
|
|
||||||
|
## Download the new file, overwriting the local copy
|
||||||
|
If ( IsDownloadable $ModuleURL )
|
||||||
|
{
|
||||||
|
Try { (New-Object Net.WebClient).DownloadFile($ModuleURL, $FilePath) }
|
||||||
|
Catch { Write-Output $_.Exception.Message }
|
||||||
|
} Else { Write-Output "Could not download ${ModuleURL}" }
|
||||||
|
}
|
||||||
|
|
||||||
|
## Determines whether a file can be downloaded or not
|
||||||
|
Function IsDownloadable
|
||||||
|
{
|
||||||
|
param([string]$url)
|
||||||
|
|
||||||
|
## Create a web request
|
||||||
|
$HTTPRequest = [System.Net.WebRequest]::Create($url)
|
||||||
|
|
||||||
|
Try
|
||||||
|
{
|
||||||
|
## Get the response
|
||||||
|
$HTTPResponse = $HTTPRequest.GetResponse()
|
||||||
|
|
||||||
|
## Save the status code
|
||||||
|
$HTTPStatus = [int]$HTTPResponse.StatusCode
|
||||||
|
}
|
||||||
|
Catch [System.Net.WebException] { $HTTPResponse = $_.Exception.Response }
|
||||||
|
|
||||||
|
## Close the connection, because we're done with it
|
||||||
|
Finally { $HTTPResponse.Close() }
|
||||||
|
|
||||||
|
## Test to make sure we received a status of 200 "OK" from the resource
|
||||||
|
If ($HTTPStatus -eq 200) { Return $true }
|
||||||
|
|
||||||
|
## If the status code is anything other than 200, return $false
|
||||||
|
Else { Return $false }
|
||||||
|
}
|
||||||
|
|
||||||
|
## EXPORT FUNCTION
|
||||||
|
## Function to log activity to the configured log directory
|
||||||
|
Function global:Log
|
||||||
|
{
|
||||||
|
Begin {}
|
||||||
|
Process {}
|
||||||
|
End {}
|
||||||
|
}
|
||||||
|
|
||||||
|
## EXPORT FUNCTION
|
||||||
|
## Function to determine if the current user context is an Administrator
|
||||||
|
Function global:IsAdmin
|
||||||
|
{
|
||||||
|
Begin {}
|
||||||
|
Process
|
||||||
|
{
|
||||||
|
If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )
|
||||||
|
{ Return $true } Else { Return $false }
|
||||||
|
}
|
||||||
|
End {}
|
||||||
|
}
|
||||||
|
|
||||||
|
## EXPORT FUNCTION
|
||||||
|
## Function to retrieve and run a powershell script
|
||||||
|
Function Run-PSScript
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[string]$Type='management-scripts',
|
||||||
|
[string]$Name,
|
||||||
|
[string]$Arguments
|
||||||
|
)
|
||||||
|
|
||||||
|
## Form the URL to the specified script
|
||||||
|
$URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1"
|
||||||
|
|
||||||
|
If ( IsDownloadable $URL )
|
||||||
|
{
|
||||||
|
$Script = (New-Object Net.WebClient).DownloadString($URL)
|
||||||
|
$ScriptBlock = [Scriptblock]::Create($Script)
|
||||||
|
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
|
||||||
|
Catch { Write-Output $_.Exception.Message }
|
||||||
|
} Else { Write-Output "Could not retrieve ${URL}" }
|
||||||
|
}
|
||||||
|
|
||||||
|
## Make sure all the management directories exist
|
||||||
|
If ( !(Test-Path $MSPRoot) )
|
||||||
|
{
|
||||||
|
Try { New-Item -Path $MSPRoot -ItemType Directory -Force | Out-Null }
|
||||||
|
Catch { Write-Output $_.Exception.Message }
|
||||||
|
}
|
||||||
|
If ( !(Test-Path $MSPScripts) )
|
||||||
|
{
|
||||||
|
Try { New-Item -Path $MSPScripts -ItemType Directory -Force | Out-Null }
|
||||||
|
Catch { Write-Output $_.Exception.Message }
|
||||||
|
}
|
||||||
|
If ( !(Test-Path $MSPTools) )
|
||||||
|
{
|
||||||
|
Try { New-Item -Path $MSPTools -ItemType Directory -Force | Out-Null }
|
||||||
|
Catch { Write-Output $_.Exception.Message }
|
||||||
|
}
|
||||||
|
If ( !(Test-Path $MSPLogs) )
|
||||||
|
{
|
||||||
|
Try { New-Item -Path $MSPLogs -ItemType Directory -Force | Out-Null }
|
||||||
|
Catch { Write-Output $_.Exception.Message }
|
||||||
|
}
|
||||||
|
|
||||||
|
## Import all modules
|
||||||
|
$ListOfModules = @(
|
||||||
|
"PSModule",
|
||||||
|
"PSModule-SetWinEvtLog"
|
||||||
|
)
|
||||||
|
ForEach ($module in $ListOfModules) { Load-Module $module }
|
||||||
|
|
||||||
|
## Make sure this module exports only the functions that should be exported
|
||||||
|
Export-ModuleMember -Function Log,IsAdmin,Run-PSScript
|
||||||
+20
-153
@@ -1,160 +1,27 @@
|
|||||||
#Requires -Version 2.0
|
#Requires -Version 2.0
|
||||||
|
|
||||||
## Set the name of this module
|
param(
|
||||||
$ModuleName = "PSModule"
|
[string]$Type='management-scripts',
|
||||||
|
[string]$Name,
|
||||||
|
[string]$Arguments)
|
||||||
|
|
||||||
## Define base URL for updating modules and resources
|
## Form the URL to the specified script
|
||||||
$BaseURL = "https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/${ModuleName}"
|
$URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1"
|
||||||
|
|
||||||
## Set the path to this module
|
## Get the script
|
||||||
$ModulePath = "${Env:MSPDir}\Scripts\${ModuleName}"
|
Write-Output "Retrieving script: ${Name}"
|
||||||
|
Try { $Script = (New-Object Net.WebClient).DownloadString($URL) }
|
||||||
|
Catch { Write-Output $_.Exception.Message }
|
||||||
|
|
||||||
## Define the paths for where we'll place stuff on the endpoint
|
## Make a script block
|
||||||
$MSPRoot = $Env:MSPDir
|
Write-Output "Creating scriptblock"
|
||||||
$MSPScripts = "${MSPRoot}\Scripts"
|
Try { $ScriptBlock = [Scriptblock]::Create($Script) }
|
||||||
$MSPTools = "${MSPRoot}\Tools"
|
Catch { Write-Output $_.Exception.Message }
|
||||||
$MSPLogs = "${MSPRoot}\Logs"
|
|
||||||
|
|
||||||
## Function to simply import a module and return an error message if it fails
|
## Run the script any any arguments
|
||||||
Function Load-Module
|
Write-Output "${Name}.ps1 ${Arguments}"
|
||||||
{
|
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
|
||||||
param([string]$modulename)
|
Catch { Write-Output $_.Exception.Message }
|
||||||
|
|
||||||
## Get a new copy of the module
|
## Finish script
|
||||||
Get-Module $modulename
|
Write-Output "${Name}.ps1 has finished"
|
||||||
|
|
||||||
## Form the local file path to the specified module
|
|
||||||
$FilePath = "${ModulePath}\${modulename}.psm1"
|
|
||||||
|
|
||||||
## Import the module
|
|
||||||
If ( Test-Path $FilePath )
|
|
||||||
{
|
|
||||||
Try { Import-Module -Name $FilePath }
|
|
||||||
Catch { Write-Output $_.Exception.Message }
|
|
||||||
} Else { Write-Output "Module does not exist: ${FilePath}" }
|
|
||||||
}
|
|
||||||
|
|
||||||
## Function to update or install a module
|
|
||||||
Function Get-Module
|
|
||||||
{
|
|
||||||
param([string]$modulename)
|
|
||||||
|
|
||||||
## Form the URL to the specified module
|
|
||||||
$ModuleURL = "${BaseURL}/${modulename}.psm1"
|
|
||||||
|
|
||||||
## Form the local file path to the specified module
|
|
||||||
$FilePath = "${ModulePath}\${modulename}.psm1"
|
|
||||||
|
|
||||||
## Download the new file, overwriting the local copy
|
|
||||||
If ( IsDownloadable $ModuleURL )
|
|
||||||
{
|
|
||||||
Try { (New-Object Net.WebClient).DownloadFile($ModuleURL, $FilePath) }
|
|
||||||
Catch { Write-Output $_.Exception.Message }
|
|
||||||
} Else { Write-Output "Could not download ${ModuleURL}" }
|
|
||||||
}
|
|
||||||
|
|
||||||
## Function to retrieve and run a script
|
|
||||||
Function Run-Script
|
|
||||||
{
|
|
||||||
param(
|
|
||||||
[string]$Type='management-scripts',
|
|
||||||
[string]$Name,
|
|
||||||
[string]$Arguments
|
|
||||||
)
|
|
||||||
|
|
||||||
## Form the URL to the specified script
|
|
||||||
$URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1"
|
|
||||||
|
|
||||||
If ( IsDownloadable $URL )
|
|
||||||
{
|
|
||||||
$Type = 'management-scripts'
|
|
||||||
$Name = "test"
|
|
||||||
$URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${Name}.ps1"
|
|
||||||
$Script = (New-Object Net.WebClient).DownloadString($URL)
|
|
||||||
$ScriptBlock = [Scriptblock]::Create($Script)
|
|
||||||
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
|
|
||||||
Catch { Write-Output $_.Exception.Message }
|
|
||||||
} Else { Write-Output "Could not retrieve ${URL}" }
|
|
||||||
}
|
|
||||||
|
|
||||||
## Determines whether a file can be downloaded or not
|
|
||||||
Function IsDownloadable
|
|
||||||
{
|
|
||||||
param([string]$url)
|
|
||||||
|
|
||||||
## Create a web request
|
|
||||||
$HTTPRequest = [System.Net.WebRequest]::Create($url)
|
|
||||||
|
|
||||||
Try
|
|
||||||
{
|
|
||||||
## Get the response
|
|
||||||
$HTTPResponse = $HTTPRequest.GetResponse()
|
|
||||||
|
|
||||||
## Save the status code
|
|
||||||
$HTTPStatus = [int]$HTTPResponse.StatusCode
|
|
||||||
}
|
|
||||||
Catch [System.Net.WebException] { $HTTPResponse = $_.Exception.Response }
|
|
||||||
|
|
||||||
## Close the connection, because we're done with it
|
|
||||||
Finally { $HTTPResponse.Close() }
|
|
||||||
|
|
||||||
## Test to make sure we received a status of 200 "OK" from the resource
|
|
||||||
If ($HTTPStatus -eq 200) { Return $true }
|
|
||||||
|
|
||||||
## If the status code is anything other than 200, return $false
|
|
||||||
Else { Return $false }
|
|
||||||
}
|
|
||||||
|
|
||||||
## EXPORT FUNCTION
|
|
||||||
## Function to log activity to the configured log directory
|
|
||||||
Function global:Log
|
|
||||||
{
|
|
||||||
Begin {}
|
|
||||||
Process {}
|
|
||||||
End {}
|
|
||||||
}
|
|
||||||
|
|
||||||
## EXPORT FUNCTION
|
|
||||||
## Function to determine if the current user context is an Administrator
|
|
||||||
Function global:IsAdmin
|
|
||||||
{
|
|
||||||
Begin {}
|
|
||||||
Process
|
|
||||||
{
|
|
||||||
If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )
|
|
||||||
{ Return $true } Else { Return $false }
|
|
||||||
}
|
|
||||||
End {}
|
|
||||||
}
|
|
||||||
|
|
||||||
## Make sure all the management directories exist
|
|
||||||
If ( !(Test-Path $MSPRoot) )
|
|
||||||
{
|
|
||||||
Try { New-Item -Path $MSPRoot -ItemType Directory -Force | Out-Null }
|
|
||||||
Catch { Write-Output $_.Exception.Message }
|
|
||||||
}
|
|
||||||
If ( !(Test-Path $MSPScripts) )
|
|
||||||
{
|
|
||||||
Try { New-Item -Path $MSPScripts -ItemType Directory -Force | Out-Null }
|
|
||||||
Catch { Write-Output $_.Exception.Message }
|
|
||||||
}
|
|
||||||
If ( !(Test-Path $MSPTools) )
|
|
||||||
{
|
|
||||||
Try { New-Item -Path $MSPTools -ItemType Directory -Force | Out-Null }
|
|
||||||
Catch { Write-Output $_.Exception.Message }
|
|
||||||
}
|
|
||||||
If ( !(Test-Path $MSPLogs) )
|
|
||||||
{
|
|
||||||
Try { New-Item -Path $MSPLogs -ItemType Directory -Force | Out-Null }
|
|
||||||
Catch { Write-Output $_.Exception.Message }
|
|
||||||
}
|
|
||||||
|
|
||||||
## Import all modules
|
|
||||||
$ListOfModules = @(
|
|
||||||
"PSModule",
|
|
||||||
"PSModule-SetWinEvtLog"
|
|
||||||
)
|
|
||||||
ForEach ($module in $ListOfModules) { Load-Module $module }
|
|
||||||
|
|
||||||
## Make sure this module exports only the functions that should be exported
|
|
||||||
Export-ModuleMember -Function Log,IsAdmin
|
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
Function global:Set-WinEvtLog {
|
||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Enable/Disable an event log.
|
||||||
|
.DESCRIPTION
|
||||||
|
Enable or disable a Windows Event Log using the System.Diagnostics.Eventing.Reader.EventLogConfiguration object.
|
||||||
|
This function only outputs terminating errors to the console. If you want to see more detail use -verbose
|
||||||
|
.EXAMPLE
|
||||||
|
Set-WinEvtLog -Enable -Log "Microsoft-Windows-DNS-Client/Operational"
|
||||||
|
.EXAMPLE
|
||||||
|
Set-WinEvtLog -Disable -Log (Get-Content ListOfLogs.txt)
|
||||||
|
.EXAMPLE
|
||||||
|
Get-Content ListOfLogs.txt | Set-WinEvtLog -Enable
|
||||||
|
.PARAMETER Log
|
||||||
|
The log(s) to modIfy.
|
||||||
|
.PARAMETER Enable
|
||||||
|
Enables the log(s).
|
||||||
|
.PARAMETER Disable
|
||||||
|
Disables the log(s).
|
||||||
|
#>
|
||||||
|
|
||||||
|
[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
|
||||||
|
param
|
||||||
|
(
|
||||||
|
[Parameter(Mandatory=$True,
|
||||||
|
ValueFromPipeline=$False,
|
||||||
|
ValueFromPipelineByPropertyName=$True,
|
||||||
|
HelpMessage='Which log do you want to enable?')]
|
||||||
|
[string[]]$Log,
|
||||||
|
[Parameter(Mandatory=$True,
|
||||||
|
ValueFromPipelineByPropertyName=$True,
|
||||||
|
ParameterSetName = 'Enable')]
|
||||||
|
[Switch]$Enable,
|
||||||
|
[Parameter(Mandatory=$True,
|
||||||
|
ValueFromPipelineByPropertyName=$True,
|
||||||
|
ParameterSetName = 'Disable')]
|
||||||
|
[Switch]$Disable
|
||||||
|
)
|
||||||
|
|
||||||
|
Begin
|
||||||
|
{
|
||||||
|
# Keep track of changes made to event logs in a separate file.
|
||||||
|
$RecordLog = "${Env:MSPDir}\Logs\WinEvtLogs-Enabled.log"
|
||||||
|
|
||||||
|
If ( !(Test-Path -Path $RecordLog) )
|
||||||
|
{
|
||||||
|
# Create the record keeping log file If it doesn't exist.
|
||||||
|
$NewFile = ((New-Item -Path $RecordLog -ItemType file).name | Out-String)
|
||||||
|
$NewFile = $NewFile.Trim()
|
||||||
|
Write-Verbose "$NewFile did not exist and has been created."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Process
|
||||||
|
{
|
||||||
|
# Make sure Record Keeping is properly configured, otherwise exit.
|
||||||
|
If ( !($RecordLog) )
|
||||||
|
{
|
||||||
|
Write-Verbose "The record keeping log does not exist, this function will not continue."
|
||||||
|
Return
|
||||||
|
}
|
||||||
|
|
||||||
|
# Loop through the name(s) in the $Log variable passed to this function.
|
||||||
|
ForEach ($name in $Log)
|
||||||
|
{
|
||||||
|
# Process the data only If the script should Process data.
|
||||||
|
If ( $pscmdlet.ShouldProcess($name, 'Enable Log') )
|
||||||
|
{
|
||||||
|
# Load the RecordLog into a variable.
|
||||||
|
$PreviouslyEnabledLogs = Get-Content $RecordLog
|
||||||
|
|
||||||
|
# Determine whether to Enable or Disable logging of the specIfied log.
|
||||||
|
Switch ($PSCmdlet.ParameterSetName)
|
||||||
|
{
|
||||||
|
'Enable' { $SetEnable = $True }
|
||||||
|
'Disable' { $SetEnable = $False }
|
||||||
|
}
|
||||||
|
|
||||||
|
# Create a new object for the specIfied log.
|
||||||
|
$LogObject = New-Object System.Diagnostics.Eventing.Reader.EventLogConfiguration $name
|
||||||
|
|
||||||
|
# Set the variable to determine whether the log has been enabled by this script or not
|
||||||
|
$IsEnabledByFunction = $False
|
||||||
|
|
||||||
|
# Determine whether or not the log had been enabled by this function.
|
||||||
|
ForEach ($item in $PreviouslyEnabledLogs)
|
||||||
|
{ If ($item -eq $name) { $IsEnabledByFunction = $True } }
|
||||||
|
|
||||||
|
# Act based on whether the log is already enabled or not.
|
||||||
|
Switch ($logObject.IsEnabled)
|
||||||
|
{
|
||||||
|
$True
|
||||||
|
{
|
||||||
|
# Inform user whether this log has been enabled by this function or not.
|
||||||
|
If ($IsEnabledByFunction)
|
||||||
|
{ Write-Verbose "$name has previously been enabled by this function." }
|
||||||
|
Else { Write-Verbose "$name has previously been enabled, but not by this function." }
|
||||||
|
}
|
||||||
|
|
||||||
|
$False
|
||||||
|
{
|
||||||
|
# Inform user that this log has been disabled by something other than this function.
|
||||||
|
If ($IsEnabledByFunction)
|
||||||
|
{ Write-Verbose "$name should already be enabled by this function, but has been disabled by other means." }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# ModIfy the log to either enable or disable it.
|
||||||
|
If ($LogObject.IsEnabled -eq $SetEnable)
|
||||||
|
{ Write-Verbose "$name is already in the desired state." }
|
||||||
|
Else
|
||||||
|
{
|
||||||
|
# Set the log to the desired state.
|
||||||
|
$LogObject.IsEnabled = $SetEnable
|
||||||
|
|
||||||
|
# Save changes to the log.
|
||||||
|
$LogObject.SaveChanges()
|
||||||
|
|
||||||
|
# VerIfy the change has been made.
|
||||||
|
Switch ($LogObject.IsEnabled)
|
||||||
|
{
|
||||||
|
$True
|
||||||
|
{
|
||||||
|
Write-Verbose "$name has been enabled."
|
||||||
|
|
||||||
|
# Make sure this log wasn't already enabled by this function so we don't write the same name more than once in $RecordLog
|
||||||
|
If (!($IsEnabledByFunction)) { Add-Content -Path $RecordLog "$name" }
|
||||||
|
}
|
||||||
|
|
||||||
|
$False
|
||||||
|
{
|
||||||
|
# Remove $name from $RecordLog If it was put there by this function.
|
||||||
|
If ($IsEnabledByFunction)
|
||||||
|
{
|
||||||
|
# Clear the contents of $RecordLog
|
||||||
|
Clear-Content -Path $RecordLog
|
||||||
|
|
||||||
|
# Add each item back in that was already there except for the current $name
|
||||||
|
ForEach ($filename In $PreviouslyEnabledLogs)
|
||||||
|
{ If ($filename -ne $name) { Add-Content -Path $RecordLog $filename} }
|
||||||
|
}
|
||||||
|
Write-Verbose "$name has been disabled."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
end {}
|
||||||
|
}
|
||||||
|
Export-ModuleMember -Function Set-WinEvtLog
|
||||||
Reference in New Issue
Block a user