-
Notifications
You must be signed in to change notification settings - Fork 26
/
uploadToOMS.ps1
139 lines (120 loc) · 4.7 KB
/
uploadToOMS.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $CloudName
)
Start-Transcript -Path "C:\AZSAdminOMSInt\uploadtoOMS_$CloudName.log"
Set-ExecutionPolicy Bypass -Force
Install-Module -Name OMSIngestionAPI -Force
Install-Module -Name AzureRM.OperationalInsights -Force
Import-Module -Name Azs.Infrastructureinsights.Admin -Force
Import-Module -Name Azs.Update.Admin -Force
Import-Module -Name Azs.Fabric.Admin -Force
#OMS Authentication Variables
$info = Get-Content -Raw -Path "C:\AZSAdminOMSInt\info_$CloudName.txt" | ConvertFrom-Json
$OMSWorkspaceId = $info.OmsWorkspaceID
$OMSSharedKey = $info.OmsSharedKey
#Cloud2 Authentication details
$Authtype = $info.ParameterSet
$Location2 = $info.Region
$cloudName2 = $info.CloudName
$State2 = "active"
Switch($Authtype)
{
#Set to AdminAccount or not set(old info file)
{($_ -eq "AdminAccount") -or ($_ -eq $null)}{
$UserName2= $info.AzureStackAdminUsername
$Password2= Get-Content "C:\AZSAdminOMSInt\azspassword_$CloudName.txt"| ConvertTo-SecureString
$Credential2=New-Object PSCredential($UserName2,$Password2)
$TenantId2 = $info.TenantId
}
#Using CertSPN
"CertSPN"{
$CertificateThumbprint2 = $info.CertificateThumbprint
$ApplicationId2 = $info.ApplicationId
$TenantId2 = $info.TenantId
}
}
$deploymentGuid = $info.DeploymentGuid
$api = "adminmanagement"
$AzureStackDomain = $info.Fqdn
$AzureStackAdminEndPoint = 'https://{0}.{1}.{2}' -f $api, $Location2, $AzureStackDomain
$AzSOEM = $info.Oem
#################################################################################
# OPERATIONAL DATA
#
#################################################################################
##############################################################################################################
# Get Data via PS for Cloud 2
Add-AzureRMEnvironment -Name $cloudName2 -ArmEndpoint $AzureStackAdminEndPoint
$result = $null
Switch($Authtype)
{
#Set to AdminAccount or not set(old info file)
{($_ -eq "AdminAccount") -or ($_ -eq $null)}{
if($TenantId2){#Use TenantID if one was provided
$result = Add-AzureRmAccount -EnvironmentName $cloudName2 -Credential $Credential2 -Tenant $TenantId2
}
else{
$result = Add-AzureRmAccount -EnvironmentName $cloudName2 -Credential $Credential2
}
}
#Using CertSPN
"CertSPN"{
$result = Add-AzureRmAccount -Environment $cloudName2 -ServicePrincipal -CertificateThumbprint $CertificateThumbprint2 -ApplicationId $ApplicationId2 -TenantId $TenantId2
}
}
if(!$result)
{
#Add-AzureRMAccount failed
Write-Warning "No Subscription info returned by Add-AzureRMAccount"
Return
}
#################################################################################
# USAGE DATA
#
#################################################################################
$usageSummary = Get-Content -Raw -Path "UsageSummary_$cloudName2.json" | ConvertFrom-Json
$logType = "Usage"
$deploymentGuid = $info.DeploymentGuid
#$subTenantHash = @{}
# $result | ForEach-Object {
# $subTenantHash.Add($_.SubscriptionId, $_.Owner)
# }
foreach ($entry in $usageSummary)
{
$usageWrapper = @()
$tenant = "Deleted Subscriptions"
# if ($subTenantHash.ContainsKey($entry.subscription)){
# $tenant = $subTenantHash[$entry.subscription]
# }
$usageData = New-Object psobject -Property @{
Type = 'Usage';
ID = $entry.Id;
Name = $entry.Name;
UsageType = $entry.Type;
MeterID = $entry.MeterId;
MeterName = $entry.MeterName;
Quantity = $entry.Quantity;
StartTime = $entry.UsageStartTime;
EndTime = $entry.UsageEndTime;
AdditionalInfo = $entry.additionalInfo;
Location = $entry.location;
CloudName = $entry.CloudName;
Tags = $entry.tags;
SubscriptionID = $entry.subscription;
ResourceType = $entry.resourceType;
ResourceName = $entry.resourceName;
ResourceURI = $entry.resourceUri;
DeploymentGuid = $deploymentGuid;
Tenant = $tenant;
Timestamp = (Get-Date).ToUniversalTime();
}
$usageWrapper += $usageData
$usageJson = ConvertTo-Json -InputObject $usageWrapper
Write-Host $usageJson
$Timestamp = "Timestamp"
#Upload JSON to OMS
#send-omsapiingestionfile -customerId $ws.CustomerId -sharedKey $wskey.PrimarySharedKey -body $usageJson -logType $logType -TimeStampField $Timestamp
Send-OMSAPIIngestionFile -customerId $OMSWorkspaceId -sharedKey $OMSSharedKey -body $usageJson -logType $logType -TimeStampField $Timestamp
}