+ "Octopus.Action.Script.ScriptBody": "# Define functions\nfunction Get-ModuleInstalled\n{\n # Define parameters\n param(\n $PowerShellModuleName\n )\n\n # Check to see if the module is installed\n if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName))\n {\n # It is installed\n return $true\n }\n else\n {\n # Module not installed\n return $false\n }\n}\n\nfunction Install-PowerShellModule\n{\n # Define parameters\n param(\n $PowerShellModuleName,\n $LocalModulesPath\n )\n\n\t# Check to see if the package provider has been installed\n if ((Get-NugetPackageProviderNotInstalled) -ne $false)\n {\n \t# Display that we need the nuget package provider\n Write-Host \"Nuget package provider not found, installing ...\"\n \n # Install Nuget package provider\n Install-PackageProvider -Name Nuget -Force\n }\n\n $moduleParameters = @{\n Name = $PowerShellModuleName\n Path = $LocalModulesPath\n Force = $true\n }\n\n # Check the version of PowerShell\n if ($PSVersionTable.PSVersion.Major -lt 7)\n {\n # Add specific version of powershell module to use\n $moduleParameters.Add(\"MaximumVersion\", \"6.7.4\")\n }\n\n\t# Save the module in the temporary location\n Save-Module @moduleParameters\n}\n\nfunction Get-NugetPackageProviderNotInstalled\n{\n\t# See if the nuget package provider has been installed\n return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))\n}\n\nfunction Get-DatabaseUserExists\n{\n\t# Define parameters\n param ($UserName)\n \n # Define working variables\n $userExists = $false\n \n\t# Get users for database\n $command = @\"\n{ usersInfo: 1 }\n\"@\n\n\t$results = Invoke-MdbcCommand -Command $command\n $users = $results[\"users\"]\n \n # Loop through returned results\n foreach ($user in $users)\n {\n \tif ($user[\"user\"] -eq $UserName)\n {\n \treturn $true\n }\n }\n \n return $false\n}\n\n# Define PowerShell Modules path\n$LocalModules = (New-Item \"$PSScriptRoot\\Modules\" -ItemType Directory -Force).FullName\n$env:PSModulePath = \"$LocalModules$([System.IO.Path]::PathSeparator)$env:PSModulePath\"\n$PowerShellModuleName = \"Mdbc\"\n\n# Set secure protocols\n[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12\n\n# Check to see if SimplySql module is installed\nif ((Get-ModuleInstalled -PowerShellModuleName $PowerShellModuleName) -ne $true)\n{\n # Tell user what we're doing\n Write-Output \"PowerShell module $PowerShellModuleName is not installed, downloading temporary copy ...\"\n\n # Install temporary copy\n Install-PowerShellModule -PowerShellModuleName $PowerShellModuleName -LocalModulesPath $LocalModules\n}\n\n# Display\nWrite-Output \"Importing module $PowerShellModuleName ...\"\n\n# Check to see if it was downloaded\nif ((Test-Path -Path \"$LocalModules\\$PowerShellModuleName\") -eq $true)\n{\n\t# Use specific location\n $PowerShellModuleName = \"$LocalModules\\$PowerShellModuleName\"\n}\n\n# Import the module\nImport-Module -Name $PowerShellModuleName\n\n# Connect to mongodb instance\n$connectionUrl = \"mongodb://$($MongoDBAdminUsername):$($MogoDBAdminUserpassword)@$($MongoDBServerName):$($MongoDBPort)\"\n\n# Connect to MongoDB server\nConnect-Mdbc $connectionUrl $MongoDBDatabaseName\n\n# Get whether the database exits\nif ((Get-DatabaseUserExists -UserName $MongoDBUsername) -eq $true)\n{\n\t# Create user\n Write-Output \"Adding $MongoDBRoles to $MongoDBUsername.\"\n \n # Create Roles array for adding\n $roles = @()\n foreach ($MongoDBRole in $MongoDBRoles.Split(\",\"))\n {\n \t$roles += @{\n \trole = $MongoDBRole.Trim()\n db = $MongoDBDatabaseName\n }\n }\n\n # Define create user command\n $command = @\"\n{\n\tupdateUser: `\"$MongoDBUsername`\" \n roles: $(ConvertTo-Json $roles)\n}\n\"@\n\n\t# Create user account\n $result = Invoke-MdbcCommand -Command $command\n \n # Check to make sure it was created successfully\n if ($result.ContainsKey(\"ok\"))\n {\n \tWrite-Output \"Successfully added role(s) $MongoDBRoles to $MongoDBUsername in database $MongoDBDatabaseName.\"\n }\n else\n {\n \tWrite-Error \"Failed, $result\"\n }\n}\nelse\n{\n\tWrite-Error \"Unable to add role(s) to $MongoDBUsername, user does not exist in $MongoDBDatabaseName.\"\n}\n\n\n\n\n\n\n"
0 commit comments