Skip to content

Instantly share code, notes, and snippets.

@michaelnoonan
Created July 10, 2013 01:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaelnoonan/5962807 to your computer and use it in GitHub Desktop.
Save michaelnoonan/5962807 to your computer and use it in GitHub Desktop.
Useful Deployment Scripts
Set-StrictMode -Version Latest
$libPath = (Split-Path -Parent $MyInvocation.MyCommand.Definition)
Import-Module $libPath\SecurityLib.psm1
function New-WindowsService {
param
(
[Parameter(Mandatory=$True,Position=0,HelpMessage="The name of the Windows Service")]
[string]$serviceName,
[Parameter(Mandatory=$True,Position=1,HelpMessage="The full path to the service executable and any command line arguments")]
[string]$binaryPathName,
[Parameter(Mandatory=$False,Position=2,HelpMessage="The User Principal Name (UPN) for the account to run the service as")]
[string]$userPrincipalName,
[Parameter(Mandatory=$False,Position=3,HelpMessage="The folder where the PowerShell credential file is located for the specified user account")]
[string]$credentialsFolder
)
$ErrorActionPreference = "Stop"
$service = Get-Service | Where-Object { $_.Name -eq "$serviceName" }
if ($service) {
Write-Host "Service $serviceName already exists... removing."
$serviceWmi = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
$serviceWmi.delete()
}
if (-not $userPrincipalName) {
Write-Host "Creating new service $serviceName running under default service account"
$service = New-Service -Name "$serviceName" -BinaryPathName "$binaryPathName" -StartupType Automatic
} else {
Write-Host "Creating new service $serviceName running as $userPrincipalName"
# have obtained this credential the same way it was done in the WebSiteLib module, the way it was done before
# throws an exception (Cannot convert argument "1", with value: "System.Management.Automation.PSCredential") when deploying
# plus it gets the $credential twice, once in the call to Get-Credential from file, and then again here.
$credential = Get-CredentialFromFile -userPrincipalName $userPrincipalName -credentialsFolder $credentialsFolder
if (-not $credential) {
throw "The powershell credential could not be created."
}
$service = New-Service -Name "$serviceName" -BinaryPathName "$binaryPathName" -StartupType Automatic -Credential $credential
}
}
function Start-WindowsServices {
param
(
[Parameter(Mandatory=$True,Position=0,HelpMessage="The names of the Windows Services to start")]
[string[]]$serviceNames,
[Parameter(Mandatory=$False,Position=1,HelpMessage="The number of seconds to wait before checking the Services")]
[int]$SecondsToWait = 30
)
$ErrorActionPreference = "Stop";
Write-Host "Starting these services: $serviceNames";
$serviceNames | Start-Service;
Start-Sleep -s $SecondsToWait;
Write-Host "Checking state of these services: $serviceNames";
$notRunning = ""
$serviceNames | Get-Service | Where-Object { $_.Status -ne "Running" } | % { $notRunning += " " + $_.Name };
if (-not $notRunning) {
Write-Host "All services have started and are still running.";
return;
} else {
Throw "The following servies have started and then stopped: " + $serviceNames;
}
}
function Stop-WindowsServices {
param
(
[Parameter(Mandatory=$True,Position=0,HelpMessage="The names of the Windows Services to stop")]
[string[]]$serviceNames,
[Parameter(Mandatory=$False,Position=1,HelpMessage="The number of seconds to wait before checking the Services")]
[int]$SecondsToWait = 10
)
$ErrorActionPreference = "Stop";
$existingServices = Get-Service | Where-Object { $serviceNames -contains $_.Name };
if (-not $existingServices) {
Write-Host "There are no installed services to stop.";
return;
}
Write-Host "Stopping these services:";
$existingServices | % { Write-Host $_.Name };
$existingServices | Stop-Service;
$interval = $SecondsToWait;
if ($interval -gt 5) { $interval = 5; }
while ($SecondsToWait -gt 0) {
$stillRunning = ""
$existingServices | Get-Service | Where-Object { $_.Status -ne "Stopped" } | % { $stillRunning += " " + $_.Name };
if (-not $stillRunning) {
Write-Host "All services have stopped.";
Start-Sleep -s 2;
return;
}
$SecondsWaited = $interval;
Write-Host "Waiting for $SecondsWaited seconds...";
Start-Sleep -s $interval;
$SecondsToWait -= $interval;
$SecondsWaited += $interval;
}
$existingServices | Get-Service | Where-Object { $_.Status -ne "Stopped" } | % { $stillRunning += " " + $_.Name };
if (-not $stillRunning) {
Write-Host "All services have stopped.";
Start-Sleep -s 2;
return;
} else {
Throw "The following services are still running: " + $stillRunning;
}
}
function Set-WindowsServiceToDefaultBehavior {
param
(
[Parameter(Mandatory=$True,Position=0,HelpMessage="The name of the Windows Service")]
[string]$serviceName
)
Set-WindowsServiceToAutoStart -serviceName $serviceName
Set-WindowsServiceFailureRecovery -serviceName $serviceName
}
function Set-WindowsServiceToAutoStart {
param
(
[Parameter(Mandatory=$True,Position=0,HelpMessage="The name of the Windows Service")]
[string]$serviceName
)
$ErrorActionPreference = "Stop"
$service = Get-Service | Where-Object { $_.Name -eq "$serviceName" }
if (-not $service) {
throw "The Windows Service '$serviceName' does not exist"
}
Set-Service $serviceName -StartupType Automatic
}
function Set-WindowsServiceBinaryPath {
param
(
[Parameter(Mandatory=$True,Position=0,HelpMessage="The name of the Windows Service")]
[string]$serviceName,
[Parameter(Mandatory=$True,Position=1,HelpMessage="The full path to the service executable and any command line arguments")]
[string]$binaryPathName
)
$ErrorActionPreference = "Stop"
$service = Get-Service | Where-Object { $_.Name -eq "$serviceName" }
if (-not $service) {
throw "The Windows Service '$serviceName' does not exist"
}
if ($service.Status -ne "Stopped") {
Stop-Service $serviceName -Force
}
Write-Host "Setting the BinaryPath for '$serviceName' to '$binaryPathName'"
& "sc.exe" config $service.Name binPath= "$binaryPathName" | Write-Host
}
function Set-WindowsServiceFailureRecovery {
param
(
[Parameter(Mandatory=$True)]
[string] $serviceName,
[Parameter(Mandatory=$False)]
[int]
$ErrorFreePeriod=60,
[Parameter(Mandatory=$False)]
[int]
$DelayTimeInMilliseconds=5000
)
$service = Get-Service $serviceName -ErrorAction SilentlyContinue
if ($service) {
& "sc.exe" failure $serviceName reset= $ErrorFreePeriod actions= restart/$DelayTimeInMilliseconds | Write-Host
}
}
function Set-WindowsServiceDependencies {
param
(
[Parameter(Mandatory=$True)]
[string] $serviceName,
[string[]] $Dependencies
)
$service = Get-Service $serviceName -ErrorAction SilentlyContinue
$svcDependencies = ""
if ($Dependencies) {
$svcDependencies = [String]::Join("/", $Dependencies)
}
if ($svcDependencies) {
Write-Host "Configuring $serviceName to depend on $svcDependencies"
} else {
Write-Host "Ensuring $serviceName has no dependencies"
}
if ($service) {
& "sc.exe" config $serviceName depend= `"$svcDependencies`" | Write-Host
}
}
@dgtlrift
Copy link

What/where is SecurityLib.psm1?

@michaelnoonan
Copy link
Author

Great question! I have no idea. It would have been a module somewhere I wasn't clever enough to include in this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment