#Requires -RunAsAdministrator function CheckMongoVersion { if (Test-Path "$env:ProgramFiles\MongoDB\Server\8.0\bin\mongod.exe") { Write-Host "MongoDB 8.x is installed." return "8.0" } elseif (Test-Path "$env:ProgramFiles\MongoDB\Server\6.0\bin\mongod.exe") { Write-Host "MongoDB 6.x is installed." return "6.0" } else { Write-Host "MongoDB is not installed." return $false } } function DownloadMongoShell { Write-Output "Downloading the Mongo Shell from the MongoDB website" $URL="https://downloads.mongodb.com/compass/mongosh-2.5.6-x64.msi" $fPath="$env:TEMP\mongosh-2.5.6-x64.msi" Invoke-WebRequest -URI $URL -OutFile $fPath Start-Process -FilePath msiexec.exe -ArgumentList "/i $fPath /quiet ALLUSERS=1" -Wait write-host "Sleeping for 10 seconds to let the system do what it must" Start-Sleep -Seconds 10 $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") $mongoShell = Test-Path "$env:ProgramFiles\mongosh\mongosh.exe" if ($mongoShell) { Write-Host "Mongo Shell installed successfully." } else { Write-Host "Mongo Shell installation failed." } } $mongoShell = Test-Path "$env:ProgramFiles\mongosh\mongosh.exe" if ($mongoShell) { Write-Host "Mongo Shell is already installed." } else { Write-Host "Mongo Shell is not installed. Installing now..." DownloadMongoShell } $installedVersion = CheckMongoVersion #This Creates variables and the config file that is used in dropping tables $mongodServerFolder = [Environment]::GetFolderPath("ProgramFiles")+"\MongoDB\Server\$installedVersion\bin\"; $mongodCfg = [Environment]::GetFolderPath("ProgramFiles")+"\MongoDB\Server\$installedVersion\bin\mongod.cfg"; $configjs= New-Item -path $mongodServerFolder -type file -name "mongoscript.js" Add-Content $configjs "db = connect( 'mongodb://localhost/smartdeploydb' );" Add-Content $configjs "db.AccountAuthentication.drop( { writeConcern: { w: 0 } } )" Add-Content $configjs "exit" $currentCfg= Get-Content -Path $mongodCfg #If auth is already disabled, this will just run the script if($currentCfg -like "*authorization: disabled*"){ Write-Host "Auth is already disabled, running script" mongosh -f $configjs Write-Output "You are good to go! Auth is dropped" }else { Write-Host "Have to disable MongoAuth real quick" (Get-Content -path $mongodCfg) -replace 'authorization: enabled','authorization: disabled'| Set-Content -Path $mongodCfg net stop mongodb net start mongodb Write-Host "Dropping the Auth Table" mongosh -f $configjs Write-Host "Turning the auth back on and rebooting the service" (Get-Content -path $mongodCfg) -replace 'authorization: disabled','authorization: enabled'| Set-Content -Path $mongodCfg net stop mongodb net start mongodb Write-Output "You are good to go! Auth is dropped" } Write-Output "Removing the JS config file"; Remove-Item $configjs Remove-Item -Path $env:ProgramFiles\SmartDeploy\SmartDeploy\Resources\ -Force -Recurse read-host Press ENTER to Finish...