<commit_before>﻿<#

.SYNOPSIS 

    This Azure/OMS Automation runbook onboards a hybrid worker. A resource group, automation account, OMS workspace, 
    and VM will all be generated if needed.


.DESCRIPTION

    This Azure/OMS Automation runbook onboards a hybrid worker. A resource group, automation account, OMS workspace,
    and VM will all be generated if needed. The major steps of the script are outlined below.
    
    1) Login to an Azure account
    2) Create a resource group if needed
    3) Create and configure an Automaiton Account if needed
    4) Create an OMS Workspace if needed
    5) Enable the Azure Automation solution in OMS
    6) Check if the machine provided was flagged as an on-premise device
        a) If not, create and configure a VM if needed
    7) Download the DSC agent
    8) Submit the configuration to register the machine as a hybrid worker


.PARAMETER IDString

    Optional. A string added to newly generated resources to create unique identifiers, If not specified,

    a random number (Maximum of 99999) is used.
 

.EXAMPLE

    New-HybridWorker


.NOTES

    AUTHOR: <NAME>, Azure/OMS Automation Team

    LASTEDIT: August 16, 2016  

#>


#Requires -RunAsAdministrator

Param (
# Setup initial variables
[Parameter(Mandatory=$false)]
[String] $IDString = "43479",
#[String] $IDString = (Get-Random -Maximum 99999),

[Parameter(Mandatory=$false)]
[String] $ResourceGroup = "hybrid-worker-" + $IDstring,

[Parameter(Mandatory=$false)]
#[String] $SubscriptionID = "",
[String] $SubscriptionID = "c9d8154e-9f57-47a9-8871-1f9b29fe2199",

# OMS Workspace
[Parameter(Mandatory=$false)]
[String] $WorkspaceName = "hybrid-worker-" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $Location = "eastus",

# Automation
[Parameter(Mandatory=$false)]
[String] $AutomationAccountName = "hybrid-worker-AA-" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $AutomationLocation = "eastus2",

[Parameter(Mandatory=$false)]
[String] $ApplicationDisplayName = $AutomationAccountName,

[Parameter(Mandatory=$false)]
[String] $CertPlainPassword = "<PASSWORD>",

[Parameter(Mandatory=$false)]
[int] $NoOfMonthsUntilExpired = 12,

# VM
[Parameter(Mandatory=$false)]
[Boolean] $OnPremise = $false,

[Parameter(Mandatory=$false)]
[String] $MachineName = "hybridVM" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $VMUser = "hybridUser",

[Parameter(Mandatory=$false)]
[String] $VMPassword = "<PASSWORD>",

[Parameter(Mandatory=$false)]
[String] $AvailabilityName = "hybrid-worker-availability-" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $StorageName = "hybridworkerstorage" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $OSDiskName = $MachineName + "OSDisk",

[Parameter(Mandatory=$false)]
[String] $VNetName = $ResourceGroup,

[Parameter(Mandatory=$false)]
[String] $PIpName = $ResourceGroup,

[Parameter(Mandatory=$false)]
[String] $InterfaceName = $ResourceGroup
)

# Parameter adjustment
$VMSecurePassword = ConvertTo-SecureString $VMPassword -AsPlainText -Force;
$VMCredential = New-Object System.Management.Automation.PSCredential ($VMUser, $VMSecurePassword);

# Login to Azure account
$Account = Add-AzureRmAccount

# 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-AzureRmSubscription

    # 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-AzureRmSubscription -SubscriptionId $SubscriptionID
    # Get the tenant id for this subscription
    $TenantID = $Subscription.TenantId
}


Set-AzureRmContext -SubscriptionID $SubscriptionID

# Create the resource group if needed
try {

    Get-AzureRmResourceGroup -Name $ResourceGroup -ErrorAction Stop
} catch {

    New-AzureRmResourceGroup -Name $ResourceGroup -Location $Location -WarningAction SilentlyContinue
}

#Create a new automation account if needed 
try {

    Get-AzureRmAutomationAccount -ResourceGroupName $ResourceGroup -Name $AutomationAccountName -ErrorAction Stop
} catch {

    New-AzureRmAutomationAccount -Name $AutomationAccountName -Location $AutomationLocation -ResourceGroupName $ResourceGroup -Plan "Free" -WarningAction SilentlyContinue
}

