Powershell Module

Hi everyone ,
I am not an experienced programmer but i love Mikrotik and Powershell , so here is my simple mikrotik powershell cmdlet module
i hope you like it , Please let me know if you think we can improve it

it supports API and API-SSL , for more information about Filters and Attributes please check http://wiki.mikrotik.com/wiki/Manual:API

Thank you

Exmaples:

PS C:>Import-Module Mikrotik.dll
PS C:> Get-Command -Module Mikrotik

CommandType Name Version Source


Cmdlet Connect-Mikrotik 1.0.0.0 Mikrotik
Cmdlet Disconnect-Mikrotik 1.0.0.0 Mikrotik
Cmdlet Send-Mikrotik 1.0.0.0 Mikrotik

#Connecting using API default port and save the connection object in C$ then we can use C$ to send commands
PS C:> $C=Connect-Mikrotik -IPaddress 192.168.3.1 -UserName admin -Password password
Connected to 192.168.3.1 , Identity=TEST1

#Connecting using API-SSL default port
PS C:> $C=Connect-Mikrotik -IPaddress 192.168.3.1 -UserName admin -Password password -UseSSL
Connected to 192.168.3.1 , Identity=TEST1

#Connecting using API-SSL on diffrenet port that 8729
PS C:> $C=Connect-Mikrotik -IPaddress 192.168.3.1 -UserName admin -Password svgafara -UseSSL -Port 3323

#get all ethernet interface which is not disabled

PS C:> Send-Mikrotik -Connection $C -Command “/interface/getall” -Filters “type=ether”,“disabled=false”

#add ip to ether 1
PS C:> Send-Mikrotik -Connection $C -Command “/ip/address/add” -Attributes “interface=ether1”,“address=192.168.201.1/24”

#get ether1 ip addres
PS C:> Send-Mikrotik -Connection $C -Command “/ip/address/getall” -Filters “interface=ether1”

#Close the connection
PS C:> Disconnect-Mikrotik -Connection $C
Mikrotik-PS-CMDLET-SRC.zip (29.6 KB)
Mikrotik-PS-Module.zip (5.39 KB)

Thanks mate, I was just thinking about something like this myself. :slight_smile:

thanks, added link in the API page to your creation.

Hello! I want to say thank you for your module.
And I ask how I can use the commands “set” and “remove” with this module?

Windows Server 2012 PS:
import-module d:\mikrotik.dll
error: HRESULT: 0x80131515
What can i do with it?
Can you help me please?

@milan63 -
Try right click and say unblock.

  • That should do the job

@navidrasi -
Would you consider open-sourcing and posting the code on GitHub?

@mitchellmnr-
Perfect
Thank You !!!

Sorry to bump an old thread but I’m wondering how to run Torch through this module. I just need the first result Torch gives me and then end the command. I tried running the following:

Import-Module –Name "C:\location\of\Mikrotik.dll" -Verbose
$C=Connect-Mikrotik -IPaddress ip.of.mikrotik -UserName admin -Password ******
Send-Mikrotik -Connection $C -Command "/tool/torch" -Attributes "interface=bridge1" >torchresults.txt
Disconnect-Mikrotik -Connection $C

It returns

Connected to ip.of.mikrotik , Identity=MikrotikIdentity

then locks up. I am unable to even Ctrl+Break out of the code. I’ve verified this command works in the CLI of the Mikrotik.

Any help would be greatly appreciated!

hello there

i have problem with this powershell script
cant gave count-only to the script !
i just want to have the number.
many thanks for your attention
kareem

Hello !

Module is great and helped me a lot :slight_smile: works like a charm !
But I have the same issue while trying to get count only to the script :frowning:
By the way, have you considered putting the source of module on GitHub and allow e.g us to contribute with issue, bugs resolving and improving it ? :wink: - would be great for me.

Thanks !

