BHS-PortalCliente.psm1
<#
=========================================================================== Created on: 02/10/2018 09:28 Created by: daniel.santos Organization: BHS Filename: BHS-PortalCliente.psm1 ------------------------------------------------------------------------- Module Name: BHS-PortalCliente =========================================================================== #> Function Get-BHSLicensePlan ( [Parameter(Mandatory = $True, HelpMessage = "Token para acessar autenticação")] $Headers, [Parameter(Mandatory = $True, HelpMessage = "Id da oferta junto a Microsoft")] $LicensePlanId, [Parameter(Mandatory = $True, HelpMessage = "Endereço de acesso")] $UrlBase) { Try { $Uri = "{0}/portaldb/LicensePlan/{1}" -f $UrlBase, $LicensePlanId $response = Invoke-RestMethod -Uri $Uri -Method Get -Headers $Headers if ($response) { $result = [PSCustomObject]@{ ResultState = "Success" Response = $response } return $result } else { $result = [PSCustomObject]@{ ResultState = "Warning" Response = $null ErrorMessage = "License Plan não encontrado"; } return $result } } catch { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "$($_.Exception.Message)" } return $result } } Function Get-BHSCompareOffice365Plan ( [Parameter(Mandatory = $True, HelpMessage = "Objeto msoluser")] $MsolUser, [Parameter(Mandatory = $True, HelpMessage = "Objeto LicensePlan")] $LicensePlan) { try { if ($MsolUser.IsLicensed) { $Return = @() $control = $true foreach ($License in $LicensePlan) { $check = $MsolUser.Licenses.AccountSkuId -join ";" -like "*{0}*" -f $License.SkuName if ($check -eq $false) { $return += $License $control = $false } } if ($control -eq $true) { return $true } else { return $Return } } else { $LicensePlan } } catch { return "$($_.Exception.Message)" } } Function New-BHSBuyLicenseOffice365 ( [Parameter(Mandatory = $True, HelpMessage = "Token para acessar autenticação")] $Headers, [String][Parameter(Mandatory = $True, HelpMessage = "Tenant Id")] $TenantID, [String][Parameter(Mandatory = $True, HelpMessage = "Id da oferta junto a Microsoft")] $OfferId, [Parameter(Mandatory = $True, HelpMessage = "Informa se a oferta é para ADDON")] $IsAddon, [String][Parameter(Mandatory = $False, HelpMessage = "Informa qual é a oferta pai do ADDON")] $ParentId, [Parameter(Mandatory = $True, HelpMessage = "Quantidade de licença a ser comprada")] $Quantity, [Parameter(Mandatory = $True, HelpMessage = "Endereço de acesso")] $UrlBase) { Try { if ($IsAddon) { $Uri = "{0}/mpc/Order/AddAddon?tenantId={1}&offerId={2}&addOnOfferId={3}&quantity={4}" -f $UrlBase, $TenantID, $ParentId, $OfferId, $Quantity } else { $Uri = "{0}/mpc/Order/AddLicense?tenantId={1}&offerId={2}&quantity={3}" -f $UrlBase, $TenantID, $OfferId, $Quantity } $response = Invoke-RestMethod -Uri $Uri -Method Post -ContentType 'application/json' -Headers $Headers $result = [PSCustomObject]@{ ResultState = "Success" Response = $response } return $result } catch { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "$($_.Exception.Message)" } return $result } } Function Get-BHSApiToken { param ( [String][Parameter(Mandatory = $true, HelpMessage = "Api Key")] $ApiKey, [Parameter(Mandatory = $true, HelpMessage = "Endereço de acesso")] $UrlBase, [Parameter(Mandatory = $false, HelpMessage = "Refresh Token")] $RefreshToken ) try { if (!$RefreshToken) { $Uri = "{0}/authentication/BhsToken/{1}" -f $UrlBase, $ApiKey $token = Invoke-RestMethod -Uri $Uri -Method Get -ErrorAction Stop $result = [PSCustomObject]@{ ResultState = "Success" Headers = @{ Authorization = "Bearer {0}" -f $token.tokenData.accessToken } TokenData = $token.tokenData ErrorMessage = "$($_.Exception.Message)" } } else { $Uri = "{0}/authentication/BhsToken" -f $UrlBase $Headers = @{ Authorization = "Bearer {0}" -f $RefreshToken } $token = Invoke-RestMethod -Uri $Uri -Method Get -Headers $Headers -ErrorAction Stop $result = [PSCustomObject]@{ ResultState = "Success" Headers = @{ Authorization = "Bearer {0}" -f $token.tokenData.accessToken } TokenData = $token.tokenData ErrorMessage = "$($_.Exception.Message)" } } return $result } catch { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "$($_.Exception.Message)" } return $result } } Function Get-BHSOffice365LicensesServices ( [Parameter(Mandatory = $True, HelpMessage = "Token para acessar autenticação")] $Headers, [Parameter(Mandatory = $True, HelpMessage = "Id da oferta junto a Microsoft")] $OfferId, [Parameter(Mandatory = $True, HelpMessage = "Trazer apenas com status {x}")] $Status, [Parameter(Mandatory = $True, HelpMessage = "Endereço de acesso")] $UrlBase) { Try { $Uri = "{0}/sqldw/office365/License/ServicesByManufacturerPartNumber/{1}" -f $UrlBase, $OfferId $response = Invoke-RestMethod -Uri $Uri -Method Get -Headers $Headers | Where-Object {$_.licenseServiceEnabled -eq $Status} $return = $response | Where-Object {$_.licenseServiceEnabled -eq $Status} $result = [PSCustomObject]@{ ResultState = "Success" Response = $return } return $result } catch { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "$($_.Exception.Message)" } return $result } } Function Remove-StringLatinCharacters( [Parameter(Mandatory = $True, HelpMessage = "UserPrincipalName")] $String) { [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding("Cyrillic").GetBytes($String)) } function Get-CheckUserPrincipalName ( [Parameter(Mandatory = $True, HelpMessage = "UserPrincipalName")] $UserPrincipalName) { try { Get-MsolUser -UserPrincipalName $UserPrincipalName -ErrorAction Stop| Out-Null $Result = [PSCustomObject]@{ Check = $True ResultState = "Success" ErrorMessage = "UserPrincipalName not available" } return $Result } catch { return $False $Result = [PSCustomObject]@{ Check = $False ResultState = "Success" ErrorMessage = "UserPrincipalName available" } return $Result } } function Get-BHSHybridWorkerConfig ( [Parameter(Mandatory = $True, HelpMessage = "TenantId")] $TenantId, [Parameter(Mandatory = $True, HelpMessage = "Bool")] $Check, [Parameter(Mandatory = $True, HelpMessage = "Token para acessar autenticação")] $Headers, [Parameter(Mandatory = $True, HelpMessage = "Endereço de acesso")] $UrlBase) { Try { if ($Check -eq $True) { $Uri = "{0}/portaldb/HybridWorker/check/{1}" -f $UrlBase, $TenantID $response = Invoke-RestMethod -Uri $Uri -Method Get -Headers $Headers if (!$response) { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "Não encontrado" } } else { $result = [PSCustomObject]@{ ResultState = "Success" ResultMessage = $response } } } else { $Uri = "{0}/portaldb/HybridWorker/{1}" -f $UrlBase, $TenantID $response = Invoke-RestMethod -Uri $Uri -Method Get -Headers $Headers $result = [PSCustomObject]@{ ResultState = "Success" ResultMessage = $response } } return $result } catch { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "$($_.Exception.Message)" } return $result } } function Get-BHSSecretValue ( [Parameter(Mandatory = $True, HelpMessage = "Secret Name")] $SecretName, [Parameter(Mandatory = $True, HelpMessage = "Endereço de acesso")] $UrlBase, [Parameter(Mandatory = $True, HelpMessage = "Token para acessar autenticação")] $Headers) { Try { $Uri = "{0}/keyvault/Secret/Name/{1}" -f $UrlBase, $SecretName $response = Invoke-RestMethod -Uri $Uri -Method Get -Headers $Headers if (!$response) { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "Não encontrado" } } else { $result = [PSCustomObject]@{ ResultState = "Success" ResultMessage = $response.value } } return $result } catch { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "$($_.Exception.Message)" } return $result } } function Get-BHSProcessCredential ( [Parameter(Mandatory = $True, HelpMessage = "API Key")] $Key, [Parameter(Mandatory = $True, HelpMessage = "URI para coletar credencial")] $Uri, [Parameter(Mandatory = $True, HelpMessage = "Token para acessar autenticação")] $Headers ) { Try { $Response = Invoke-RestMethod -Uri $Uri -Method Get -Headers $Headers if ($Response.ResultState -eq "Success") { $SecurePassword = $Response.UserPassword | ConvertTo-SecureString -Key $key $Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $Response.UserName, $SecurePassword return $Credential } else{ Throw "Não foi possível encontrar a credencial" } } catch { Throw "Não foi possível encontrar a credencial" } } function Get-BHSGenerateHash ( $Key, $String ) { Try { $SecureString = ConvertTo-SecureString -String $String -AsPlainText -Force $Encrypted = ConvertFrom-SecureString -SecureString $SecureString -Key $key $result = [PSCustomObject]@{ ResultState = "Success" ResultMessage = $Encrypted } return $result } catch { Throw } } Function Set-BHSAdAttributes ($Object, $Credential) { if ($Object.CustomAttribute1) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute1 -Credential $Credential -Server $Object.Domain Set-ADUser $Object.SamAccountName -Add @{extensionAttribute1 = $Object.CustomAttribute1} -Credential $Credential -Server $Object.Domain } if ($Object.CustomAttribute2) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute2 -Credential $Credential -Server $Object.Domain Set-ADUser $Object.SamAccountName -Add @{extensionAttribute2 = $Object.CustomAttribute2} -Credential $Credential -Server $Object.Domain } if ($Object.CustomAttribute3) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute3 -Credential $Credential -Server $Object.Domain Set-ADUser $Object.SamAccountName -Add @{extensionAttribute3 = $Object.CustomAttribute3} -Credential $Credential -Server $Object.Domain } if ($Object.CustomAttribute4) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute4 -Credential $Credential -Server $Object.Domain Set-ADUser $Object.SamAccountName -Add @{extensionAttribute4 = $Object.CustomAttribute4} -Credential $Credential -Server $Object.Domain } if ($Object.CustomAttribute5) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute5 -Credential $Credential -Server $Object.Domain Set-ADUser $Object.SamAccountName -Add @{extensionAttribute5 = $Object.CustomAttribute5} -Credential $Credential -Server $Object.Domain } if ($Object.CustomAttribute6) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute6 -Credential $Credential -Server $Object.Domain Set-ADUser $Object.SamAccountName -Add @{extensionAttribute6 = $Object.CustomAttribute6} -Credential $Credential -Server $Object.Domain } #if ($usermaster) { # Set-ADUser $Object.SamAccountName -Manager $usermaster.DistinguishedName -Credential $Credential #} if ($Object.PrimarySmtpAddress) { $AdUser = Get-ADUser $Object.SamAccountName -Properties proxyAddresses -Server $Object.Domain -Credential $Credential $newEmailAddress = @() $_primarySmtpAddress = $Object.PrimarySmtpAddress if ($AdUser.proxyAddresses) { $aux = "smtp:$_primarySmtpAddress" $currentEmailAddress = $AdUser.proxyAddresses.replace("SMTP:", "smtp:") if ($currentEmailAddress -like "*smtp:$_primarySmtpAddress*") { $newEmailAddress = $currentEmailAddress.replace("$aux", "SMTP:$_primarySmtpAddress") } else { $newEmailAddress += $currentEmailAddress $newEmailAddress += "SMTP:$_primarySmtpAddress" } if ($Object.TenantId -eq "00D1C4C4-973A-4BD9-A55F-F938D5884C60") { if (!$newEmailAddress -like "*@agenteinvest.com.br*") { $newEmailAddress += "smtp:{0}@agenteinvest.com.br" -f $Object.SamAccountName } if (!$newEmailAddress -like "*SIP*") { $newEmailAddress += "SIP:{0}" -f $Object.PrimarySmtpAddress } } Set-ADUser $Object.SamAccountName -Clear proxyAddresses -Credential $Credential -Server $Object.Domain foreach ($address in $newEmailAddress) { Set-ADUser $Object.SamAccountName -Add @{proxyAddresses = $address} -Credential $Credential -Server $Object.Domain } } else { $newEmailAddress = @() $newEmailAddress += "SMTP:$_primarySmtpAddress" if ($Object.TenantId -eq "00D1C4C4-973A-4BD9-A55F-F938D5884C60") { $newEmailAddress += "SIP:$_primarySmtpAddress" $newEmailAddress += "smtp:{0}@agenteinvest.com.br" -f $Object.SamAccountName } foreach ($address in $newEmailAddress) { Set-ADUser $Object.SamAccountName -Add @{proxyAddresses = $address} -Credential $Credential -Server $Object.Domain } } } } function Get-CheckCodigoAssessor ( [Parameter(Mandatory = $True, HelpMessage = "Tenant Id")] $TenantId, [Parameter(Mandatory = $True, HelpMessage = "Codigo de Assessor")] $CodAssessor, [Parameter(Mandatory = $True, HelpMessage = "Endereço de acesso")] $UrlBase, [Parameter(Mandatory = $True, HelpMessage = "Token para acessar autenticação")] $Headers) { Try { <# $Headers = $Token.Headers $TenantId = "00d1c4c4-973a-4bd9-a55f-f938d5884c60" $CodAssessor = "B00002" #> $Uri = "{0}/graph/Check/CheckCodigoAssessor?tenantId={1}&value={2}" -f $UrlBase, $TenantId, $CodAssessor $response = Invoke-RestMethod -Uri $Uri -Method Get -Headers $Headers if (!$response) { Throw "$($_.Exception.Message)" } else { if($response.resultState -eq "Available"){ $result = [PSCustomObject]@{ ResultState = "Success" ErrorMessage = $null } return $result } else{ $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = $response.errorMessage } return $result } } } catch { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "$($_.Exception.Message)" } return $result } } |