# Check if the account is configured as a runAs account
$ConnectionAssetName = "AzureRunAsConnection"

try {
    $ServicePrincipalConnection = Get-AzureRmAutomationConnection -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccountName -Name $ConnectionAssetName -ErrorAction Stop
} catch {    
    # Configure the Automation account as a RunAs account
    $CurrentDate = Get-Date
    $EndDate = $CurrentDate.AddMonths($NoOfMonthsUntilExpired)
    $KeyId = (New-Guid).Guid<commit_msg>Updated Parameter Descriptions and Consolidated Location<commit_after>﻿<#

.SYNOPSIS 

    This Azure/OMS Automation runbook onboards a hybrid worker. A resource group, automation account, OMS workspace, 
    and VM will all be generated if needed.


.DESCRIPTION

    This Azure/OMS Automation runbook onboards a hybrid worker. A resource group, automation account, OMS workspace,
    and VM will all be generated if needed. The major steps of the script are outlined below.
    
    1) Login to an Azure account
    2) Create a resource group if needed
    3) Create and configure an Automaiton Account if needed
    4) Create an OMS Workspace if needed
    5) Enable the Azure Automation solution in OMS
    6) Check if the machine provided was flagged as an on-premise device
        a) If not, create and configure a VM if needed
    7) Download the DSC agent
    8) Submit the configuration to register the machine as a hybrid worker


.PARAMETER IDString

    Optional. A string added to newly generated resources to create unique identifiers. If not specified,

    a random number (Maximum of 99999) is used.

 
.PARAMETER ResourceGroup

    Optional. The name of the resource group to be referenced. If not specified, a new resource group

    is created, referencing the IDString in order to create a unique identifier.


.PARAMETER SubscriptionID

    Optional. A string containing the SubscriptionID to be used. If not specified, the first subscriptionID

    associated with the account is used.


.PARAMETER WorkspaceName

    Optional. The name of the OMS Workspace to be referenced. If not specified, a new OMS workspace 

    is created, referencing the IDString in order to create a unique identifier.


.PARAMETER Location

    Optional. The region of the OMS workspace and VM to be referenced. If not specified, "westeurope" is used.


.PARAMETER AutomationAccountName

    Optional. The name of the Automation account to be referenced. If not specified, a new automation account 

    is created, referencing the IDString in order to create a unique identifier.


.PARAMETER ApplicationDisplayName

    Optional. The name of the Application Display to be referenced. If not specified, a new application 

    is created with the AutomationAccountName as the name.


.PARAMETER CertPlainPassword

    Optional. The password for the AzureRunAs certificate. If not specified,"<PASSWORD>" is used.


.PARAMETER NoOfMonthsUntilExpired

    Optional. The number of months until the Automation key credential expires. If not specified, 12 is used.


.PARAMETER OnPremise

    Optional. A boolean to flag the provided machine as an on-premise device. If not specified, the value is

    set to false, and the machine is assumed to be an Azure VM.


.PARAMETER MachineName

    Optional. The computer name (Azure VM or on-premise) to be referenced. If not specified, a computer name

    is created, referencing the IDString in order to create a unique identifier.


.PARAMETER VMUser

    Optional. The username for the provided user machine. If not specified,"hybridUser" is used.


.PARAMETER VMPassword

    Optional. The password for the provided user on the machine. If not specified,"<PASSWORD>" is used.


.PARAMETER AvailabilityName

    Optional. The name of the Availability set to be referenced. If not specified, a new availability set 

    is created, referencing the IDString in order to create a unique identifier.


.PARAMETER StorageName

    Optional. The name of the Storage account to be referenced. If not specified, a new storage account 

    is created, referencing the IDString in order to create a unique identifier.


.PARAMETER OSDiskName

    Optional. The name of the OS Disk to be referenced. If not specified, a new OS Disk is created, 

    referencing the IDString in order to create a unique identifier.


.PARAMETER VNetName

    Optional. The name of the virtual network to be referenced. If not specified, a new virtual network

    is created with the Resource Group name as the virtal network name.


.PARAMETER PIpName

    Optional. The Public IP address name to be referenced. If not specified, a new public IP address is 

    created with the Resource Group name as the public IP address name.


