BHS-PortalCliente.psm1
<#
=========================================================================== Created on: 02/10/2018 09:28 Created by: daniel.santos Organization: BHS Filename: BHS-PortalCliente.psm1 ------------------------------------------------------------------------- Module Name: BHS-PortalCliente =========================================================================== #> #https://dev-portal.bhs.com.br/api/portaldb/LicensePlan/BACE76F1-888B-4EE0-9B8F-1D83B3566C72 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 { <#Teste $User = $CredentialAPI.UserName $Password = $CredentialAPI.GetNetworkCredential().Password #> param ( [String][Parameter(Mandatory = $false, HelpMessage = "Usuário para acessar api de login")] $User, [String][Parameter(Mandatory = $false, HelpMessage = "Senha para acessar api de login")] $Password, [System.Management.Automation.PSCredential][Parameter(Mandatory = $false, HelpMessage = "Credencial api de login")] $ApiCredential, [Parameter(Mandatory = $True, HelpMessage = "Endereço de acesso")] $UrlBase ) try { if($ApiCredential){ $jsonRequestAutentication = @{ UserID = $ApiCredential.GetNetworkCredential().UserName; accessKey = $ApiCredential.GetNetworkCredential().Password; } | ConvertTo-Json } else{ $jsonRequestAutentication = @{ UserID = $User; accessKey = $Password; } | ConvertTo-Json } $Uri = "{0}/authentication/Login" -f $UrlBase $token = Invoke-RestMethod -Uri $Uri -Method Post -Body ([System.Text.Encoding]::UTF8.GetBytes($jsonRequestAutentication)) -ContentType 'application/json' -ErrorAction Stop $result = [PSCustomObject]@{ ResultState = "Success" Headers = @{ Authorization = "Bearer {0}" -f $token.accessToken } ErrorMessage = "$($_.Exception.Message)" } return $result } catch { $result = [PSCustomObject]@{ ResultState = "Failed" ErrorMessage = "$($_.Exception.Message)" } return $result } } Function Get-BHSOffice365LicensesServices ( <# a Teste $Headers = $Headers $OfferId = $License.OfferId $Status = $false #> [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 ( $Key, $Uri ) { Try { $Response = Invoke-RestMethod -Uri $Uri -Method Get 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 } } catch { Throw } } 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 Set-ADUser $Object.SamAccountName -Add @{extensionAttribute1 = $Object.CustomAttribute1} -Credential $Credential } if ($Object.CustomAttribute2) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute2 -Credential $Credential Set-ADUser $Object.SamAccountName -Add @{extensionAttribute2 = $Object.CustomAttribute2} -Credential $Credential } if ($Object.CustomAttribute3) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute3 -Credential $Credential Set-ADUser $Object.SamAccountName -Add @{extensionAttribute3 = $Object.CustomAttribute3} -Credential $Credential } if ($Object.CustomAttribute4) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute4 -Credential $Credential Set-ADUser $Object.SamAccountName -Add @{extensionAttribute4 = $Object.CustomAttribute4} -Credential $Credential } if ($Object.CustomAttribute5) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute5 -Credential $Credential Set-ADUser $Object.SamAccountName -Add @{extensionAttribute5 = $Object.CustomAttribute5} -Credential $Credential } if ($Object.CustomAttribute6) { Set-ADUser $Object.SamAccountName -Clear extensionAttribute6 -Credential $Credential Set-ADUser $Object.SamAccountName -Add @{extensionAttribute6 = $Object.CustomAttribute6} -Credential $Credential } #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 foreach($address in $newEmailAddress){ Set-ADUser $Object.SamAccountName -Add @{proxyAddresses = $address} -Credential $Credential } } else{ $newEmailAddress = "SMTP:$_primarySmtpAddress" if($Object.TenantId -eq "00D1C4C4-973A-4BD9-A55F-F938D5884C60"){ $newEmailAddress = "SIP:$_primarySmtpAddress" $newEmailAddress = "smtp:$_primarySmtpAddress" } foreach($address in $newEmailAddress){ Set-ADUser $Object.SamAccountName -Add @{proxyAddresses = $address} -Credential $Credential } } } } |