Skip to content

Instantly share code, notes, and snippets.

@benwillkommen
Last active February 23, 2016 17:29
Show Gist options
  • Save benwillkommen/7dd2a7a3a42315d79c4c to your computer and use it in GitHub Desktop.
Save benwillkommen/7dd2a7a3a42315d79c4c to your computer and use it in GitHub Desktop.
ServiceNow lack-of-API hack script ¯\_(ツ)_/¯
function New-LogicalisMonitoringSuppression{
[cmdletbinding()]
Param(
[String[]]
[Parameter(Mandatory=$true)]
$Server,
$ShowBrowser = $False
)
begin{}
process{
# Create instance of InternetExplorer
$ie = new-object -com internetexplorer.application
$ie.visible = $ShowBrowser
$activityMessage = "Creating Logicalis suppression for $Server..."
Write-Progress -Activity $activityMessage -PercentComplete 0 -CurrentOperation "Requesting login page..."
$ie.navigate2("https://optimal.service-now.com/optimal/request_corporate_services.do")
while($ie.busy) {start-sleep 1}
Write-Progress -Activity $activityMessage -PercentComplete 25 -CurrentOperation "Logging in..."
$credentials = $env:LogicalisServiceNowCredentials | ConvertFrom-Json
$username = $credentials.username
$password = $credentials.password
$ie.Document.parentWindow.execScript("jQuery('#user_name').val('$username');", "javascript")
$ie.Document.parentWindow.execScript("jQuery('#user_password').val('$password');", "javascript")
$ie.Document.parentWindow.execScript("jQuery('#sysverb_login').click();", "javascript")
while($ie.busy) {start-sleep 1}
Write-Progress -Activity $activityMessage -PercentComplete 50 -CurrentOperation "Requesting monitoring suppression page..."
#go to monitoring suppression page, without the iframe garbage
$ie.navigate2("https://optimal.service-now.com/optimal/com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=0c3213e10f533500751c0eece1050ed8")
while($ie.busy) {start-sleep 1}
Write-Progress -Activity $activityMessage -PercentComplete 75 -CurrentOperation "Submitting monitoring suppression form..."
#input the times and servers
#convert current time to Eastern, add 15 minutes
#TODO: make start and end time optional parameters in the future
$start = [System.TimeZoneInfo]::ConvertTimeFromUtc((get-date).ToUniversalTime(), [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")).AddSeconds(15)
$end= $start.AddMinutes(15)
$startString = "{0:yyyy}-{0:MM}-{0:dd} {0:HH}:{0:mm}:{0:ss}" -f $start
$endString = "{0:yyyy}-{0:MM}-{0:dd} {0:HH}:{0:mm}:{0:ss}" -f $end
$inputStartTimeCommand = @"
jQuery(jQuery('.cat_item_option[type=text]')[0]).val('$startString')
"@
$inputEndTimeCommand = @"
jQuery(jQuery('.cat_item_option[type=text]')[1]).val('$endString')
"@
$selectServerCommand = @"
jQuery('option').filter(function(i,e){
return jQuery(e).html() === '$server';
}).each(function(i,e){
console.log(jQuery(e).val());
console.log(jQuery("#ci_list_select_0").val());
jQuery("#ci_list_select_0").val(jQuery(e).val());
console.log(jQuery("#ci_list_select_0").val());
});
"@
$addServerCommand = @"
jQuery("#ci_list_select_0_add_remove_container a").first().click()
"@
$orderNowCommand = @"
jQuery("#order_now").click()
"@
$ie.Document.parentWindow.execScript($inputStartTimeCommand, "javascript")
$ie.Document.parentWindow.execScript($inputEndTimeCommand, "javascript")
Start-Sleep 5 # seems to need a short delay for the select box interactions :(
$ie.Document.parentWindow.execScript($selectServerCommand, "javascript")
Start-Sleep 5
$ie.Document.parentWindow.execScript($addServerCommand, "javascript")
$ie.Document.parentWindow.execScript($orderNowCommand, "javascript")
while($ie.busy) {start-sleep 1}
Write-Progress -Activity $activityMessage -PercentComplete 100 -CurrentOperation "Logging out..."
#go back to a page with a header so we can log out
$ie.navigate2("https://optimal.service-now.com/optimal/request_corporate_services.do")
while($ie.busy) {start-sleep 1}
#logout
$ie.Document.parentWindow.execScript("jQuery('.cms_header_login_link').click();", "javascript")
while($ie.busy) {start-sleep 1}
}
end{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment