Skip to content

Commit

Permalink
version 1.1.1
Browse files Browse the repository at this point in the history
Updated to support bulk insert
  • Loading branch information
tyconsulting committed Dec 4, 2016
1 parent 339b502 commit ce26c82
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 26 deletions.
Binary file modified OMSDataInjection/OMSDataInjection.psd1
Binary file not shown.
66 changes: 43 additions & 23 deletions OMSDataInjection/OMSDataInjection.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Function New-OMSDataInjection
[Parameter(ParameterSetName = 'InjectByPSObjectWithIndividualParameters', Mandatory = $true,HelpMessage = 'Please specify the PSObject containing OMS data')]
[Parameter(ParameterSetName = 'InjectByPSObjectWithConnection', Mandatory = $true,HelpMessage = 'Please specify the PSObject containing OMS data')]
[ValidateNotNullOrEmpty()]
[PSObject]$OMSDataObject,
[PSObject[]]$OMSDataObject,

[Parameter(ParameterSetName = 'InjectByJSONStringWithConnection', Mandatory = $true,HelpMessage = 'Please specify the JSON format string containing OMS data')]
[Parameter(ParameterSetName = 'InjectByJSONStringWithIndividualParameters', Mandatory = $true,HelpMessage = 'Please specify the JSON format string containing OMS data')]
Expand All @@ -42,6 +42,9 @@ Function New-OMSDataInjection

)

#Maximum HTTP request body size = 30mb
$RequestBodyLimitMB = 30
$RequestBodyLimitBytes = $RequestBodyLimitMB * 1mb
Write-Verbose -Message 'Validate JSON format if JSON format string is specified'
If ($PSBoundParameters.ContainsKey('OMSDataJSON'))
{
Expand All @@ -55,33 +58,38 @@ Function New-OMSDataInjection
Write-Verbose -Message 'Valid JSON data provided.'

Write-Verbose -Message 'Validate If the PS object or the JSON input input contains the Time Stamp field'
If ($OMSDataObject.$UTCTimeStampField -eq $null)
For ($i = 0; $i -lt $OMSDataObject.count; $i++)
{
If ($OMSDataJSON -eq $Null)
If ($OMSDataObject[$i].$UTCTimeStampField -eq $null)
{
Throw ("The input object `$OMSDataObject does not contain a property for the specified Time Stamp Field '{0}'." -f $UTCTimeStampField)
} else {
Throw ("The input JSON string `$OMSDataJSON does not contain a property for the specified Time Stamp Field '{0}'." -f $UTCTimeStampField)
}
If ($OMSDataJSON -eq $Null)
{
Throw ("The input object `$OMSDataObject does not contain a property for the specified Time Stamp Field '{0}'." -f $UTCTimeStampField)
} else {
$IndividualJSON = ConvertTO-JSON $OMSDataObject[$i]
Throw ("The input JSON string `$IndividualJSON does not contain a property for the specified Time Stamp Field '{0}'." -f $UTCTimeStampField)
}

Exit -1
}
Write-Verbose -Message ("'{0}' is contained in the input JSON/PSObject parameter." -f $UTCTimeStampField)
Write-Verbose -Message ("'{0}' value: '{1}'." -f $UTCTimeStampField, $OMSDataObject.$UTCTimeStampField)
If ($OMSDataObject.$UTCTimeStampField.GetType().FullName -ieq 'system.datetime')
{
$OMSDataObject.$UTCTimeStampField = $OMSDataObject.$UTCTimeStampField.ToString('yyyy-MM-ddTHH:mm:ss.fffZ')
} else {
#Validate if the Time stamp specified contains a valid datetime value
Try
{
$timestamp = ([datetime]::Parse($OMSDataObject.$UTCTimeStampField)).ToString('yyyy-MM-ddTHH:mm:ss.fffZ')
$OMSDataObject.$UTCTimeStampField = $timestamp
} Catch {
Throw ('The {0} does not contain valid date time' -f $UTCTimeStampField)
Exit -1
}
#Write-Verbose -Message ("'{0}' is contained in the input JSON/PSObject parameter." -f $UTCTimeStampField)
#Write-Verbose -Message ("'{0}' value: '{1}'." -f $UTCTimeStampField, $OMSDataObject[$i].$UTCTimeStampField)
If ($OMSDataObject[$i].$UTCTimeStampField.GetType().FullName -ieq 'system.datetime')
{
$OMSDataObject[$i].$UTCTimeStampField = $OMSDataObject[$i].$UTCTimeStampField.ToString('yyyy-MM-ddTHH:mm:ss.fffZ')
} else {
#Validate if the Time stamp specified contains a valid datetime value
Try
{
$timestamp = ([datetime]::Parse($OMSDataObject[$i].$UTCTimeStampField)).ToString('yyyy-MM-ddTHH:mm:ss.fffZ')
$OMSDataObject[$i].$UTCTimeStampField = $timestamp
} Catch {
Throw ('The {0} does not contain valid date time' -f $UTCTimeStampField)
Exit -1
}
}
}


#Inject activity into OMS
If ($PSBoundParameters.ContainsKey('OMSWorkSpaceId'))
Expand All @@ -94,6 +102,15 @@ Function New-OMSDataInjection
}

