I have a PowerShell Script that will invoke a command on a remote server. I'm trying to set this script up so that I can pass in a service parameter and it will drop that specific table in MongoDB
$service = "DatabaseName"
$username = "username"
$password = "password"
$pass = ConvertTo-SecureString -AsPlainText $password -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $pass
Invoke-Command -ComputerName Remote-Server1 -Credential $cred -ArgumentList $service -ScriptBlock {
param($service)
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' $service --eval 'db.dropDatabase()'
}
Invoke-Command -ComputerName Remote-Server2 -Credential $cred -ArgumentList $service -ScriptBlock {
param($service)
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' $service --eval 'db.dropDatabase()'
}
If I were to use the following it does not drop the MongoDB database:
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' $service --eval 'db.dropDatabase()'
However, If I use the following it works as expected:
& 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' DatabaseName --eval 'db.dropDatabase()'
Why does it work when I hard code that database name but not when I use a variable.