Created
October 23, 2019 08:36
-
-
Save martencassel/d6c349c59f1c56de92829354c93c2f3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Configure MSSQL server for a UiPath Orchestrator database | |
.Description | |
Install UiPath Orchestrator database and web application database user | |
.PARAMETER databaseName | |
String. The database name. | |
.PARAMETER databaseUserName | |
String. The database username. | |
.PARAMETER databaseUserPassword | |
String. The database user password. | |
.INPUTS | |
Parameters above. | |
.OUTPUTS | |
None | |
.Example | |
powershell.exe -ExecutionPolicy Bypass ".\Install-UiPathDatabase.ps1" -databaseName "uipath" -databaseUserName "uipath_sql" -databaseUserPassword "P@ssw0rdP@ssw0rd" | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory = $true)] | |
[string] $databaseName, | |
[Parameter(Mandatory = $true)] | |
[string] $databaseUserName, | |
[Parameter(Mandatory = $true)] | |
[string] $databaseUserPassword | |
) | |
# Set Error Action to Silently Continue | |
$ErrorActionPreference = "SilentlyContinue" | |
# Script Version | |
$sScriptVersion = "1.0" | |
# Debug mode; $true - enabled ; $false - disabled | |
$sDebug = $true | |
# Log File Info | |
$sLogPath = "C:\temp\log" | |
$sLogName = "Install-UiPathDatabase.ps1.log" | |
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName | |
function Main { | |
try { | |
Start-Transcript -Path "$sLogPath\Install-UiPathDatabase-Transcript.ps1.txt" -Append | |
# Setup temp dir in %appdata%\Local\Temp | |
$tempDirectory = (Join-Path 'C:\temp\' "UiPathDatabase-$(Get-Date -f "yyyyMMddhhmmssfff")") | |
New-Item -ItemType Directory -Path $tempDirectory -Force | |
net stop mssqlserver | |
net start mssqlserver | |
$userName = $databaseUserName | |
$dbName = $databaseName | |
$password = $databaseUserPassword | |
$sql = @" | |
USE [master] | |
GO | |
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2 | |
GO | |
create database [$dbName]; | |
GO | |
CREATE LOGIN [$userName] WITH PASSWORD=N'$password', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=ON | |
GO | |
USE [$dbName] | |
GO | |
CREATE USER [$userName] FOR LOGIN [$userName] | |
GO | |
ALTER ROLE [db_datareader] ADD MEMBER [$userName] | |
GO | |
ALTER ROLE [db_datawriter] ADD MEMBER [$userName] | |
GO | |
ALTER ROLE [db_ddladmin] ADD MEMBER [$userName] | |
GO | |
GRANT EXECUTE TO [$userName] | |
GO | |
"@ | |
invoke-sqlcmd $sql | |
net stop mssqlserver | |
net start mssqlserver | |
} | |
catch { | |
Log-Error -LogPath $sLogFile -ErrorDesc "$($_.exception.message) on $(Get-Date)" -ExitGracefully $True | |
} | |
} | |
<# | |
.SYNOPSIS | |
Creates log file | |
.DESCRIPTION | |
Creates log file with path and name that is passed. Checks if log file exists, and if it does deletes it and creates a new one. | |
Once created, writes initial logging data | |
.PARAMETER LogPath | |
Mandatory. Path of where log is to be created. Example: C:\Windows\Temp | |
.PARAMETER LogName | |
Mandatory. Name of log file to be created. Example: Test_Script.log | |
.PARAMETER ScriptVersion | |
Mandatory. Version of the running script which will be written in the log. Example: 1.5 | |
.INPUTS | |
Parameters above | |
.OUTPUTS | |
Log file created | |
#> | |
function Log-Start { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$LogPath, | |
[Parameter(Mandatory = $true)] | |
[string]$LogName, | |
[Parameter(Mandatory = $true)] | |
[string]$ScriptVersion | |
) | |
Process { | |
$sFullPath = $LogPath + "\" + $LogName | |
# Check if file exists and delete if it does | |
if ((Test-Path -Path $sFullPath)) { | |
Remove-Item -Path $sFullPath -Force | |
} | |
# Create file and start logging | |
New-Item -Path $LogPath -Value $LogName -ItemType File | |
Add-Content -Path $sFullPath -Value "***************************************************************************************************" | |
Add-Content -Path $sFullPath -Value "Started processing at [$([DateTime]::Now)]." | |
Add-Content -Path $sFullPath -Value "***************************************************************************************************" | |
Add-Content -Path $sFullPath -Value "" | |
Add-Content -Path $sFullPath -Value "Running script version [$ScriptVersion]." | |
Add-Content -Path $sFullPath -Value "" | |
Add-Content -Path $sFullPath -Value "Running with debug mode [$sDebug]." | |
Add-Content -Path $sFullPath -Value "" | |
Add-Content -Path $sFullPath -Value "***************************************************************************************************" | |
Add-Content -Path $sFullPath -Value "" | |
# Write to screen for debug mode | |
Write-Debug "***************************************************************************************************" | |
Write-Debug "Started processing at [$([DateTime]::Now)]." | |
Write-Debug "***************************************************************************************************" | |
Write-Debug "" | |
Write-Debug "Running script version [$ScriptVersion]." | |
Write-Debug "" | |
Write-Debug "Running with debug mode [$sDebug]." | |
Write-Debug "" | |
Write-Debug "***************************************************************************************************" | |
Write-Debug "" | |
} | |
} | |
<# | |
.SYNOPSIS | |
Writes an error to a log file | |
.DESCRIPTION | |
Writes the passed error to a new line at the end of the specified log file | |
.PARAMETER LogPath | |
Mandatory. Full path of the log file you want to write to. Example: C:\Windows\Temp\Test_Script.log | |
.PARAMETER ErrorDesc | |
Mandatory. The description of the error you want to pass (use $_.Exception) | |
.PARAMETER ExitGracefully | |
Mandatory. Boolean. If set to True, runs Log-Finish and then exits script | |
.INPUTS | |
Parameters above | |
.OUTPUTS | |
None | |
#> | |
function Log-Error { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$LogPath, | |
[Parameter(Mandatory = $true)] | |
[string]$ErrorDesc, | |
[Parameter(Mandatory = $true)] | |
[boolean]$ExitGracefully | |
) | |
Process { | |
Add-Content -Path $LogPath -Value "Error: An error has occurred [$ErrorDesc]." | |
# Write to screen for debug mode | |
Write-Debug "Error: An error has occurred [$ErrorDesc]." | |
# If $ExitGracefully = True then run Log-Finish and exit script | |
if ($ExitGracefully -eq $True) { | |
Log-Finish -LogPath $LogPath | |
Break | |
} | |
} | |
} | |
<# | |
.SYNOPSIS | |
Write closing logging data & exit | |
.DESCRIPTION | |
Writes finishing logging data to specified log and then exits the calling script | |
.PARAMETER LogPath | |
Mandatory. Full path of the log file you want to write finishing data to. Example: C:\Windows\Temp\Script.log | |
.PARAMETER NoExit | |
Optional. If this is set to True, then the function will not exit the calling script, so that further execution can occur | |
.INPUTS | |
Parameters above | |
.OUTPUTS | |
None | |
#> | |
function Log-Finish { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$LogPath, | |
[Parameter(Mandatory = $false)] | |
[string]$NoExit | |
) | |
Process { | |
Add-Content -Path $LogPath -Value "" | |
Add-Content -Path $LogPath -Value "***************************************************************************************************" | |
Add-Content -Path $LogPath -Value "Finished processing at [$([DateTime]::Now)]." | |
Add-Content -Path $LogPath -Value "***************************************************************************************************" | |
Add-Content -Path $LogPath -Value "" | |
# Write to screen for debug mode | |
Write-Debug "" | |
Write-Debug "***************************************************************************************************" | |
Write-Debug "Finished processing at [$([DateTime]::Now)]." | |
Write-Debug "***************************************************************************************************" | |
Write-Debug "" | |
# Exit calling script if NoExit has not been specified or is set to False | |
if (!($NoExit) -or ($NoExit -eq $False)) { | |
Exit | |
} | |
} | |
} | |
Log-Start -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion | |
Main | |
Log-Finish -LogPath $sLogFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment