forked from Azure/azure-docs-powershell-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-analytics-create-new-resource.ps1
57 lines (40 loc) · 1.95 KB
/
log-analytics-create-new-resource.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
#Variables for common values
$ResourceGroupName = "ResourceGroup01"
$SubscriptionID = "SubscriptionID"
$WorkspaceName = "DefaultWorkspace-" + (Get-Random -Maximum 99999) + "-" + $ResourceGroupName
$Location = "eastus"
# Stop the script if any errors occur
$ErrorActionPreference = "Stop"
# Connect to the current Azure account
Write-Output "Pulling Azure account credentials..."
# Login to Azure account
$Account = Add-AzAccount
# If a subscriptionID has not been provided, select the first registered to the account
if ([string]::IsNullOrEmpty($SubscriptionID)) {
# Get a list of all subscriptions
$Subscription = Get-AzSubscription
# Get the subscription ID
$SubscriptionID = (($Subscription).SubscriptionId | Select -First 1).toString()
# Get the tenant id for this subscription
$TenantID = (($Subscription).TenantId | Select -First 1).toString()
} else {
# Get a reference to the current subscription
$Subscription = Get-AzSubscription -SubscriptionId $SubscriptionID
# Get the tenant id for this subscription
$TenantID = $Subscription.TenantId
}
# Set the active subscription
$null = Set-AzContext -SubscriptionID $SubscriptionID
# Check that the resource group is valid
$null = Get-AzResourceGroup -Name $ResourceGroupName
# Create a new Log Analytics workspace if needed
try {
$Workspace = Get-AzOperationalInsightsWorkspace -Name $WorkspaceName -ResourceGroupName $ResourceGroupName -ErrorAction Stop
$ExistingtLocation = $Workspace.Location
Write-Output "Workspace named $WorkspaceName in region $ExistingLocation already exists."
Write-Output "No further action required, script quitting."
} catch {
Write-Output "Creating new workspace named $WorkspaceName in region $Location..."
# Create the new workspace for the given name, region, and resource group
$Workspace = New-AzOperationalInsightsWorkspace -Location $Location -Name $WorkspaceName -Sku Standard -ResourceGroupName $ResourceGroupName
}