Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable running multiple versions of PoshBot side-by-side #122

Merged
merged 3 commits into from
Oct 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions PoshBot/Classes/Command.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class Command : BaseLogger {
[hashtable]$Options
)

Import-Module -Name $Options.PoshBotManifestPath -Force -Verbose:$false -WarningAction SilentlyContinue -ErrorAction Stop

Import-Module -Name $Options.ManifestPath -Scope Local -Force -Verbose:$false -WarningAction SilentlyContinue

$cmd = $Options.Function
Expand Down Expand Up @@ -110,6 +112,7 @@ class Command : BaseLogger {
OriginalMessage = $ParsedCommand.OriginalMessage.ToHash()
ConfigurationDirectory = $script:ConfigurationDirectory
BackendType = $Backend
PoshBotManifestPath = (Join-Path -Path $script:moduleBase -ChildPath "PoshBot.psd1")
}
if ($this.FunctionInfo) {
$options.Function = $this.FunctionInfo
Expand Down
94 changes: 45 additions & 49 deletions PoshBot/Public/New-PoshBotScheduledTask.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,61 +79,57 @@ function New-PoshBotScheduledTask {
[switch]$Force
)

# Find the latest version of the module
if ($mod = Get-Module -Name PoshBot -ListAvailable -Verbose:$false | Sort-Object -Property Version | Select-Object -First 1) {
if ($Force -or (-not (Get-ScheduledTask -TaskName $Name -ErrorAction SilentlyContinue))) {
if ($PSCmdlet.ShouldProcess($Name, 'Created PoshBot scheduled task')) {

if ($Force -or (-not (Get-ScheduledTask -TaskName $Name -ErrorAction SilentlyContinue))) {
if ($PSCmdlet.ShouldProcess($Name, 'Created PoshBot scheduled task')) {

$taskParams = @{
Description = $Description
}

# Determine path to module and scheduled task script
$modPath = $mod.ModuleBase
$startScript = Join-Path -Path $modPath -ChildPath '/Task/StartPoshBot.ps1'
$taskParams = @{
Description = $Description
}

# Scheduled task action
$arg = "& '$startScript' -Path '$Path'"
$actionParams = @{
Execute = "$($env:SystemDrive)\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Argument = '-ExecutionPolicy ByPass -Command "' + $arg + '"'
WorkingDirectory = $modPath
}
$taskParams.Action = New-ScheduledTaskAction @actionParams

# Scheduled task at logon trigger
$taskParams.Trigger = New-ScheduledTaskTrigger -AtStartup

# Scheduled task settings
$settingsParams = @{
AllowStartIfOnBatteries = $true
DontStopIfGoingOnBatteries = $true
ExecutionTimeLimit = 0
RestartCount = 999
RestartInterval = (New-TimeSpan -Minutes 1)
}
$taskParams.Settings = New-ScheduledTaskSettingsSet @settingsParams
# Determine path to scheduled task script
# Not adding '..\' to -ChildPath parameter because during module build
# this script will get merged into PoshBot.psm1 and \Task folder will be
# a direct child of $PSScriptRoot
$startScript = Resolve-Path -LiteralPath (Join-Path -Path $PSScriptRoot -ChildPath 'Task\StartPoshBot.ps1')

# Scheduled task action
$arg = "& '$startScript' -Path '$Path'"
$actionParams = @{
Execute = "$($env:SystemDrive)\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Argument = '-ExecutionPolicy Bypass -NonInteractive -Command "' + $arg + '"'
WorkingDirectory = $PSScriptRoot
}
$taskParams.Action = New-ScheduledTaskAction @actionParams

# Scheduled task at logon trigger
$taskParams.Trigger = New-ScheduledTaskTrigger -AtStartup

# Scheduled task settings
$settingsParams = @{
AllowStartIfOnBatteries = $true
DontStopIfGoingOnBatteries = $true
ExecutionTimeLimit = 0
RestartCount = 999
RestartInterval = (New-TimeSpan -Minutes 1)
}
$taskParams.Settings = New-ScheduledTaskSettingsSet @settingsParams

# Create / register the task
$registerParams = @{
TaskName = $Name
Force = $true
}
# Scheduled task principal
$registerParams.User = $Credential.UserName
$registerParams.Password = $Credential.GetNetworkCredential().Password
$task = New-ScheduledTask @taskParams
$newTask = Register-ScheduledTask -InputObject $task @registerParams
if ($PassThru) {
$newTask
}
# Create / register the task
$registerParams = @{
TaskName = $Name
Force = $true
}
# Scheduled task principal
$registerParams.User = $Credential.UserName
$registerParams.Password = $Credential.GetNetworkCredential().Password
$task = New-ScheduledTask @taskParams
$newTask = Register-ScheduledTask -InputObject $task @registerParams
if ($PassThru) {
$newTask
}
} else {
Write-Error -Message "Existing task named [$Name] found. To overwrite, use the -Force"
}
} else {
Write-Error -Message 'Unable to find PoshBot module! Can not scheduled the task'
Write-Error -Message "Existing task named [$Name] found. To overwrite, use the -Force"
}
}

Expand Down
8 changes: 5 additions & 3 deletions PoshBot/Public/Start-PoshBot.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ function Start-PoshBot {
$sb = {
param(
[parameter(Mandatory)]
[hashtable]$Configuration
[hashtable]$Configuration,
[string]$PoshBotManifestPath
)

Import-Module PoshBot -ErrorAction Stop
Import-Module $PoshBotManifestPath -ErrorAction Stop

try {
$tempConfig = New-PoshBotConfiguration
Expand Down Expand Up @@ -127,8 +128,9 @@ function Start-PoshBot {

$instanceId = (New-Guid).ToString().Replace('-', '')
$jobName = "PoshBot_$instanceId"
$poshBotManifestPath = (Join-Path -Path $PSScriptRoot -ChildPath "PoshBot.psd1")

$job = Start-Job -ScriptBlock $sb -Name $jobName -ArgumentList $Configuration.ToHash()
$job = Start-Job -ScriptBlock $sb -Name $jobName -ArgumentList $Configuration.ToHash(),$poshBotManifestPath

# Track the bot instance
$botTracker = @{
Expand Down
5 changes: 1 addition & 4 deletions PoshBot/Task/StartPoshBot.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#requires -modules PoshBot

[cmdletbinding()]
param(
[parameter(Mandatory)]
Expand All @@ -16,6 +14,5 @@ param(
})]
[string]$Path
)

Import-Module PoshBot -Force -ErrorAction Stop
Import-Module "$PSScriptRoot\..\PoshBot.psd1" -Force -ErrorAction Stop
Start-PoshBot -Path $Path