$OMSLogBody = ConvertTo-Json -InputObject $OMSDataObject

#Check request body size before injecting
$RequestBodySize = [System.Text.ASCIIEncoding]::UTF8.GetByteCount($OMSLogBody)
If ($RequestBodySize -gt $RequestBodyLimitBytes)
{
Throw "The request body is too big. Maximum size is 30 MB, current size: $($RequestBodySize/1mb) MB. Please split logs into multiple smaller chunks."
Exit -1
}
Write-Verbose "HTTP POST request body size: $RequestBodySize bytes."
$LogType = $LogType
Publish-OMSData -OMSConnection $OMSConnection -body $OMSLogBody -LogType $LogType
}
Expand Down Expand Up @@ -186,13 +203,16 @@ Function Publish-OMSData

}
}

if ($response.StatusCode -eq 202)
{
Write-Verbose -Message 'OMS data injection accepted!'
$InjectSuccessful = $true
} else {
Write-Error $ErrorMessage
$InjectSuccessful = $false
}
$InjectSuccessful
}

#endregion
34 changes: 31 additions & 3 deletions OMSDataInjection/OMSDataInjection.psm1-Help.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<helpItems xmlns="http://msh" schema="maml">
<!-- Edited with: SAPIEN PowerShell HelpWriter 2016 v1.1.20-->
<!--Edited with: SAPIEN PowerShell HelpWriter 2016 v1.1.20-->
<!-- Generated by: SAPIEN PowerShell HelpWriter 2016 v1.1.20-->
<!--
Module: OMSDataInjection
Expand All @@ -12,8 +12,7 @@
<command:details>
<command:name>New-OMSDataInjection</command:name>
<maml:description>
<maml:para>
</maml:para>
<maml:para/>
</maml:description>
<maml:copyright>
<maml:para/>
Expand Down Expand Up @@ -363,6 +362,35 @@ $InjectData = New-OMSDataInjection -OMSConnection $OMSConnection -LogType &apos;
<maml:para>Injecting data using JSON formatted string by specifying the OMSWorkspace Azure Automation / SMA connection object (to be used in a runbook)</maml:para>
</dev:remarks>
</command:example>
<command:example>
<maml:title>-------------------------- EXAMPLE 3 --------------------------</maml:title>
<maml:introduction>
<maml:para>PS C:\&gt;</maml:para>
</maml:introduction>
<dev:code>$PrimaryKey = Read-Host -Prompt &apos;Enter the primary key&apos;
$arrOMSDataObjects = @()
$ObjProperties1 = @{
Computer = $env:COMPUTERNAME
Username = $env:USERNAME
Message = &apos;This is test message #1 injected by the OMSDataInjection module. Input data type: PSObject&apos;
LogTime = [Datetime]::UtcNow
}
$OMSDataObject1 = New-Object -TypeName PSObject -Property $ObjProperties1
$arrOMSDataObjects += $OMSDataObject1

$ObjProperties2 = @{
Computer = $env:COMPUTERNAME
Username = $env:USERNAME
Message = &apos;This is test message #2 injected by the OMSDataInjection module. Input data type: PSObject&apos;
LogTime = [Datetime]::UtcNow
}
$OMSDataObject2 = New-Object -TypeName PSObject -Property $ObjProperties2
$arrOMSDataObjects += $OMSDataObject2
$InjectData = New-OMSDataInjection -OMSWorkSpaceId &apos;8fb61d08-12ac-400a-a45b-0e607994779f&apos; -PrimaryKey $PrimaryKey -LogType &apos;OMSTestData&apos; -UTCTimeStampField &apos;LogTime&apos; -OMSDataObject $arrOMSDataObjects</dev:code>
<dev:remarks>
<maml:para>Injecting multiple log entries using an array of PS objects by specifying the OMS workspace Id and primary key</maml:para>
</dev:remarks>
</command:example>
</command:examples>
</command:command>
<command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10">
Expand Down

0 comments on commit ce26c82

Please sign in to comment.