Community discussions

MikroTik App
 
navidrasi
just joined
Topic Author
Posts: 9
Joined: Sun Sep 18, 2011 11:22 pm

Powershell Module

Wed Jun 01, 2016 8:48 pm

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
You do not have the required permissions to view the files attached to this post.
 
bitplumber
just joined
Posts: 1
Joined: Thu Jun 02, 2016 4:35 am

Re: Powershell Module

Tue Jun 14, 2016 12:58 pm

Thanks mate, I was just thinking about something like this myself.  :)
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Powershell Module

Tue Jun 14, 2016 4:00 pm

thanks, added link in the API page to your creation.
 
fsbl
just joined
Posts: 1
Joined: Thu Sep 01, 2016 2:36 pm

Re: Powershell Module

Thu Sep 01, 2016 2:40 pm

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?
 
milan63
just joined
Posts: 2
Joined: Sun Jan 08, 2017 9:58 pm

Re: Powershell Module

Sun Jan 08, 2017 10:08 pm

Windows Server 2012 PS:
import-module d:\mikrotik.dll
error: HRESULT: 0x80131515
What can i do with it?
Can you help me please?
 
User avatar
mitchellmnr
just joined
Posts: 6
Joined: Tue Mar 08, 2016 10:04 pm

Re: Powershell Module

Tue Mar 07, 2017 8:53 pm

@milan63 -
Try right click and say unblock.
* That should do the job

@navidrasi -
Would you consider open-sourcing and posting the code on GitHub?
 
milan63
just joined
Posts: 2
Joined: Sun Jan 08, 2017 9:58 pm

Re: Powershell Module

Wed May 24, 2017 7:20 pm

@mitchellmnr-
Perfect
Thank You !!!
 
nateingalls
just joined
Posts: 1
Joined: Thu Nov 09, 2017 5:35 pm

Re: Powershell Module

Thu Nov 09, 2017 6:16 pm

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!
 
vedodedo
just joined
Posts: 1
Joined: Sun Sep 30, 2018 3:12 pm

Re: Powershell Module

Sun Sep 30, 2018 3:16 pm

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
 
gbudny
just joined
Posts: 16
Joined: Tue Feb 09, 2016 10:57 am
Location: Poland, Katowice

Re: Powershell Module

Sun Jan 27, 2019 9:55 pm

Hello !

Module is great and helped me a lot :) works like a charm !
But I have the same issue while trying to get count only to the script :(
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 ? ;) - would be great for me.

Thanks !
 
flydvorkin
just joined
Posts: 15
Joined: Mon Mar 11, 2019 12:59 pm

Re: Powershell Module

Thu Jul 04, 2019 1:18 pm

After 6.45.1 update previous API authorization method is deprecated and dll is not working :-(
Can you update dll with new auth method?
 
flydvorkin
just joined
Posts: 15
Joined: Mon Mar 11, 2019 12:59 pm

Re: Powershell Module

Thu Jul 04, 2019 4:24 pm

In accordance with the API manual change source code in this function to:

Code: Select all

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
You do not have the required permissions to view the files attached to this post.
 
gbudny
just joined
Posts: 16
Joined: Tue Feb 09, 2016 10:57 am
Location: Poland, Katowice

Re: Powershell Module

Fri Jul 05, 2019 6:13 pm

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 ;)

Thanks
 
flydvorkin
just joined
Posts: 15
Joined: Mon Mar 11, 2019 12:59 pm

Re: Powershell Module

Tue Jul 23, 2019 4:55 pm

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 ;)
Hi.
I don't know, i'm not C-programmer, just only find and fix auth procedure in accordance with official API manual.
 
User avatar
KareemAlKhayat
just joined
Posts: 1
Joined: Tue Jan 21, 2020 10:59 am
Contact:

Re: Powershell Module

Mon Jun 22, 2020 5:16 pm

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 :)
You do not have the required permissions to view the files attached to this post.
 
ronuron
just joined
Posts: 1
Joined: Thu Jun 13, 2019 10:34 am

Re: Powershell Module

Tue Aug 11, 2020 10:48 am

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: (:) [Connect-Mikrotik], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Mikrotik.Connect_Mikrotik


Thank you a lot,
Rostislav
 
tecnidom
just joined
Posts: 1
Joined: Thu Aug 20, 2020 9:45 am

Re: Powershell Module

