Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script Resource Issue
Script MongoDBAdminConfiguration
{
SetScript =
{
import-module gac
Add-Type -AssemblyName (Get-GACAssembly "MongoDB.Bson")
Add-Type -AssemblyName (Get-GACAssembly "MongoDB.Driver")
$client = new-object MongoDB.Driver.MongoClient("mongodb://localhost")
$server = $client.GetServer()
$adminDB = $server.GetDatabase("admin")
$username = "admin"
$password = new-object MongoDB.Driver.PasswordEvidence("blahblah")
$users = $adminDB.GetCollection("system.users")
$document = $users.FindOne([MongoDB.Driver.Builders.Query]::EQ("user", $username))
if ($document -eq $null)
{
$document = new-object MongoDB.Bson.BsonDocument("user", $username)
}
$document["pwd"] = [MongoDB.Driver.MongoUser]::HashPassword($username,$password)
$roles = new-object MongoDB.Bson.BsonArray
$roles.Add("userAdminAnyDatabase")
$document["roles"] = $roles
@("userAdminAnyDatabase")
$users.Save($document);
}
TestScript =
{
import-module gac
Add-Type -AssemblyName (Get-GACAssembly "MongoDB.Bson")
Add-Type -AssemblyName (Get-GACAssembly "MongoDB.Driver")
$client = new-object MongoDB.Driver.MongoClient("mongodb://localhost")
$server = $client.GetServer()
$adminDB = $server.GetDatabase("admin")
$users = $adminDB.GetCollection("system.users")
$returnvalue = $false
try
{
$document = $users.FindOne([MongoDB.Driver.Builders.Query]::EQ("user", "user"))
}
catch
{
$returnvalue = $true
}
return $returnvalue
}
GetScript =
{
@{"MongoDB"="Configured"}
}
Requires = @("[Package]MongoDBDriver","[Archive]PowerShellGAC","[Service]MongoDB")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.