After 6.45.1 update previous API authorization method is deprecated and dll is not working :frowning:
Can you update dll with new auth method?

In accordance with the API manual change source code in this function to:
public bool Login(string username, string password)
{
Send(“/login”);
Send(“=name=” + username);
Send(“=password=” + password, true);
if (Read()[0] == “!done”)
{
return true;
}
else
{
return false;
}
}
In attach - source and dll
Mikrotik-PS-Module-SRC.rar (153 KB)
Mikrotik-PS-Module.zip (4.35 KB)

Hey,

flydvorkin thanks a lot ! Have you planned to add it on git as a fork to let us work on this or report issues/bugs etc ?
I use API quite lot and would appreciate it :wink:

Thanks

Hi.
I don’t know, i’m not C-programmer, just only find and fix auth procedure in accordance with official API manual.

Hello everyone
Mr. Navidrasi
I have modified the module you created to match the new login method in Mikrotik Router-OS (post-v6.43).

NOTE: I live in Syria and i don’t know if this Modification is legal or not
In case that the modification is illegal please email me to remove it.
my mail is: kareem.kh95@outlook.com

Thanks :slight_smile:
Mikrotik-PS-Module(post v6.43 Login Fixed).zip (4.59 KB)

Hello,

thanks you all for this Mikrotik powershell module.

I need one advice from you - I need to control routerboard without password.
But how can I do it from powershell??

PS C:\Temp\Mikrotik\moduly\new> $Login=Connect-Mikrotik -IPaddress 192.168.30.1 -UserName admin

cmdlet Connect-Mikrotik at command pipeline position 1
Supply values for the following parameters:
Password:
Connect-Mikrotik : Cannot bind argument to parameter ‘Password’ because it is an empty string.
At line:1 char:8

  • $Login=Connect-Mikrotik -IPaddress 192.168.30.1 -UserName admin
  •    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  • CategoryInfo : InvalidData: (:slight_smile: [Connect-Mikrotik], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Mikrotik.Connect_Mikrotik


    Thank you a lot,
    Rostislav

I have the same problem as ronuron.

I need to configure the factory routers, which come without a password and give me the same error

“Connect-Mikrotik : No se puede enlazar el argumento con el parámetro ‘Password’ porque es una cadena vacía. En C:\Mikrotik\script.ps1: 8 Carácter: 71 + … krotik -IPaddress 192.168.88.1 -UserName $Usuario -Password $Password + ~~~~~~~~~ + CategoryInfo : InvalidData: (:slight_smile: [Connect-Mikrotik], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Mikrotik.Connect_Mikrotik” I need the configuration to be for clumsy as I will not be the person who will do it, but it will be done by unqualified personnel and I need it to be done with just a couple of clicks, it does not serve me to use winbox and restore a backup as it can be 800 routers in total.

I have the same problem as ronuron.

I need to configure the factory routers, which come without a password and give me the same error

“Connect-Mikrotik : No se puede enlazar el argumento con el parámetro ‘Password’ porque es una cadena vacía. En C:\Mikrotik\script.ps1: 8 Carácter: 71 + … krotik -IPaddress 192.168.88.1 -UserName $Usuario -Password $Password + ~~~~~~~~~ + CategoryInfo : InvalidData: (:slight_smile: [Connect-Mikrotik], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Mikrotik.Connect_Mikrotik”

I need the configuration to be for clumsy as I will not be the person who will do it, but it will be done by unqualified personnel and I need it to be done with just a couple of clicks, it does not serve me to use winbox and restore a backup as it can be 800 routers in total.

Thanks in advance

Hello all,

how can i disable a IP from address-list? or enable?

thanks :slight_smile:

I am trying to start a script using this module:

Send-Mikrotik -Connection $C -Command “/system/script/run” …

I can’t figure out how to correctly pass the script number or name.
Whatever I do, I get “script not found” or “no such item”.

Please help.

There is no such thing as mikrotik.dll