Community discussions

MikroTik App
 
User avatar
erizetael
just joined
Topic Author
Posts: 7
Joined: Mon Jun 10, 2013 10:04 am
Location: indonesia

if condition for array

Wed Oct 25, 2017 9:46 am

Hi,
How to create a boolean from an array. I have an array variable. This array list contains 5 hotspot user names that I will input each month at the beginning of each month. What I want to do is
if(the array names exist in /tool user-manager user) do={remove them all} else={create them and assign to a profile}. Easily I want to write that if those names exist, it will return true, and otherwise it will return false, so I can create them and assign to a profile. Please help..
:local name
:set name "name1, name2, name3, name4, name5";
:if(/tool user-manager user $name) do={
	/tool user-manager user remove $name
} else={
	/tool user-manager user add username=$name password=$name customer=admin
	/tool user-manager user create-and-activate-profile profile=800menit customer=admin
}
 
icosasupport
newbie
Posts: 29
Joined: Fri Oct 13, 2017 8:37 pm

Re: if condition for array

Tue Nov 14, 2017 2:16 am

Hi erizetael,

This should be close to what you wish to achieve.
Cheers,
Icosa.
:local names [:toarray "name1,name2,name3,name4,name5"]
:local profile "800menit"
/tool user-manager
:foreach user in=$names do={
	if([:tool user-manager user [find username=$user]] = "") do={
		[:tool user-manager user add username=$name password=$name customer=admin]
		[:tool user-manager user create-and-activate-profile profile=$profile customer=admin]
		[:log info "Added username $user to User-Manager with profile $profile"]
	} else={
		[:tool user-manager user remove [find username=$user]]
		[:log info "Removed username $user from User-Manager"]
	}
}

Who is online

Users browsing this forum: eworm, GoogleOther [Bot] and 36 guests