MongoDB/VegaShellMongoDB.ps1
function ConnectMongoDb { <# .SYNOPSIS Cette fonction sert a initier la connexion a une base MongoDB utilisé dans les function find /update /insert /delete .DESCRIPTION ConnectionString --> caine de connexion mongodb $MongoDBUsername --> user $MongoDBPassword --> password secureString $DataBaseName --> Database ou ce connecter .EXAMPLE $ParamConnectMongoDB = @{ ConnectionString = 'mongodb+srv://prod.[....].azure.mongodb.net/YPN_cache' MongoDBUsername = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue 'common-mongouser') MongoDBPassword = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue 'common-mongodb-password' -EncryptOutput) DataBaseName = 'YPN_Cache' } $ConnectDatabase = connectMongoDb @ParamConnectMongoDB #> [CmdletBinding()] param ( [ValidateNotNullOrEmpty()] $ConnectionString, [ValidateNotNullOrEmpty()] [String]$MongoDBUsername, [ValidateNotNullOrEmpty()] [SecureString]$MongoDBPassword, [ValidateNotNullOrEmpty()] $Database ) begin { try { try { import-module Mdbc | Out-Null Write-Verbose "Modules Installed and Imported succesfully" } catch { install-module -Name Mdbc -AllowClobber -force import-module Mdbc | Out-Null } try { $ssPtrProd = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($MongoDBPassword) $SecretKey = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtrProd) } catch { TryCatchError } finally { #purge une variable en mémoire non managé sinon peut provoquer un memory leak [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtrProd) } Write-Verbose -message "Generating Connection string" $mongoConnectionStr = $ConnectionString.split('//') | Where-Object { $_ } $mongoConnectionStrWithCred = "$($mongoConnectionStr[0])//$($MongoDBUsername):$([uri]::EscapeDataString($SecretKey))@$($mongoConnectionStr[1])" } catch { TryCatchError } } process { try { Connect-Mdbc -ConnectionString $mongoConnectionStrWithCred -DatabaseName $Database } catch { TryCatchError } } end { Write-Information "Connected to MongoDB using : $($mongoConnectionStrWithCred)" return $Database } } function FindMongo { <# .SYNOPSIS Cette fonction sert a faire une recherche dans mongo db qui pourra etre appelée dans d'autre scripts .DESCRIPTION ConnectionString --> permet de rechercher si un champ existe $Database --> base de données $Collection --> collection $field --> champs de recherche $value --> valeur du champs recherché $field2 --> 2eme champs de recherche $value2 --> 2eme valeur du champs recherché $ExistField --> champ que l on veut filtrer si il existe ou non (avec ExistValue) ExistValue --> si le champ existe ou non (booleen) VaultName --> nom du vault VaultValueLogin --> nom du login mongo dans le vault VaultValuePwd --> nom du password mongo dans le vault .EXAMPLE $connctionString = "mongodb+srv://prod[...].azure.mongodb.net" $ParamFindMongo = @{ ConnectionString = $connctionString Database = "YPN_cache" Collection = "users" field = "name" value = "TRENCHARD" field2 = "domain" value2 = "FORMALOOP" ExistField = "active" ExistValue = $true VaultName = "SEC[...]KVT004" VaultValueLogin = "common-mongouser" VaultValuePwd = "common-mongopassword" } FindMongo @ParamFindMongo OU $ParamFindMongo = @{ ConnectionString = $ConnectionString Database = "YPN_cache" Collection = "users" MongoRequestFilter = "{ login: 'ptrenchard@VegaShell.com', currency: 'EUR', 'roles.name': 'cabinet' }" VaultName = "SEC[...]KVT004" VaultValueLogin = "common-mongouser" VaultValuePwd = "common-mongopassword" } FindMongo @ParamFindMongo #> Param( [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true, HelpMessage = "mongodb+srv://[....].azure.mongodb.net")] $ConnectionString, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] $Database, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$Collection, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $false)] [string]$field, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $false)] [string]$value, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $false)] [string]$field2, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $false)] [string]$value2, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $false)] [string]$ExistField, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $false)][ValidateSet($true, $false)] [boolean]$ExistValue, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $false, HelpMessage = "filtre MongoDB ex: { domain: 'VegashellCoproration', service: 'loophubcollaboratif', 'userData.roleName': 'client' } ")] [string]$MongoRequestFilter, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultName, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultValueLogin, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultValuePwd ) begin { try { Import-Module Mdbc | Out-Null Write-Verbose "Modules Installed and Imported succesfully" #connection a l'instance prod/int voulu, sur la base $ParamConnectMongoDB = @{ ConnectionString = $ConnectionString MongoDBUsername = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue $VaultValueLogin) MongoDBPassword = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue $VaultValuePwd -EncryptOutput) Database = $Database } $ConnectDatabase = connectMongoDb @ParamConnectMongoDB Write-Information "Connexion a l'instance reussi" #Get instance id from hostname and instance $InstanceCollection = Get-MdbcCollection -Name $Collection -Database $ConnectDatabase Write-Information "connexion a la collection reussi" } catch { TryCatchError } } process { try { Write-Information "Recuperation des documents" #if si 2 filtre if ($field2) { #if si field exist specifié if ($ExistField) { $ExistValue = [System.Convert]::ToBoolean($ExistValue) $ServerFilter = [Hashtable]@{ $field = $value; $field2 = $value2; $ExistField = @{ '$exists' = $ExistValue } } } else { $ServerFilter = [Hashtable]@{ $field = $value; $field2 = $value2 } } } elseif ($MongoRequestFilter) { $ServerFilter = $MongoRequestFilter } else { #if si field exist specifié if ($ExistField) { $ExistValue = [System.Convert]::ToBoolean($ExistValue) $ServerFilter = [Hashtable]@{ $field = $value; $ExistField = @{ '$exists' = $ExistValue } } } else { $ServerFilter = [Hashtable]@{ $field = $value } } } $FindValue = Get-MdbcData -Collection $InstanceCollection -Filter $ServerFilter | ConvertTo-Json Write-Information "Recuperation du/des document(s) reussi" } catch { TryCatchError } } end { try { # si resultat vide alors erreur if ($null -eq $FindValue) { Write-Information "Resultat Find vide" $Find = @{state = 0; value = "Resultat Find vide"} } else { $Find = @{state = 1; value = ($FindValue | ConvertFrom-Json)} return $Find } } catch { tryCatchError } } } function UpdateMongo { <# .SYNOPSIS Cette fonction sert a faire un update dans mongo db qui pourra etre appelée dans d'autre scripts .DESCRIPTION ConnectionString --> environnement de destination $Database --> base de données $Collection --> collection $field --> champs de recherche $value --> valeur du champs recherché $update --> champ a changé et valeur a affecter $many --> Determine si la recherche peu update plusieurs resultat ou un seul (le 1er de la liste si s'en est une) $Unset --> Choisir de SET ou UNSET (modifier ou supprimer) VaultName --> nom du vault VaultValueLogin --> nom du login mongo dans le vault VaultValuePwd --> nom du password mongo dans le vault .EXAMPLE Pour passer le champs accepte a la valeur booleen $false sur le document dont le champ rib est égal à $rib = '180250020004720293721' UpdateMongo -Instances MongoIntBigdata -Database "YPN_bigdata" -Collection "ribIban_paul" -field "rib" -value $rib -update @{accepte = $false} ou $ParamUpdateMongo = @{ ConnectionString = $connctionString Database = "YPN_cache" Collection = "customClasses_Paul20221017" field = "id" value = "d3d8f78a-2538-4dbe-a47b-4576b5977b50" update = @{"slots.axeAna$axe" = 1} VaultName = "SEC[...]KVT004" VaultValueLogin = "common-mongouser" VaultValuePwd = "common-mongopassword" } UpdateMongo @ParamUpdateMongo -Unset #> Param( [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true, HelpMessage = "mongodb+srv://[....].azure.mongodb.net")] [string]$ConnectionString, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$Database, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$Collection, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$field, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$value, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [hashtable]$update, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultName, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultValueLogin, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultValuePwd, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $false)] [switch]$Unset, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $false)] [switch]$Many ) begin { try { Import-Module Mdbc | Out-Null Write-Verbose "Modules Installed and Imported succesfully" #connection a l'instance prod/int voulu, sur la base $ParamConnectMongoDB = @{ ConnectionString = $ConnectionString MongoDBUsername = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue $VaultValueLogin) MongoDBPassword = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue $VaultValuePwd -EncryptOutput) DataBaseName = $Database } $ConnectDatabase = connectMongoDb @ParamConnectMongoDB Write-Information "Connexion a l'instance reussi" #Get instance id from hostname and instance $InstanceCollection = Get-MdbcCollection -Name $Collection -Database $ConnectDatabase Write-Information "connexion a la collection reussi" $ServerFilter = @{ $field = $value } } catch { TryCatchError } } process { #recupere le ou les documents mongo voulu try { if ($Many) { if ($unset) { $UpdateValue = Update-MdbcData -Collection $InstanceCollection -Filter $ServerFilter @{'$unset' = $update } -Result -Many | ConvertTo-Json } else { $UpdateValue = Update-MdbcData -Collection $InstanceCollection -Filter $ServerFilter @{'$set' = $update } -Result -Many | ConvertTo-Json } } else { if ($unset) { $UpdateValue = Update-MdbcData -Collection $InstanceCollection -Filter $ServerFilter @{'$unset' = $update } -Result | ConvertTo-Json } else { $UpdateValue = Update-MdbcData -Collection $InstanceCollection -Filter $ServerFilter @{'$set' = $update } -Result | ConvertTo-Json } } Write-Information "Update du document reussi" Write-Information "" } catch { Write-Warning "Erreur de 'update' (mauvaise -collection?)" TryCatchError } } end { # si resultat vide alors erreur if ($null -eq $UpdateValue) { Write-Information "Resultat Update vide" $Update = @{state = 0; value = "Resultat Update vide"} return $Update } else { $Update = @{state = 1; value = $UpdateValue} return $Update | ConvertFrom-Json } } } function InsertMongo { <# .SYNOPSIS Cette fonction sert a faire un insert dans mongo db qui pourra etre appele dans d'autre scripts .DESCRIPTION ConnectionString --> environnement de destination $Database --> base de données $Collection --> collection $document, --> document a importe au format tableau @{}, exemple le $Find de la function FUNC_MONGO_FIND.ps1 VaultName --> nom du vault VaultValueLogin --> nom du login mongo dans le vault VaultValuePwd --> nom du password mongo dans le vault .EXAMPLE $ParamInsertMongo = @{ ConnectionString = $connctionString Database = "YPN_cache" Collection = "customClasses_copyPaul" Document = $variableFind VaultName = "SEC[...]KVT004" VaultValueLogin = "common-mongouser" VaultValuePwd = "common-mongopassword" } InsertMongo @ParamInsertMongo #> Param( [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true, HelpMessage = "mongodb+srv://[....].azure.mongodb.net")] [string]$ConnectionString, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$Database, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$Collection, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] $document, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultName, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultValueLogin, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultValuePwd ) begin { try { Import-Module Mdbc | Out-Null Write-Verbose "Modules Installed and Imported succesfully" #connection a l'instance prod/int voulu, sur la base $ParamConnectMongoDB = @{ ConnectionString = $ConnectionString MongoDBUsername = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue $VaultValueLogin) MongoDBPassword = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue $VaultValuePwd -EncryptOutput) DataBaseName = $Database } $ConnectDatabase = connectMongoDb @ParamConnectMongoDB Write-Information "Connexion a l'instance reussi" #Get instance id from hostname and instance $InstanceCollection = Get-MdbcCollection -Name $Collection -Database $ConnectDatabase Write-Information "connexion a la collection reussi" } catch { TryCatchError } } process { #Insert du / des documents Write-Information "Insert du document en cours" try { Add-MdbcData -Collection $InstanceCollection $document Write-Information "Insert du document reussi" $Insert = @{state = 1; value = "Document inséré"} } catch { Write-Warning "Insert du document en erreur ou deja present" $Insert = @{state = 0; value = "Insert du document en erreur ou deja present"} TryCatchError -continue } } end { return $Insert } } function DeleteMongo { <# .SYNOPSIS Cette fonction sert a Delete un document dans mongodb qui pourra etre appelé dans d'autre scripts, elle ne fait pas de Backup .DESCRIPTION ConnectionString --> environnement de destination $Database --> base de données $Collection --> collection $field --> champs de recherche $value --> valeur du champs recherché VaultName --> nom du vault VaultValueLogin --> nom du login mongo dans le vault VaultValuePwd --> nom du password mongo dans le vault .EXAMPLE DeleteMongo -Instances MongoProdcache -Database YPN_cache -Collection databases_20210910 -field id -value dba78816-a6d9-46cc-b0f2-9105f0e0f739 #> Param( [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true, HelpMessage = "mongodb+srv://[....].azure.mongodb.net")] [string]$ConnectionString, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$Database, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$Collection, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$field, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$value, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultName, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultValueLogin, [ValidateNotNullOrEmpty()] [Parameter(Mandatory = $true)] [string]$VaultValuePwd ) begin { try { Import-Module Mdbc | Out-Null Write-Verbose "Modules Installed and Imported succesfully" #connection a l'instance prod/int voulu, sur la base $ParamConnectMongoDB = @{ ConnectionString = $ConnectionString MongoDBUsername = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue $VaultValueLogin) MongoDBPassword = (GetKeyVault -SecretVaultName $VaultName -SecretVaultNameValue $VaultValuePwd) DataBaseName = $Database } $ConnectDatabase = connectMongoDb @ParamConnectMongoDB Write-Information "Connexion a l'instance reussi" #Get instance id from hostname and instance $InstanceCollection = Get-MdbcCollection -Name $Collection -Database $ConnectDatabase Write-Information "connexion a la collection reussi" $ServerFilter = @{ $field = $value } } catch { TryCatchError } } process { #Delete du / des documents Write-Information "suppression du document en cours" try { $ServerFilter = [Hashtable]@{ $field = $value } $DeleteValue = Remove-MdbcData -Collection $InstanceCollection $ServerFilter -Result Write-Information "suppression du document reussi" } catch { Write-Warning "Suppression du document en erreur" TryCatchError } } end { # si resultat vide alors erreur if ($null -eq $DeleteValue) { Write-Information "Resultat Delete vide" $Delete = @{state = 0; value = "Resultat Delete vide"} } else { $Delete = @{state = 1; value = $DeleteValue} return $Delete } } } |