.PARAMETER InterfaceName

    Optional. The name of the network interface to be referenced. If not specified, a new interface is

    created with the Resource Group name as the network interface name.


.EXAMPLE

    New-HybridWorker

    New-HybridWorker -IDString 12345


.NOTES

    AUTHOR: <NAME>, Azure/OMS Automation Team

    LASTEDIT: August 18, 2016  

#>


#Requires -RunAsAdministrator

Param (
# Setup initial variables
[Parameter(Mandatory=$false)]
#[String] $IDString = "49583",
[String] $IDString = (Get-Random -Maximum 99999),

[Parameter(Mandatory=$false)]
[String] $ResourceGroup = "hybrid-worker-" + $IDstring,

[Parameter(Mandatory=$false)]
#[String] $SubscriptionID = "",
[String] $SubscriptionID = "c9d8154e-9f57-47a9-8871-1f9b29fe2199",

# OMS Workspace
[Parameter(Mandatory=$false)]
[String] $WorkspaceName = "hybrid-worker-" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $Location = "westeurope",

# Automation
[Parameter(Mandatory=$false)]
[String] $AutomationAccountName = "hybrid-worker-AA-" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $ApplicationDisplayName = $AutomationAccountName,

[Parameter(Mandatory=$false)]
[String] $CertPlainPassword = "<PASSWORD>",

[Parameter(Mandatory=$false)]
[int] $NoOfMonthsUntilExpired = 12,

# VM
[Parameter(Mandatory=$false)]
[Boolean] $OnPremise = $false,

[Parameter(Mandatory=$false)]
[String] $MachineName = "hybridVM" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $VMUser = "hybridUser",

[Parameter(Mandatory=$false)]
[String] $VMPassword = "<PASSWORD>",

[Parameter(Mandatory=$false)]
[String] $AvailabilityName = "hybrid-worker-availability-" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $StorageName = "hybridworkerstorage" + $IDstring,

[Parameter(Mandatory=$false)]
[String] $OSDiskName = $MachineName + "OSDisk",

[Parameter(Mandatory=$false)]
[String] $VNetName = $ResourceGroup,

[Parameter(Mandatory=$false)]
[String] $PIpName = $ResourceGroup,

[Parameter(Mandatory=$false)]
[String] $InterfaceName = $ResourceGroup
)

# Parameter adjustment
$VMSecurePassword = ConvertTo-SecureString $VMPassword -AsPlainText -Force;
$VMCredential = New-Object System.Management.Automation.PSCredential ($VMUser, $VMSecurePassword);

# Login to Azure account
$Account = Add-AzureRmAccount

# 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-AzureRmSubscription

    # 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-AzureRmSubscription -SubscriptionId $SubscriptionID
    # Get the tenant id for this subscription
    $TenantID = $Subscription.TenantId
}


Set-AzureRmContext -SubscriptionID $SubscriptionID

# Create the resource group if needed
try {

    Get-AzureRmResourceGroup -Name $ResourceGroup -ErrorAction Stop
} catch {

    New-AzureRmResourceGroup -Name $ResourceGroup -Location $Location -WarningAction SilentlyContinue
}

#Create a new automation account if needed 
try {

    Get-AzureRmAutomationAccount -ResourceGroupName $ResourceGroup -Name $AutomationAccountName -ErrorAction Stop
} catch {

    New-AzureRmAutomationAccount -Name $AutomationAccountName -Location $Location -ResourceGroupName $ResourceGroup -Plan "Free" -WarningAction SilentlyContinue
}

# Check if the account is configured as a runAs account
$ConnectionAssetName = "AzureRunAsConnection"

try {
    $ServicePrincipalConnection = Get-AzureRmAutomationConnection -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccountName -Name $ConnectionAssetName -ErrorAction Stop
} catch {    
    # Configure the Automation account as a RunAs account
    $CurrentDate = Get-Date
    $EndDate = $CurrentDate.AddMonths($NoOfMonthsUntilExpired)
    $KeyId = (New-Guid).Guid
{'fraction_non_alphanumeric': 0.0854143232095988, 'fraction_numerical': 0.008998875140607425, 'mean_word_length': 4.506193228736581, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6931658', 'n_tokens_mistral': 3933, 'n_tokens_neox': 3753, 'n_words': 1374}