Skip to content

Instantly share code, notes, and snippets.

@haydosw
Created March 12, 2015 03:09
Show Gist options
  • Save haydosw/5fe79ff3f6e0d8836b9e to your computer and use it in GitHub Desktop.
Save haydosw/5fe79ff3f6e0d8836b9e to your computer and use it in GitHub Desktop.
Simple script to scan the .\bin directory, look for Topshelf service executable (very basic *.exe scan) and install. (http://docs.topshelf-project.com/en/latest/overview/faq.html#what-about-command-line-arguments)
# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal = new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this
$Host.UI.RawUI.WindowTitle = "Service Manager (Elevated)"
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
clear-host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process
exit
}
# Run your code that needs to be elevated her
Get-ChildItem "$PSScriptRoot\bin" -Filter *.exe | Where {$_.Name -notlike '*vshost*' -and $_.Name -notlike 'config'} | ForEach-Object {
Write-Host "-------------------------------------------"
Write-Host "Service Installer [install service]"
Write-Host "-------------------------------------------"
Write-Host "`nService: $_`n"
Write-Host "Is this the correct service?`n(enter to contiune, or simply close and install manually)"
Read-Host
Write-Host "-------------------------------------------"
Write-Host "Installing..."
Write-Host "-------------------------------------------"
Invoke-Expression "$PSScriptRoot\bin\$_ install"
Write-Host "`n-------------------------------------------"
Write-Host "Starting..."
Write-Host "-------------------------------------------"
Invoke-Expression "$PSScriptRoot\bin\$_ start"
Write-Host "`n`n-------------------------------------------"
Write-Host "End"
Write-Host "-------------------------------------------"
Read-Host
exit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment