Skip to content

Instantly share code, notes, and snippets.

@neerajks77
Last active September 8, 2023 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neerajks77/77f20b5f61e37db47cf5863f4a8d6f0c to your computer and use it in GitHub Desktop.
Save neerajks77/77f20b5f61e37db47cf5863f4a8d6f0c to your computer and use it in GitHub Desktop.
This script intakes the parameters from incoming ServiceNow Requests for setting the email size limit
###############################################
#Author:Neeraj Kumar
#Description:This script intakes the parameters from incoming ServiceNow Requests for setting the email size limit
#Date: 01/19/2023
####. ###########################################
param(
[Parameter(Mandatory=$true)]
[string] $ServiceRequestNumber,
[Parameter(Mandatory=$true)]
[string] $RequestItemNumber,
[Parameter(Mandatory=$true)]
[string] $SCTaskNumber,
[Parameter(Mandatory=$true)]
[string] $UPIid,
[Parameter(Mandatory=$true)]
[string] $IssueWarningQuota,
[Parameter(Mandatory=$true)]
[string] $ProhibitSendQuota,
[Parameter(Mandatory=$true)]
[string] $ProhibitSendReceiveQuota
)
# Enforcing validation of code
Set-StrictMode -Version Latest
# use this function to set the mailbox size limit
function SetMailboxSize{
try{
if ($UPIid -match "^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")
{
#Connect to Exchange Online
Connect-ExchangeOnline -AppId $AppId -CertificateThumbPrint $ThumbPrint -Organization $Organization -ShowBanner:$false -ShowProgress:$false #-ErrorAction Stop
[string] $GetEmail = Get-Mailbox -Identity $UPIid -ErrorAction SilentlyContinue
if (($GetEmail -eq "") -or ($GetEmail -eq $null)){
$Global:Reason = "The email address provided does not exist. Please raise a new request with correct email id. The ticket has been marked as closed imcompleted."
$Global:Outputs = "The email address provided does not exist. Please raise a new request with correct email id. The ticket has been marked as closed imcompleted."
$Global:Provisioningstate = "Failed"
$Global:RequestState = "Closed_Incomplete"
}
else{
Set-Mailbox -Identity $UPIid -IssueWarningQuota $IssueWarningQuota -ProhibitSendQuota $ProhibitSendQuota -ProhibitSendReceiveQuota $ProhibitSendReceiveQuota
$Global:Outputs = "Mailbox size has been changed for Email ID - " + $UPIid + ". Request has been Completed."
$Global:Reason = "Mailbox size has been changed for Email ID - " + $UPIid + ". Request has been Completed."
$Global:Provisioningstate = "Succeeded"
$Global:RequestState = "Closed_Complete"
}
}
else{
$Global:Reason = "The email address provided is in the incorrect format. Please raise a new request with correct email id. The ticket has been marked as closed imcompleted."
$Global:Outputs = "The email address provided is in the incorrect format. Please raise a new request with correct email id. The ticket has been marked as closed imcompleted."
$Global:Provisioningstate = "Failed"
$Global:RequestState = "Closed_Incomplete"
}
}
Catch{
$Global:Provisioningstate = "Failed"
$Global:RequestState = "Closed_Incomplete"
$Global:Reason = $_.Exception.ToString()
$Global:Outputs = $_.Exception.ToString()
$error[0] | Format-List -Force
}
# GetSNOWAuthorizationToken
# UpdateRITMPostProcessingtheRequest
UpdateSNOWwithTaskStatus
}
function UpdateSNOWwithTaskStatus{
try{
$SNOWSessionURL = $Global:SNOWURL
$Type = "application/json"
# Set headers
$GlobalHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$GlobalHeaders.Add('Accept', $Type)
$GlobalHeaders.Add('Content-Type', $Type)
# Build & set authentication header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username, $Password)))
$GlobalHeaders.Add('Authorization', ('Basic {0}' -f $base64AuthInfo))
$body = @{
"closed_at"= $currentUTCtime
"DeploymentName" = $SCTaskNumber
"ritm" = $RequestItemNumber
"Provisioningstate" = $Global:Provisioningstate
"Outputs" = $Global:Outputs
"Reason" = $Global:Reason
"request_state" = $Global:RequestState
"close_notes"= $Global:LogMessage
"comments_and_work_notes" = $Global:LogMessage
"comments" = $Global:LogMessage
}
$bodyJSON = $body | ConvertTo-Json
Write-Host $bodyJSON
Try
{
write $bodyJSON
$result = Invoke-RestMethod -Method POST -Uri $SNOWSessionURL -Body $bodyJSON -Headers $GlobalHeaders
if(!$result)
{
throw "Error Occured"
}
else
{
Write-Output $result
Write-Output "Succeeded"
}
}
Catch
{
$_.Exception.ToString()
$error[0] | Format-List -Force
}
}
catch{
$_.Exception.ToString()
$error[0] | Format-List -Force
}
}
##################################################
#Get the SericeNow Authorization token to write the updates back to SericeNow
##################################################
<#function GetSNOWAuthorizationToken {
$SNOWSessionURL = $Global:SNOWURL + "oauth_token.do"
$AuthBody = [System.Text.Encoding]::UTF8.GetBytes("grant_type=password&username="+$Username+"&password="+$Password+"&client_id="+$ClientID+"&client_secret="+$ClientSecret)
$Type = "application/json"
# Authenticating with API
Try
{
$SNOWSessionResponse = Invoke-RestMethod -Method POST -Uri $SNOWSessionURL -Body $AuthBody -ContentType "application/x-www-form-urlencoded"
}
Catch
{
$_.Exception.ToString()
$error[0] | Format-List -Force
}
# Extracting the token from the JSON response
$Global:SNOWSessionHeader = @{'Authorization' = "Bearer $($SNOWSessionResponse.access_token)"}
}
function UpdateRITMPostProcessingtheRequest{
if($Global:MailboxSizeSet)
{
try{
$method = 'PUT'
$Type = "application/json"
$UpdateRITMURL= $Global:SNOWURL + "api/now/table/sc_req_item/"+ $RequestItemNumber
$UpdateReqURL = $Global:SNOWURL + "api/now/table/sc_request/"+ $ServiceRequestNumber
$body = @{
"closed_at"= $currentUTCtime
"state" = $Global:State
"request_state" = $Global:RequestState
"close_notes"= $Global:LogMessage
"comments_and_work_notes" = $Global:LogMessage
"comments" = $Global:LogMessage
}
$bodyJSON = $body | ConvertTo-Json
$utf8Bytes = [System.Text.Encoding]::UTf8.GetBytes($bodyJSON)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-RestMethod -Headers $Global:SNOWSessionHeader -Method $method -Uri $UpdateRITMURL -Body $bodyJSON -ContentType $Type
Invoke-RestMethod -Headers $Global:SNOWSessionHeader -Method $method -Uri $UpdateReqURL -Body $bodyJSON -ContentType $Type
}
catch{
Write-Host $_.Exception.ToString()
$error[0] | Format-List -Force
}
}
}#>
# Initiate the variable here
##################################
[Boolean] $EmailExists = $true
[Boolean] $Global:MailboxSizeSet = $false
[string] $Organization = Get-AutomationVariable -Name 'Organization'
[string] $Global:SNOWURL = Get-AutomationVariable -Name 'SNOWURL'
[string] $Username = Get-AutomationVariable -Name 'SNOWUser'
[string] $Password = Get-AutomationVariable -Name 'SNOWUserpassword'
[string] $Thumbprint = Get-AutomationVariable -Name 'Cert_Thumbprint'
[string] $AppId = Get-AutomationVariable -Name 'AppID'
[string] $Global:Provisioningstate = ''
[string] $Global:Reason = ' '
[string] $Global:Outputs = ' '
[string] $Global:LogMessage = ''
[string] $Global:State = "2"
[string] $Global:RequestState = "2"
$date = Get-Date
[System.DateTime] $currentUTCtime = $date.ToUniversalTime()
#####################################
# call the function to provision email id
#####################################
SetMailboxSize
################################################################# End Main Section ############################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment