function New-PortalUser([string]$adminLogin, [string]$adminPassword, [string]$userEmail, [string]$authenticationSite, [string]$clientRealmURI, [string]$adminURI)
{
	$Token = Get-Token -adminLogin $adminLogin -adminPassword $adminPassword -authenticationSite $authenticationSite -clientRealmURI $clientRealmURI;
	
	# Создаем пользователя на портале
	Write-Host ($MSG_PORTAL_CREATING_USER -f $userEmail,$adminURI);
	$User = Add-MgmtSvcUser -AdminUri $adminURI -Token $Token -Name $userEmail -email $userEmail -DisableCertificateValidation -State 'Active' -ErrorAction Stop;
	Write-Host ($MSG_PORTAL_CREATED_USER -f $userEmail,$adminURI);
}

function Delete-PortalUser([string]$adminLogin, [string]$adminPassword, [string]$userEmail, [string]$authenticationSite, [string]$clientRealmURI, [string]$adminURI)
{
	$Token = Get-Token -adminLogin $adminLogin -adminPassword $adminPassword -authenticationSite $authenticationSite -clientRealmURI $clientRealmURI;
	
	# Удаляем пользователя с портала
	Write-Host ($MSG_PORTAL_DELETING_USER -f $userEmail,$adminURI);
	$User = Remove-MgmtSvcUser -AdminUri $adminURI -Token $Token -Name $userEmail -Force -DisableCertificateValidation  -Confirm:$false -ErrorAction Stop;
	Write-Host ($MSG_PORTAL_DELETED_USER -f $userEmail,$adminURI);
}

function New-MemberShipUser([string]$userEmail, [string]$userPassword)
{
	$provider = [System.Web.Security.Membership]::Providers[$providerName];
	[Guid]$UserID = [Guid]::NewGuid();
	$status = 0;
	
	Write-Host ($MSG_AZURE_MEMBERSHIP_CREATING_USER -f $userEmail);
	$userMemberShipObj = $provider.CreateUser($userEmail, $userPassword, $userEmail, "1", "1", $true, $UserID, [ref]$status);
	if($userMemberShipObj -eq $null)
	{
		throw $status;
	}
	else
	{
		Write-Host ($MSG_AZURE_MEMBERSHIP_CREATED_USER -f $userEmail);
	}
}

function Delete-MemberShipUser([string]$userEmail)
{
	$provider = [System.Web.Security.Membership]::Providers[$providerName];
	[Guid]$UserID = [Guid]::NewGuid();
	$status = 0;
	
	Write-Host ($MSG_AZURE_MEMBERSHIP_DELETING_USER -f $userEmail);
	$userDeleted = $provider.DeleteUser($userEmail, $true);
	if($userDeleted)
	{
		Write-Host ($MSG_AZURE_MEMBERSHIP_DELETED_USER -f $userEmail);
	}
	else
	{
		throw ($MSG_AZURE_MEMBERSHIP_DELETING_USER_ERROR -f $userEmail);
	}
}

function Add-Subscription([string]$adminLogin, [string]$adminPassword, [string]$userEmail, [string]$planName, [string]$authenticationSite, [string]$clientRealmURI, [string]$adminURI)
{
	$Token = Get-Token -adminLogin $adminLogin -adminPassword $adminPassword -authenticationSite $authenticationSite -clientRealmURI $clientRealmURI;
	
	# Получаем план
	Write-Host ($MSG_PORTAL_PLAN_GETTING -f $planName,$adminURI);
	$Plan = Get-MgmtSvcPlan -AdminUri $adminURI -Token $Token -DisableCertificateValidation -ErrorAction Stop | Where-Object { $_.DisplayName -eq $planName };
	if($Plan -eq $null)
	{
		throw ($MSG_ERROR_PORTAL_PLAN_GETTING -f $planName,$adminURI);
	}
	Write-Host ($MSG_PORTAL_PLAN_GETTED -f $planName,$adminURI);
	
	# Проверяем подписан ли уже пользователь на этот план
	Write-Host ($MSG_PORTAL_SUBSCRIPTION_EXIST_CHECKING -f $userEmail,$planName,$adminURI);
	$Subscription = Get-MgmtSvcSubscription -AdminUri $adminURI -Token $Token -DisableCertificateValidation -ErrorAction Stop | Where-Object { ($_.PlanId -eq $Plan.Id) -and ($_.AccountAdminLiveEmailId -eq $userEmail) };
	if($Subscription -eq $null)
	{
		# Пользователь не подписан, добавляем подписку
		Write-Host ($MSG_PORTAL_SUBSCRIPTION_NOT_EXIST -f $userEmail,$planName,$adminURI);
		Write-Host ($MSG_PORTAL_SUBSCRIPTION_ADDING -f $userEmail,$planName,$adminURI);
		$Subscription = Add-MgmtSvcSubscription -AdminUri $adminURI -Token $Token -AccountAdminLiveEmailId $userEmail -AccountAdminLivePuid $userEmail -PlanId $Plan.Id -FriendlyName $userEmail -DisableCertificateValidation -ErrorAction Stop;
		Write-Host ($MSG_PORTAL_SUBSCRIPTION_ADDED -f $userEmail,$planName,$adminURI);
	}
	Write-Host ($MSG_PORTAL_SUBSCRIPTION_EXIST -f $userEmail,$planName,$adminURI);
	return $Subscription.SubscriptionID;
}

function Get-Subscription([string]$adminLogin, [string]$adminPassword, [string]$userEmail, [string]$authenticationSite, [string]$clientRealmURI, [string]$adminURI)
{
	$Token = Get-Token -adminLogin $adminLogin -adminPassword $adminPassword -authenticationSite $authenticationSite -clientRealmURI $clientRealmURI;
	
	# Проверяем подписан ли уже пользователь на этот план
	Write-Host ($MSG_PORTAL_SUBSCRIPTION_EXIST_CHECKING -f $userEmail,$planName,$adminURI);
	$Subscription = Get-MgmtSvcSubscription -AdminUri $adminURI -Token $Token -DisableCertificateValidation -ErrorAction Stop | Where-Object { $_.AccountAdminLiveEmailId -eq $userEmail };
	if($Subscription -eq $null)
	{
		return $null;
	}
	else
	{
		return $Subscription.SubscriptionID;
	}
}

function Get-Token([string]$adminLogin, [string]$adminPassword, [string]$authenticationSite, [string]$clientRealmURI)
{
	# Создаем учетные данные
	$Password = ConvertTo-SecureString –String $adminPassword –AsPlainText -Force -ErrorAction Stop;
	$UserCredential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $adminLogin, $Password -ErrorAction Stop;
	
	# Получаем токен администратора
	Write-Host ($MSG_PORTAL_TOKEN_GETTING -f $authenticationSite);
	$Token = Get-MgmtSvcToken –AuthenticationSite $authenticationSite -ClientRealm $clientRealmURI -User $UserCredential -DisableCertificateValidation -Type Windows -ErrorAction Stop;
	Write-Host ($MSG_PORTAL_TOKEN_GETTED -f $authenticationSite);
	
	return $Token;
}