From 6df6f1cdf05b173ad488f5c17cf1c13ea144c25b Mon Sep 17 00:00:00 2001 From: twerthi Date: Mon, 28 Oct 2024 14:15:19 -0700 Subject: [PATCH] Fixing bug in mongodb templates --- step-templates/mongodb-add-update-user-roles.json | 10 +++++----- step-templates/mongodb-create-database.json | 12 ++++++------ step-templates/mongodb-create-user.json | 10 +++++----- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/step-templates/mongodb-add-update-user-roles.json b/step-templates/mongodb-add-update-user-roles.json index 26db9793c..e987de346 100644 --- a/step-templates/mongodb-add-update-user-roles.json +++ b/step-templates/mongodb-add-update-user-roles.json @@ -3,13 +3,13 @@ "Name": "MongoDB - Add or update user roles ", "Description": "Adds roles to an existing user in a MongoDB database.", "ActionType": "Octopus.Script", - "Version": 2, + "Version": 3, "Author": "twerthi", "Packages": [], "Properties": { "Octopus.Action.Script.ScriptSource": "Inline", "Octopus.Action.Script.Syntax": "PowerShell", - "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\t# Save the module in the temporary location\n Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force\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" + "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" }, "Parameters": [ { @@ -84,10 +84,10 @@ } ], "$Meta": { - "ExportedAt": "2020-12-02T20:31:19.166Z", - "OctopusVersion": "2020.5.0", + "ExportedAt": "2024-10-28T21:13:02.226Z", + "OctopusVersion": "2024.3.12899", "Type": "ActionTemplate" }, "LastModifiedBy": "twerthi", "Category": "mongodb" - } \ No newline at end of file + } diff --git a/step-templates/mongodb-create-database.json b/step-templates/mongodb-create-database.json index c040dc0f5..dddd41d84 100644 --- a/step-templates/mongodb-create-database.json +++ b/step-templates/mongodb-create-database.json @@ -3,13 +3,13 @@ "Name": "MongoDB - Create Database if not exists", "Description": "Creates a new database on a MongoDB server with an initial collection.", "ActionType": "Octopus.Script", - "Version": 3, + "Version": 4, "Author": "twerthi", "Packages": [], "Properties": { "Octopus.Action.Script.ScriptSource": "Inline", "Octopus.Action.Script.Syntax": "PowerShell", - "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\t# Save the module in the temporary location\n Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force\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-DatabaseExists\n{\n\t# Define parameters\n param ($DatabaseName)\n \n\t# Execute query\n $mongodbDatabases = Get-MdbcDatabase\n \n return $mongodbDatabases.DatabaseNamespace -contains $DatabaseName\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://$($MongoDBUsername):$($MogoDBUserpassword)@$($MongoDBServerName):$($MongoDBPort)\"\n\n# Connect to MongoDB server\nConnect-Mdbc $connectionUrl \"admin\"\n\n# Get whether the database exits\nif ((Get-DatabaseExists -DatabaseName $MongoDBDatabaseName) -ne $true)\n{\n\t# Create database\n Write-Output \"Database $MongoDBDatabaseName doesn't exist.\"\n Connect-Mdbc $connectionUrl \"$MongoDBDatabaseName\"\n \n # Databases don't get created unless some data has been added\n Add-MdbcCollection $MongoDBInitialCollection\n \n # Check to make sure it was successful\n if ((Get-DatabaseExists -DatabaseName $MongoDBDatabaseName))\n {\n \t# Display success\n Write-Output \"$MongoDBDatabaseName created successfully.\" \n }\n else\n {\n \tWrite-Error \"Failed to create $MongoDBDatabaseName!\"\n }\n}\nelse\n{\n\tWrite-Output \"Database $MongoDBDatabaseName already exists.\"\n}\n\n\n\n\n\n\n" + "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-DatabaseExists\n{\n\t# Define parameters\n param ($DatabaseName)\n \n\t# Execute query\n $mongodbDatabases = Get-MdbcDatabase\n \n return $mongodbDatabases.DatabaseNamespace -contains $DatabaseName\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://$($MongoDBUsername):$($MogoDBUserpassword)@$($MongoDBServerName):$($MongoDBPort)\"\n\n# Connect to MongoDB server\nConnect-Mdbc $connectionUrl \"admin\"\n\n# Get whether the database exits\nif ((Get-DatabaseExists -DatabaseName $MongoDBDatabaseName) -ne $true)\n{\n\t# Create database\n Write-Output \"Database $MongoDBDatabaseName doesn't exist.\"\n Connect-Mdbc $connectionUrl \"$MongoDBDatabaseName\"\n \n # Databases don't get created unless some data has been added\n Add-MdbcCollection $MongoDBInitialCollection\n \n # Check to make sure it was successful\n if ((Get-DatabaseExists -DatabaseName $MongoDBDatabaseName))\n {\n \t# Display success\n Write-Output \"$MongoDBDatabaseName created successfully.\" \n }\n else\n {\n \tWrite-Error \"Failed to create $MongoDBDatabaseName!\"\n }\n}\nelse\n{\n\tWrite-Output \"Database $MongoDBDatabaseName already exists.\"\n}\n\n\n\n\n\n\n" }, "Parameters": [ { @@ -74,11 +74,11 @@ } ], "$Meta": { - "ExportedAt": "2020-12-02T20:27:29.307Z", - "OctopusVersion": "2020.5.0", + "ExportedAt": "2024-10-28T21:07:02.249Z", + "OctopusVersion": "2024.3.12899", "Type": "ActionTemplate" }, "LastModifiedOn": "2021-07-26T16:50:00.000+00:00", - "LastModifiedBy": "bobjwalker", + "LastModifiedBy": "twerthi", "Category": "mongodb" - } \ No newline at end of file + } diff --git a/step-templates/mongodb-create-user.json b/step-templates/mongodb-create-user.json index 9160c6dbc..0206047cc 100644 --- a/step-templates/mongodb-create-user.json +++ b/step-templates/mongodb-create-user.json @@ -3,13 +3,13 @@ "Name": "MongoDB - Create User if not exists", "Description": "Creates a new database user on a MongoDB server.", "ActionType": "Octopus.Script", - "Version": 2, + "Version": 3, "Author": "twerthi", "Packages": [], "Properties": { "Octopus.Action.Script.ScriptSource": "Inline", "Octopus.Action.Script.Syntax": "PowerShell", - "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\t# Save the module in the temporary location\n Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force\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) -ne $true)\n{\n\t# Create user\n Write-Output \"User $MongoDBUsername doesn't exist in database $MongoDBDatabaseName.\"\n \n # Define create user command\n $command = @\"\n{\n\tcreateUser: `\"$MongoDBUsername`\"\n pwd: `\"$MongoDBUserPassword`\"\n 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 \"User $MongoDBUsername successfully created in database $MongoDBDatabaseName.\"\n }\n else\n {\n \tWrite-Error \"Failed, $result\"\n }\n}\nelse\n{\n\tWrite-Output \"User $MongoDBUsername already exists in database $MongoDBDatabaseName.\"\n}\n\n\n\n\n\n\n" + "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) -ne $true)\n{\n\t# Create user\n Write-Output \"User $MongoDBUsername doesn't exist in database $MongoDBDatabaseName.\"\n \n # Define create user command\n $command = @\"\n{\n\tcreateUser: `\"$MongoDBUsername`\"\n pwd: `\"$MongoDBUserPassword`\"\n 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 \"User $MongoDBUsername successfully created in database $MongoDBDatabaseName.\"\n }\n else\n {\n \tWrite-Error \"Failed, $result\"\n }\n}\nelse\n{\n\tWrite-Output \"User $MongoDBUsername already exists in database $MongoDBDatabaseName.\"\n}\n\n\n\n\n\n\n" }, "Parameters": [ { @@ -84,10 +84,10 @@ } ], "$Meta": { - "ExportedAt": "2020-12-02T20:29:45.311Z", - "OctopusVersion": "2020.5.0", + "ExportedAt": "2024-10-28T21:10:17.114Z", + "OctopusVersion": "2024.3.12899", "Type": "ActionTemplate" }, "LastModifiedBy": "twerthi", "Category": "mongodb" - } \ No newline at end of file + }