Skip to content

Commit

Permalink
Added New-PowershellStarterService.ps1 that automatically changes the…
Browse files Browse the repository at this point in the history
… app.config and adds powershellstarter.exe as a service.
  • Loading branch information
AlexAsplund committed May 27, 2017
1 parent 6afed5b commit 17582af
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
3 changes: 3 additions & 0 deletions PowershellStarter/PowershellStarter.cs
Expand Up @@ -43,8 +43,11 @@ public PowershellStarterService()
System.Diagnostics.EventLog.CreateEventSource(ConfigurationManager.AppSettings["EventLogSource"], ConfigurationManager.AppSettings["EventLog"]);

}

eventLog1.Source = ConfigurationManager.AppSettings["EventLogSource"];
eventLog1.Log = ConfigurationManager.AppSettings["EventLog"];


}

// For fetching exited event from script and forcing service to terminate
Expand Down
103 changes: 103 additions & 0 deletions PowershellStarter/stable/Current/New-PowershellStarterService.ps1
@@ -0,0 +1,103 @@
######################################################################
#
#
# Automatically changes scriptpath and scriptargs in powershellstarter.exe.config
# and adds powershellstarter.exe as a service.
#
#
#
#####################################################################
Param(

[parameter(mandatory=$true)]
[string]$ScriptPath,
[parameter(mandatory=$true)]
[string]$ScriptParameters,
[parameter(mandatory=$true)]
[string]$ServiceName,
[parameter(mandatory=$true)]
[ValidateSet("Automatic", "Manual", "Boot", "Disabled","System")]
[string]$StartupType,
[System.Management.Automation.PSCredential]$Credential,
[string]$DependsOn,
[string]$ServiceDisplayName,
[switch]$Confirm,
[switch]$StartService
)

Import-Module Microsoft.PowerShell.Management

<#
.Synopsis
Gets a app.conf and sets the value of a specified key
.DESCRIPTION
Gets a app.conf and sets the value of a specified key
.EXAMPLE
Set-AppConfValue -Path $AppConfigFile -Key "ScriptParameters" -Value $ScriptParameters
#>
function Set-AppConfValue
{
[CmdletBinding()]
Param
(
# Path to app.conf
[Parameter(Mandatory=$true)]
$Path,
# Key
[Parameter(Mandatory=$true)]
$Key,
[Parameter(Mandatory=$true)]
$Value

)

Begin
{
$AppConfig = New-Object XML

if(Test-Path $Path){

$AppConfig.Load($appConfigFile)

}else{

Write-Error "file not found"
break

}

}
Process
{

($AppConfig.configuration.appSettings.add | ? {$_.key -eq $Key}).value = $Value

}
End
{

$appConfig.Save($appConfigFile)

}
}

# Get the directory of this script file
$CurrentDirectory = [IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)

# Get the full path and file name of the App.config file in the same directory as this script
$AppConfigFile = [IO.Path]::Combine($currentDirectory, 'powershellstarter.exe.config')
$AppPath = [IO.Path]::Combine($currentDirectory, 'powershellstarter.exe')

Set-AppConfValue -Path $AppConfigFile -Key "ScriptPath" -Value $ScriptPath
Set-AppConfValue -Path $AppConfigFile -Key "ScriptParameters" -Value $ScriptParameters

if($ServiceDescription -eq $null){$ServiceDescription = $ServiceName}


New-Service -Name $ServiceName -BinaryPathName $AppPath -DisplayName $ServiceName -StartupType ([System.ServiceProcess.ServiceStartMode]$StartupType) -Credential $Credential -Confirm:$Confirm

if($StartService){

Start-Service $ServiceName

}
4 changes: 2 additions & 2 deletions PowershellStarter/stable/Current/PowershellStarter.exe.config
Expand Up @@ -6,8 +6,8 @@
<appSettings>
<add key="Eventlog" value="Application" />
<add key="EventlogSource" value="PSTestService" />
<add key="ScriptPath" value="C:\temp\pstest.ps1" />
<add key="ScriptParameters" value="-TestParameter hello" />
<add key="ScriptPath" value="test" />
<add key="ScriptParameters" value="test" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<system.web>
Expand Down

0 comments on commit 17582af

Please sign in to comment.