0

I am trying to create a powershell script which will create a session in a remote machine and run a series of commands. These commands in question are to drop a Mongodb database before deployment of code.

I have the session side working but when I try to run the cmd I get X is not recognized as the name of a cmdlet.

The process I take when I am logged into the remote machine and using cmd is:

  1. 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe'
  2. use <database>
  3. db.dropDatabase()

This works correctly and I am trying to run those in powershell. They need to be run line by line in order to work.

ps1:

$session = New-PSSession -ComputerName "remoteMachine" -Credential $cred

Enter-PSSession -Session $session

Invoke-Command -ComputerName "remoteMachine" -ScriptBlock {
    & 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' 
    & 'use <database>'
    & 'db.dropDatabase()'
}

Exit-PSSession
Get-PSSession | Remove-PSSession

When this is run I am getting the following errors:

The term 'use Assessment' is not recognized as the name of a cmdlet,

The term 'db.dropDatabase()' is not recognized as the name of a cmdlet,

1 Answer 1

1

I managed to figure it out by looking through & 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' --help.

Rather than using multiple lines, I put the command on one line like:

Invoke-Command -ComputerName "remoteMachine" -ScriptBlock {
    & 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' <database> --eval '<action>'
}

For example:

Invoke-Command -ComputerName "remoteMachine" -ScriptBlock {
    & 'C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe' testDatabase --eval 'db.dropDatabase()'
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.