Fri Aug 21, 2020 9:39 am

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: (:) [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: (:) [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
 
dima1002
Member Candidate
Member Candidate
Posts: 160
Joined: Fri Jan 26, 2018 8:40 pm

Re: Powershell Module

Wed Sep 02, 2020 7:05 pm

Hello all,

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

thanks :-)
 
Jivo
newbie
Posts: 35
Joined: Wed Jun 08, 2011 11:44 pm

Re: Powershell Module

Tue Sep 06, 2022 3:17 pm

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.
 
manolito2112
just joined
Posts: 3
Joined: Wed Feb 28, 2018 11:55 pm

Re: Powershell Module

Wed Dec 07, 2022 6:49 pm

There is no such thing as mikrotik.dll
 
MrMattiPants
just joined
Posts: 4
Joined: Sat Mar 10, 2018 12:14 pm

Re: Powershell Module

Sat Jul 08, 2023 9:01 pm

There is no such thing as mikrotik.dll
You will need to Download the “MikroTik-PS-Module.zip”, located at the bottom of the OP (or simply Click the Link, I left below)

After extracting the ZIP Folder, you should see the resulting “MikroTik-PS-Module” Folder, which should contain the “MikroTik.dll” file, along with a “README.txt” File, containing Instructions, Examples, etc.

MikroTik-PS-Module.zip Download:
download/file.php?id=24687

If for some reason, you don’t see the “MikroTik.dll” File, in the Extracted Folder, I would check your Anti-Virus, to ensure that it hasn’t Quarantined the File, for some reason or another.

I should also note that Windows 10/11 may block DLL Files that have been Downloaded from the Internet. As a result, you may have to Unblock the File.

To accomplish this task, simply Right-Click on the “MikroTik.dll” File and Select the “Properties” from the Drop-Down/Context Menu.

Once the “Properties” Panel Opens, Select the “General” Tab and if you can see an “Unblock” Checkbox toward the bottom of the Panel, you’ll want to Check the box and Click the “Apply” Button, in the Bottom-Right of the Panel, to Unblock it.

I hope this explanation is helpful.
 
MrMattiPants
just joined
Posts: 4
Joined: Sat Mar 10, 2018 12:14 pm

Re: Powershell Module

Sat Jul 08, 2023 9:22 pm

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: (:) [Connect-Mikrotik], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Mikrotik.Connect_Mikrotik


Thank you a lot,
Rostislav
I would try it with an Empty Password String, as follows.
$Login=Connect-Mikrotik -IPaddress 192.168.30.1 -UserName admin -Password ""
If the Script above doesn't produce the expected results, you could try the following method.
$Password = ([string]::Empty)
$Login=Connect-Mikrotik -IPaddress 192.168.30.1 -UserName admin -Password $Password
Please, report back, to let us know whether one or the other (or both) worked, or not.
It would be greatly appreciated.
 
MrMattiPants
just joined
Posts: 4
Joined: Sat Mar 10, 2018 12:14 pm

Re: Powershell Module

Sat Jul 08, 2023 10:36 pm

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.
I experimented with the Script related Commands a bit and based on my findings, I am NOT able to find any evidence that the Author implemented the "/system/script/run" Command, into the MikroTik PowerShell Module.

I created a Script, named simply "test" (which was allocated the "id" of "*e"), which writes the word "Test" to the MikroTik Log, specifically for Testing.

From the MikroTik CLI, I had no problem running the following Commands, nor did I have any problems running their PowerShell equivalents.

MiktoTik CLI Command (Name):
:put [system script get test]
PowerShell Equivalent (Name):
Send-Mikrotik -Connection $C -Command "/system/script/getall" -Filters "name=test"
MiktoTik CLI Command (ID):
:put [system script get *e]
PowerShell Equivalent (ID):
Send-Mikrotik -Connection $C -Command "/system/script/getall" -Filters ".id=*E"
Output from MikroTik CLI & PowerShell Equivalent:

.id=*E=name=test=owner=admin11=policy=reboot,read,write,test,sniff,sensitive=last-started=jul/08/2023 13:50:36=run-count=2=source=log info test=invalid=false

When it came to Running the Script, I had No issues, when Running the following Commands from the MikroTik CLI.

MiktoTik CLI Command (Name):
system script run test
MiktoTik CLI Command (ID):
system script run *e
Unfortunately, the PowerShell Equivalents did Not work, at all.

PowerShell Equivalent (Name):
Send-Mikrotik -Connection $C -Command "/system/script/run" -Filters "name=test"
PowerShell Equivalent (ID):
Send-Mikrotik -Connection $C -Command "/system/script/run" -Filters ".id=*E"
Output from PowerShell Equivalent:
script not found

I tried several variations of the two PowerShell Scripts above, but each and every time, the "Script not found" message was returned.

On a positive note, I was able to find a couple Discussions, that do a decent job of explaining how calls are made to via MikroTik API, in relation to Running Scripts, etc.

Run scripts from API:
viewtopic.php?t=52477

Running a script from API:
viewtopic.php?t=40581

That being said, I plan on going through the Source Code for the MikroTik PowerShell Module, to determine whether the "/system/script/run" Command has been Implemented and if Not, I'll do what I can, to implement it, myself.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Powershell Module

Sun Jul 09, 2023 7:57 pm

Didn't you realize that whoever you answer to hasn't visited the forum for years and these are all old questions???

For example @tecnidom visited the forum only 3 years ago, the day he wrote the question, and then he disappears...


If you want to help others, answer the recently opened questions instead of the ones opened more than 3 years ago...

Who is online

Users browsing this forum: No registered users and 18 guests