Skip to content

Instantly share code, notes, and snippets.

@martencassel
Created October 23, 2019 09:48
Show Gist options
  • Save martencassel/ad943c9dbd1740a2614e73f3236e67fb to your computer and use it in GitHub Desktop.
Save martencassel/ad943c9dbd1740a2614e73f3236e67fb to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Configure fileshare for UiPath Orchestrator Nuget
.Description
Configure fileshare for UiPath Orchestrator Nuget
.PARAMETER shareUsername
String. The local username
.PARAMETER sharePassword
String. The local usernames password
.PARAMETER folderName
String. The folder name
.INPUTS
Parameters above.
.OUTPUTS
None
.Example
powershell.exe -ExecutionPolicy Bypass ".\Install-UiPathNugetFileShare.ps1" -shareUsername "web" -sharePassword "P@ssw0rdP@ssw0rd" -folderName "nuget"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $shareUsername,
[Parameter(Mandatory = $true)]
[string] $sharePassword,
[Parameter(Mandatory = $true)]
[string] $folderName
)
# 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-UiPathNugetFileShare.ps1.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
function Main {
try {
Start-Transcript -Path "$sLogPath\Install-UiPathNugetFileShare-Transcript.ps1.txt" -Append
# Setup temp dir in %appdata%\Local\Temp
$tempDirectory = (Join-Path 'C:\temp\' "UiPathNugetFileShare-$(Get-Date -f "yyyyMMddhhmmssfff")")
New-Item -ItemType Directory -Path $tempDirectory -Force
Install-WindowsFeature -Name FS-FileServer -IncludeAllSubFeature -IncludeManagementTools
$Secure_String_Pwd = ConvertTo-SecureString $sharePassword -AsPlainText -Force
New-LocalUser -Name $shareUsername -Password $Secure_String_Pwd
mkdir c:\nugetpackages
New-SmbShare -Name nugetpackages -Path 'C:\nugetpackages' -FullAccess 'web','Everyone'
}
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