Skip to content

Commit

Permalink
Merge pull request #19 from otabut/dev-0.4.0
Browse files Browse the repository at this point in the history
0.4.0 release
  • Loading branch information
otabut authored Jan 30, 2018
2 parents f2e965a + ec56fe4 commit b57652a
Show file tree
Hide file tree
Showing 17 changed files with 610 additions and 60 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ Fix - #10 Prevent from computer restart loop
Add - #11 Complete refactory of Install-PowerLSS.ps1

Add - #13 Support Scheduled tasks for W2K8R2, W2K12, W2K12R2, W2K16


## 0.4.0
Add - #7 Support of configuration file instead of command-line parameters

Add - #15 Add set/get cmdlets for PowerLSS settings

Add - #20 Support of configurations in Install-PowerLSS

48 changes: 48 additions & 0 deletions Functions/Copy-LSS_Configuration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Function Copy-LSS_Configuration
{
Param (
[parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][String]$Source,
[parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][String]$Target
)

$ErrorActionPreference = "stop"
try
{
If (!(Get-module PowerLSS))
{
Import-Module PowerLSS
}

$ParameterList = @("LogFile","InitialDelay","Include","Exclude","ValidExitCodes","Retry","AllowReboot","ContinueIfRebootRequest","ContinueOnFailure","DisableAtTheEnd","ConsoleOutput","Output","CustomLogging","DontRunPreActions","DontRunPostActions","ScriptsFolder","ScheduledTaskLogFile")
$RegPathSource = "HKLM:\SOFTWARE\PowerLSS\$Source"
$RegPathTarget = "HKLM:\SOFTWARE\PowerLSS\$Target"

if (!(Test-Path $RegPathSource -PathType Container))
{
Write-Host "Source configuration doesn't exist" -ForegroundColor Red
Return
}

if (!(Test-Path $RegPathTarget -PathType Container))
{
New-Item -Path $RegPathTarget | Out-Null
}

ForEach ($Parameter in $ParameterList)
{
$Value = (Get-ItemProperty -Path $RegPathSource)."$Parameter"
if ($Value)
{
New-ItemProperty -Path $RegPathTarget -Name $Parameter -Value $Value -PropertyType string -Force | Out-Null
}
}

Write-Host "Configuration copied successfully" -ForegroundColor Green
}
catch
{
$ErrorMessage = $_.Exception.Message
$ErrorLine = $_.InvocationInfo.ScriptLineNumber
Write-Error "Error on line $ErrorLine. The error message was: $ErrorMessage"
}
}
190 changes: 190 additions & 0 deletions Functions/Get-LSS_Configuration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
Function Get-LSS_Configuration
{
Param (
[parameter(Mandatory=$true,ParameterSetName='Specific')][ValidateNotNullOrEmpty()][String]$ConfigurationName,
[parameter(Mandatory=$true,ParameterSetName='Current')][Switch]$Current,
[parameter(Mandatory=$true,ParameterSetName='All')][Switch]$All
)

## FUNCTIONS

Function Read-Config ($AllConfigurations)
{
ForEach ($ConfigurationName in $AllConfigurations)
{
#Define default values
$LogFile = "$Path\PowerLSS.log"
$InitialDelay = 60
$Include = "ps1"
$Exclude = ""
$ValidExitCodes = "0,3010"
$Retry = $false
$AllowReboot = $false
$ContinueIfRebootRequest = $false
$ContinueOnFailure = $false
$DisableAtTheEnd = $true
$ConsoleOutput = $false
$Output = $false
$CustomLogging = $true
$DontRunPreActions = $false
$DontRunPostActions = $false
$ScriptsFolder = "PostInstall"
$ScheduledTaskLogFile = "$Path\Functions\Install-PowerLSS.log"

$RegPath = $RegBasePath + "\$ConfigurationName"
$CurrentConfiguration = ($ConfigurationName -eq (Get-ItemProperty -Path $RegBasePath)."CurrentConfiguration")
if (Test-Path $RegPath -PathType Container)
{
Write-Host "Configuration $ConfigurationName was found" -ForegroundColor Green
ForEach ($Parameter in $ParameterList)
{
$Value = (Get-ItemProperty -Path $RegPath)."$Parameter"
if ($Value)
{
Set-Variable $Parameter -Value $Value
}
else
{
Write-Host "No value found for $Parameter, using default value" -ForegroundColor Yellow
}
}
$ReturnObject = $True
}
else
{
if ($CurrentConfiguration)
{
Write-Host "Configuration $ConfigurationName cannot be found but this is the current configuration so returning default values" -ForegroundColor Yellow
$ReturnObject = $True
}
else
{
Write-Host "Configuration $ConfigurationName cannot be found" -ForegroundColor Yellow
$ReturnObject = $False
}
}

if ($ReturnObject)
{
[PSCustomObject]@{
"ConfigurationName" = $ConfigurationName
"LogFile" = $LogFile
"InitialDelay" = [int]$InitialDelay
"Include" = $Include
"Exclude" = $Exclude
"ValidExitCodes" = $ValidExitCodes
"Retry" = @{$true="True";$false="False"}[$Retry -eq "True"]
"AllowReboot" = @{$true="True";$false="False"}[$AllowReboot -eq "True"]
"ContinueIfRebootRequest" = @{$true="True";$false="False"}[$ContinueIfRebootRequest -eq "True"]
"ContinueOnFailure" = @{$true="True";$false="False"}[$ContinueOnFailure -eq "True"]
"DisableAtTheEnd" = @{$true="True";$false="False"}[$DisableAtTheEnd -eq "True"]
"ConsoleOutput" = @{$true="True";$false="False"}[$ConsoleOutput -eq "True"]
"Output" = @{$true="True";$false="False"}[$Output -eq "True"]
"CustomLogging" = @{$true="True";$false="False"}[$CustomLogging -eq "True"]
"DontRunPreActions" = @{$true="True";$false="False"}[$DontRunPreActions -eq "True"]
"DontRunPostActions" = @{$true="True";$false="False"}[$DontRunPostActions -eq "True"]
"ScriptsFolder" = $ScriptsFolder
"ScheduledTaskLogFile" = $ScheduledTaskLogFile
}
}
}
}

## MAIN

$ErrorActionPreference = "stop"
try
{
If (!(Get-module PowerLSS))
{
Import-Module PowerLSS
}

#Variables
$Path = Split-Path((Get-Module PowerLSS).path)
$RegBasePath = "HKLM:\SOFTWARE\PowerLSS"
$ParameterList = @("LogFile","InitialDelay","Include","Exclude","ValidExitCodes","Retry","AllowReboot","ContinueIfRebootRequest","ContinueOnFailure","DisableAtTheEnd","ConsoleOutput","Output","CustomLogging","DontRunPreActions","DontRunPostActions","ScriptsFolder","ScheduledTaskLogFile")

if (!(Test-Path $RegBasePath -PathType Container))
{
New-Item -Path $RegBasePath | Out-Null
}

switch ($PsCmdlet.ParameterSetName)
{
"Specific"
{
Read-Config $ConfigurationName.split(',')
}
"Current"
{
$CurrentConfiguration = (Get-ItemProperty -Path $RegBasePath)."CurrentConfiguration"
if (!($CurrentConfiguration))
{
Write-Host "No current configuration has been set, returning default values" -ForegroundColor Red

#Define default values
$LogFile = "$Path\PowerLSS.log"
$InitialDelay = 60
$Include = "ps1"
$Exclude = ""
$ValidExitCodes = "0,3010"
$Retry = $false
$AllowReboot = $false
$ContinueIfRebootRequest = $false
$ContinueOnFailure = $false
$DisableAtTheEnd = $true
$ConsoleOutput = $false
$Output = $false
$CustomLogging = $true
$DontRunPreActions = $false
$DontRunPostActions = $false
$ScriptsFolder = "PostInstall"
$ScheduledTaskLogFile = "$Path\Functions\Install-PowerLSS.log"

[PSCustomObject]@{
"LogFile" = $LogFile
"InitialDelay" = $InitialDelay
"Include" = $Include
"Exclude" = $Exclude
"ValidExitCodes" = $ValidExitCodes
"Retry" = $Retry
"AllowReboot" = $AllowReboot
"ContinueIfRebootRequest" = $ContinueIfRebootRequest
"ContinueOnFailure" = $ContinueOnFailure
"DisableAtTheEnd" = $DisableAtTheEnd
"ConsoleOutput" = $ConsoleOutput
"Output" = $Output
"CustomLogging" = $CustomLogging
"DontRunPreActions" = $DontRunPreActions
"DontRunPostActions" = $DontRunPostActions
"ScriptsFolder" = $ScriptsFolder
"ScheduledTaskLogFile" = $ScheduledTaskLogFile
}
}
else
{
Read-Config $CurrentConfiguration
}
}
"All"
{
$AllConfigurations = (Get-ChildItem -Path $RegBasePath).PSChildName
if (!($AllConfigurations))
{
Write-Host "No configuration found" -ForegroundColor Yellow
}
else
{
Read-Config $AllConfigurations
}
}
}
}
catch
{
$ErrorMessage = $_.Exception.Message
$ErrorLine = $_.InvocationInfo.ScriptLineNumber
Write-Error "Error on line $ErrorLine. The error message was: $ErrorMessage"
}
}
13 changes: 13 additions & 0 deletions Functions/Get-LSS_CurrentConfigurationName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Function Get-LSS_CurrentConfigurationName
{
$RegBasePath = "HKLM:\SOFTWARE\PowerLSS"
$CurrentConfiguration = (Get-ItemProperty -Path $RegBasePath)."CurrentConfiguration"
if (!($CurrentConfiguration))
{
Write-Host "No current configuration has been set" -ForegroundColor Red
}
else
{
$CurrentConfiguration
}
}
18 changes: 5 additions & 13 deletions Functions/Install-PowerLSS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ Function Install-PowerLSS
.DESCRIPTION
.LINK
https://github.com/otabut/PowerLSS
.NOTES
Author: Olivier TABUT
0.3.0 release (28/01/2018)
ChangeLog:
Initial version (14/01/2018)
0.2.0 release (21/01/2018)
0.3.0 release (28/01/2018)
.PARAMETER Force
Delete any previous scheduled task if exists before creating it
Expand Down Expand Up @@ -57,7 +46,10 @@ Function Install-PowerLSS
Try
{
#Import PowerLSS helper module
Import-Module PowerLSS
If (!(Get-module PowerLSS))
{
Import-Module PowerLSS
}

#Variables
$Global:Log = @()
Expand Down Expand Up @@ -174,7 +166,7 @@ Function Install-PowerLSS
if ($DoStart)
{
Write-LSS_Log -Step "Startup" -Status "Information" -Comment "Processing PowerLSS scheduled task startup"
$Command = Initialize-LSS_ScheduledTask
$Command = Start-LSS_ScheduledTask
if ($Command.Result -eq 'Success')
{
Write-LSS_Log -Step "Startup" -Status "Information" -Comment "PowerLSS scheduled task started successfully"
Expand Down
2 changes: 1 addition & 1 deletion Functions/Register-LSS_ScheduledTask.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Function Register-LSS_ScheduledTask
{
$TASK_CREATION = @{"TASK_VALIDATE_ONLY"="0x1";"TASK_CREATE"="0x2";"TASK_UPDATE"="0x4";"TASK_CREATE_OR_UPDATE"="0x6";"TASK_DISABLE"="0x8";"TASK_DONT_ADD_PRINCIPAL_ACE"="0x10";"TASK_IGNORE_REGISTRATION_TRIGGERS"="0x20"}
$flags = $TASK_CREATION.Get_Item("TASK_CREATE")
$XML = '<?xml version="1.0" encoding="UTF-16"?><Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"><RegistrationInfo><Date>2018-01-14T10:48:55.1494814</Date><Author>otabut</Author></RegistrationInfo><Triggers><BootTrigger><Enabled>true</Enabled></BootTrigger></Triggers><Principals><Principal id="Author"><UserId>S-1-5-18</UserId><RunLevel>HighestAvailable</RunLevel></Principal></Principals><Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>true</StopIfGoingOnBatteries><AllowHardTerminate>true</AllowHardTerminate><StartWhenAvailable>false</StartWhenAvailable><RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable><IdleSettings><StopOnIdleEnd>true</StopOnIdleEnd><RestartOnIdle>false</RestartOnIdle></IdleSettings><AllowStartOnDemand>true</AllowStartOnDemand><Enabled>true</Enabled><Hidden>false</Hidden><RunOnlyIfIdle>false</RunOnlyIfIdle><DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession><UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine><WakeToRun>false</WakeToRun><ExecutionTimeLimit>P3D</ExecutionTimeLimit><Priority>7</Priority></Settings><Actions Context="Author"><Exec><Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command><Arguments>-NoProfile -ExecutionPolicy ByPass -file "'+$Path+'\PowerLSS.ps1" -LogFile "'+$Path+'\PowerLSS.log"</Arguments></Exec></Actions></Task>'
$XML = '<?xml version="1.0" encoding="UTF-16"?><Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"><RegistrationInfo><Date>2018-01-14T10:48:55.1494814</Date><Author>otabut</Author></RegistrationInfo><Triggers><BootTrigger><Enabled>true</Enabled></BootTrigger></Triggers><Principals><Principal id="Author"><UserId>S-1-5-18</UserId><RunLevel>HighestAvailable</RunLevel></Principal></Principals><Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>true</StopIfGoingOnBatteries><AllowHardTerminate>true</AllowHardTerminate><StartWhenAvailable>false</StartWhenAvailable><RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable><IdleSettings><StopOnIdleEnd>true</StopOnIdleEnd><RestartOnIdle>false</RestartOnIdle></IdleSettings><AllowStartOnDemand>true</AllowStartOnDemand><Enabled>true</Enabled><Hidden>false</Hidden><RunOnlyIfIdle>false</RunOnlyIfIdle><DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession><UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine><WakeToRun>false</WakeToRun><ExecutionTimeLimit>P3D</ExecutionTimeLimit><Priority>7</Priority></Settings><Actions Context="Author"><Exec><Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command><Arguments>-NoProfile -ExecutionPolicy ByPass -file "'+$Path+'\PowerLSS.ps1" -CurrentConfiguration</Arguments></Exec></Actions></Task>'
$TaskFolder.RegisterTask("\PowerLSS",$XML,$flags,'S-1-5-18',$null,5) | Out-Null
$Result = "Success"
}
Expand Down
45 changes: 45 additions & 0 deletions Functions/Remove-LSS_Configuration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Function Remove-LSS_Configuration
{
Param (
[parameter(Mandatory=$true)][String]$ConfigurationName
)

$ErrorActionPreference = "stop"
try
{
If (!(Get-module PowerLSS))
{
Import-Module PowerLSS
}

$RegBasePath = "HKLM:\SOFTWARE\PowerLSS"
$AllConfigurations = $ConfigurationName.split(',')

ForEach ($ConfigurationName in $AllConfigurations)
{
$RegPath = "$RegBasePath\$ConfigurationName"
if (Test-Path $RegPath -PathType Container)
{
if ($ConfigurationName -ne (Get-ItemProperty -Path $RegBasePath)."CurrentConfiguration")
{
Remove-Item -Path $RegPath -Force
Write-Host "Configuration $ConfigurationName has been removed successfully" -ForegroundColor Green
}
else
{
Write-Host "Configuration $ConfigurationName cannot be removed as it is the current configuration" -ForegroundColor Red
}
}
else
{
Write-Host "Configuration $ConfigurationName cannot be found" -ForegroundColor Yellow
}
}
}
catch
{
$ErrorMessage = $_.Exception.Message
$ErrorLine = $_.InvocationInfo.ScriptLineNumber
Write-Error "Error on line $ErrorLine. The error message was: $ErrorMessage"
}
}
Loading

0 comments on commit b57652a

Please sign in to comment.