From f4905fec680fc7027f642a17b7adb6db64ef5ef2 Mon Sep 17 00:00:00 2001 From: David Yoder Date: Tue, 25 Jun 2024 16:06:37 -0400 Subject: [PATCH] added a bunch of functions --- Tools.ps1 | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/Tools.ps1 b/Tools.ps1 index b28c68d..76cbcc6 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -12,8 +12,9 @@ # Define the root path for the MSP in the Windows Registry [string]$Global:MSPRegistryRoot = "HKEY_LOCAL_MACHINE\SOFTWARE\${MSPName}" -# Create a global variable to keep a ticket number if it needs to be used by any scripts +# Create a global variable to keep a ticket and billing related variables if they are needed later $Global:TicketNumber = $null +$Global:TimeAttributedToUser = "" # Setup the MSP management directories Function Setup-MSPDirectories { @@ -1017,9 +1018,10 @@ Function Restart-EndpointOnUptime ([timespan]$MaxUptime) { Catch { Write-Error $_.Exception.Message } } +# Function to download and install local tools used by scripts Function Install-LocalTools () { param( - [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Force + [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Force=$false ) $ToolURLs = @( @@ -1040,24 +1042,39 @@ Function Install-LocalTools () { } } +Function Source-PSScript ([string]$ScriptName) { + If ( $ScriptName.StartsWith('http://') -or $ScriptName.StartsWith('https://') ) { + $URL = $ScriptName + } Else { + $URL = "https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/${ScriptName}.ps1" + } + $ScriptFile = Download-File -URL $URL + Write-Output "Sourcing ${ScriptFile}" + . $ScriptFile + If ( Test-Path $ScriptFile ) { Write-Output "Removing ${ScriptFile}" ; Remove-Item $ScriptFile -Force -ErrorAction SilentlyContinue } +} + +# Function to import the Syncro Module Function Import-RMMModule () { Try { Import-Module $env:SyncroModule -WarningAction SilentlyContinue -ErrorAction Stop } Catch { Write-Error $_.Exception.Message ; Return } } +# Function to create a new ticket Function New-RMMTicket () { param( [Parameter(ValueFromPipeline=$false,Mandatory=$true)][string]$Subject, [Parameter(ValueFromPipeline=$false,Mandatory=$false)][string]$IssueType="Remote Support", [Parameter(ValueFromPipeline=$false,Mandatory=$true)][string]$InitialIssue ) - Import-RMMModule + If ( !(Get-Command Create-Syncro-Ticket -ErrorAction SilentlyContinue) ) { Import-RMMModule} Try { $Ticket = Create-Syncro-Ticket -Subject $Subject -IssueType $IssueType -Status "New" } Catch { Write-Error $_.Exception.Message ; Return } $Global:TicketNumber = $Ticket.ticket.id New-TicketComment -TicketID $Global:TicketNumber -Subject "Issue" -Comment $InitialIssue -Hidden -DoNotEmail } +# Function to create a new comment on a ticket Function New-TicketComment () { param( [Parameter(ValueFromPipeline=$false,Mandatory=$false)]$TicketID=$null, @@ -1066,12 +1083,37 @@ Function New-TicketComment () { [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Hidden=$false, [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$DoNotEmail=$false ) + If ( !(Get-Command Create-Syncro-Ticket-Comment -ErrorAction SilentlyContinue) ) { Import-RMMModule} If ( ($null -eq $TicketID) -and ($null -eq $Global:TicketNumber) ) { Return } If ( ($null -eq $TicketID) -and ($null -ne $Global:TicketNumber) ) { $TicketID = $Global:TicketNumber } Try { Create-Syncro-Ticket-Comment -TicketIdOrNumber $TicketID -Subject $Subject -Body $Comment -Hidden $Hidden -DoNotEmail $DoNotEmail } Catch { Write-Error $_.Exception.Message ; Return } } +# Function to create a new billable time entry on a ticket +Function New-TicketTimerEntry () { + param( + [Parameter(ValueFromPipeline=$false,Mandatory=$false)]$TicketID=$null, + [Parameter(ValueFromPipeline=$false,Mandatory=$false)][int]$BillableMinutes=15, + [Parameter(ValueFromPipeline=$true,Mandatory=$true)][string]$Comment, + [Parameter(ValueFromPipeline=$false,Mandatory=$false)][string]$BillableUser=$Global:TimeAttributedToUser, + [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$ChargeLater=$false + ) + If ( $BillableUser -eq "" ) { Write-Error "Billable user not specified" ; Return } + If ( !(Get-Command Create-Syncro-Ticket-TimerEntry -ErrorAction SilentlyContinue) ) { Import-RMMModule} + If ( $ChargeLater -eq $true ) { $ChargeTime = "false" } Else { $ChargeTime = "true" } + $StartAt = (Get-Date).AddMinutes(-$BillableMinutes).ToString("o") + Create-Syncro-Ticket-TimerEntry -TicketIdOrNumber $TicketID -StartTime $StartAt -DurationMinutes $BillableMinutes -Notes $Comment -UserIdOrEmail $BillableUser -ChargeTime $ChargeTime +} + +# Initial setup of the MSP management directories and log file Setup-MSPDirectories Setup-LogFile + +# Install the local tools Install-LocalTools + +# Check the RMM runtime variable to see if a billable user was specified, if so then set the $TimeAttributedToUser +If ( $TimeAttributedToUser.Trim().Length -ge 6 ) { $Global:TimeAttributedToUser = $TimeAttributedToUser.Trim().ToLower() } +# Check the RMM runtime variable to see if a ticket number was specified, if so then set the $TicketID +If ( $TicketNumber.Trim().Length -gt 1 ) { $Global:TicketNumber = $TicketNumber.Trim() } \ No newline at end of file