Samples/InstallAndAddDataGateway-Sample.ps1


  # This sample helps automate the installation and configuration of the On-premises data gateway using available PowerShell cmdlets. This script helps with silent install of new gateway cluster with one gateway member only. The script also allows addition gateway admins. For information on each PowerShell script visit the help page for individual PowerSHell cmdlets.

# Before begining to install and register a gateway, for connecting to the gateway service, you would need to use the # Connect-DataGatewayServiceAccount. More information documented in the help page of that cmdlet.

Param(
  # Documented on the Install-DataGateway help page
  [Parameter()]
  [Switch]
  $AcceptConditions,

  # Documented on the Install-DataGateway help page
  [Parameter()]
  [string]
  $InstallerLocation,

  # Documented on the Add-DataGatewayCluster help page
  [Parameter(Mandatory = $true)]
  [SecureString]
  $RecoveryKey,

  # Documented on the Add-DataGatewayCluster help page
  [Parameter(Mandatory = $true)]
  [string]
  $GatewayName,

  # Documented on the Add-DataGatewayClusterUser help page as $PrincipalObjectId
  # Note: If you are using a service principal to configure your gateway,
  # specifying this paramater will be the only way to administer the
  # gateway outside of the PowerShell module.
  [Parameter()]
  [Guid]
  $AdditionalGatewayAdminIdForNewGateway
)
$ErrorActionPreference = "stop"

# Thrown an error if not logged in
Get-DataGatewayAccessToken | Out-Null

# Run the gateway installer on the local computer
$installDataGatewayArguments = @{
  AcceptConditions  = $AcceptConditions; 
  InstallerLocation = $InstallerLocation;
}
Install-DataGateway @installDataGatewayArguments

# Create a gateway cluster and save the cluster ID
$addDataGatewayClusterArguments = @{
  RecoveryKey = $RecoveryKey;
  GatewayName = $GatewayName;
}
$newGatewayClusterId = (Add-DataGatewayCluster @addDataGatewayClusterArguments).GatewayObjectId

# Optionally add admin to new gateway
if ($null -ne $AdminPrincipalObjectIdForNewGateway) {
  $addDataGatewayClusterUserArguments = @{
    GatewayClusterId       = $newGatewayClusterId;
    PrincipalObjectId      = $AdditionalGatewayAdminIdForNewGateway;
    Role                   = "Admin";
    AllowedDataSourceTypes = $null;
  }
  Add-DataGatewayClusterUser @addDataGatewayClusterUserArguments
}