Sign in to follow this  
Followers 0
waardd

automate creation of variables

10 posts in this topic

Question: Is it possible to create variables form a count result

for example:

A count of active network adaptors results in 3

what i want is that variables $nw1, $w2 and $nw3 are created

If the count has more or less the according varibles should be made

for example:

Count of adaptors is 8, $nw1 to $nw8 are made

Count of adaptors is 1, just $nw1 is created

Who can help me?

Here to code so far:

#include <Array.au3>
_AdapterToArray()
;show array
_ArrayDelete($avArray, 0); because first one is empty
_ArrayDisplay($avArray, "$avArray AFTER _ArrayAdd()")

Func _AdapterToArray()
Global $strComputer = "localhost"
Global $objWMIService, $colAdapters, $n, $a, $avArray
;set wmiservice
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
;set array to number of adaptors
$colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x00).Count
Global $avArray[$colitems+1][3]
;fill array with Name and MAC adaptors
$colAdapters = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
$n = 1
For $objAdapter in $colAdapters
$avArray [$n][0] = $objAdapter.Description
$avArray [$n][1] = $objAdapter.MACAddress
$avArray [$n][2] = $n
$n = $n + 1
Next
EndFunc

Share this post


Link to post
Share on other sites



You already have arrays in your script. I would stick with arrays because handling is much easier. You can directly access them by index or process all in a loop.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Fair enough :) I got it working with indeed the array.

Another question

Suppose i have an array

[0]|Intel® 82567LM Gigabit Network Connection|00:24:E8:E7:82:1F|1

[1]|Intel® WiFi Link 5100 AGN|00:24:D6:58:32:C4|2

[2]|VirtualBox Host-Only Ethernet Adapter|08:00:27:00:74:C6|3

How do i then set for example $ipwifi to the correct @IPAddress (in this case @IPAddress2)

Share this post


Link to post
Share on other sites

How are you collecting and storing the IP info the IP address?

One way would be to export the arp cache to a text file then read and compare the MAC address and marry the two up that way


[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

This is my function so far:

Func _AdapterToArray()
Global $strComputer = "localhost"
Global $objWMIService, $colAdapters, $n, $a, $avArray
Global $nw1, $nw2, $nwlabelcolor2, $nwlabelcolor1
;set wmiservice
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")
;set array to number of adaptors
$colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x00).Count
Global $avArray[$colitems+1][3]
;fill array with Name and MAC adaptors
$colAdapters = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
$n = 1
For $objAdapter in $colAdapters
$avArray [$n][0] = $objAdapter.Description
$avArray [$n][1] = $objAdapter.MACAddress
$avArray [$n][2] = $n
$n = $n + 1
Next
_ArrayDelete($avArray, 0)
Global $adaptorLAN = _ArraySearch($avArray, "Network Connection", 0, 0, 0, 1, 1, 0)
;msgbox(0,"",$adaptorLAN)
if $adaptorLAN = -1 Then
$nw1string = "LAN adaptor niet aangesloten!"
$nw1 = "Disconnected"
$nwlabelcolor1 = "0xaa0000"
Else
If $adaptorLAN+1 = 1 Then
  $nw1 = @IPAddress1
Else
  if $adaptorLAN+1 = 2 Then
   $nw1 = @IPAddress2
  Else
   if $adaptorLAN+1 = 3 Then
    $nw1 = @IPAddress3
   EndIf
  EndIf
EndIf
Global $nw1string = $avArray[_ArraySearch($avArray, "Connection", 0, 0, 0, 1, 1, 0)][0]& " heeft IP: "&$nw1
  $nwlabelcolor1 = "0x00aaaa"
EndIf
;msgbox(0,"",$nw1string)
Global $adaptorwifi = _ArraySearch($avArray, "WiFi", 0, 0, 0, 1, 1, 0)
;msgbox(0,"",$adaptorwifi)
if $adaptorwifi = -1 Then
$nw2string = "WiFi adaptor niet aangesloten!"
$nw2 = "Disconnected"
$nwlabelcolor2 = "0xaa0000"
Else
If $adaptorwifi+1 = 1 Then
  $nw2 = @IPAddress1
Else
  if $adaptorwifi+1 = 2 Then
   $nw2 = @IPAddress2
  Else
   if $adaptorwifi+1 = 3 Then
    $nw2 = @IPAddress3
   EndIf
  EndIf
EndIf
Global $nw2string = $avArray[_ArraySearch($avArray, "WiFi", 0, 0, 0, 1, 1, 0)][0]& " heeft IP: "&$nw2
$nwlabelcolor2 = "0x00aaaa"
EndIf
;msgbox(0,"",$nw2string)
EndFunc

Share this post


Link to post
Share on other sites

It's also easy to add variables with an array by the use of ReDim(), nuke the contents of an entire array by removing the dim tag (the number in [brackets]) and setting it to zero. You can do so much dynamic work with arrays.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

sry, I realised that you are running this locally so arp is no good - bum steer

I don't know if this might help or be of use. It's part of something I was working on a while ago but never finished. You may be able to make it work for you.

It was done on my work PC running 2008 and I never really progressed to checking it on any other O/S

it extracts the info that you want - kinda

#include <Array.au3>
Dim $ipmac[1], $txt
RunWait(@ComSpec & " /c ipconfig /all > " & @TempDir & "\ipinfo.txt") ; pipe Ipconfig /all to file
$file = FileOpen(@TempDir & "\ipinfo.txt", 0)
While 1  ;read file line by line
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
If $line = "" Then ContinueLoop
If StringInStr($line, "(preferred)") Then $line = StringReplace($line, "(preferred)", "") ;Strip unwanted txt from Windows 2008 output
If StringInStr($line, "Description") Or StringInStr($line, "Physical") Or StringInStr($line, "Address") Then  ;Filter info
  $st1 = StringSplit($line, ":")  ;split to extract required info
  If StringInStr($txt, ".")  Then  ;Output when Description, Physical Address and IP address have been collected
   MsgBox(0, $line, $txt)   ;Show detail === write to file / array if required
   $txt = ""      ;Reset string for next adapter
  EndIf
  _ArrayAdd($ipmac, $st1[2])   ;Add required info to array
  $txt &= $st1[2] & " " ;Concatenate info to string
Else
  ContinueLoop
EndIf
WEnd
_ArrayDisplay($ipmac, "Ip info")
FileClose(@TempDir & "\ipinfo.txt")
FileDelete(@TempDir & "\ipinfo.txt")

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Share this post


Link to post
Share on other sites

Looks great!! Had to translate the terms to dutch to get it to work :)

Here is my final solution:

Func _AdapterToArray()
Global $strComputer = "localhost"
Global $objWMIService, $colAdapters, $n, $a, $avArray
Global $nw1, $nw2, $nwlabelcolor2, $nwlabelcolor1
;set wmiservice
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")
;set array to number of adaptors
$colItems = $objWMIService.ExecQuery("SELECT Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x00).Count
Global $avArray[$colitems+1][9]
$avArray [0][0] = "Volgnummer"
$avArray [0][1] = "Omschrijving"
$avArray [0][2] = "MAC"
$avArray [0][3] = "IPv4"
$avArray [0][4] = "Subnet IPv4"
$avArray [0][5] = "IPv6"
$avArray [0][6] = "Subnet IPv6"
$avArray [0][7] = "Servicenaam"
$avArray [0][7] = "InterfaceIndex"
;fill array with Name and MAC adaptors
$colAdapters = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
$n = 1
For $objAdapter in $colAdapters
  $avArray [$n][0] = $n
  $avArray [$n][1] = $objAdapter.Description
  $avArray [$n][2] = $objAdapter.MACAddress
  $avArray [$n][3] = $objAdapter.IPAddress(0)
  $avArray [$n][4] = $objAdapter.IPSubnet(0)
  $avArray [$n][5] = $objAdapter.IPAddress(1)
  $avArray [$n][6] = $objAdapter.IPSubnet(1)
  $avArray [$n][7] = $objAdapter.ServiceName
  $avArray [$n][7] = $objAdapter.InterfaceIndex
  $n = $n + 1
Next

;_ArrayDisplay($avArray)
Global $adaptorLAN = _ArraySearch($avArray, "Network Connection", 0, 0, 0, 1, 1, 1)
if $adaptorLAN = -1 Then
  $nw1string = "LAN adaptor niet aangesloten!"
  $nw1 = "Disconnected"
  $nwlabelcolor1 = "0xaa0000"
Else
  Global $nw1string = $avArray[_ArraySearch($avArray, "Connection", 0, 0, 0, 1, 1, 1)][1]& " heeft IPv4: "& $avArray[_ArraySearch($avArray, "Connection", 0, 0, 0, 1, 1, 1)][3]
  $nwlabelcolor1 = "0x00aaaa"
  $nw1 = $avArray[_ArraySearch($avArray, "Connection", 0, 0, 0, 1, 1, 1)][3]
EndIf
Global $adaptorwifi = _ArraySearch($avArray, "WiFi", 0, 0, 0, 1, 1, 1)
if $adaptorwifi = -1 Then
  $nw2string = "WiFi adaptor niet aangesloten!"
  $nw2 = "Disconnected"
  $nwlabelcolor2 = "0xaa0000"
Else
  Global $nw2string = $avArray[_ArraySearch($avArray, "WiFi", 0, 0, 0, 1, 1, 1)][1]& " heeft IPv4: "& $avArray[_ArraySearch($avArray, "WiFi", 0, 0, 0, 1, 1, 1)][3]
  $nwlabelcolor2 = "0x00aaaa"
  $nw2 = $avArray[_ArraySearch($avArray, "WiFi", 0, 0, 0, 1, 1, 1)][3]
EndIf
EndFunc

Share this post


Link to post
Share on other sites

$avArray [0][5] = "IPv6"

$avArray [0][6] = "Subnet IPv6"

$avArray [0][7] = "Servicenaam"

$avArray [0][7] = "InterfaceIndex"

$avArray [$n][6] = $objAdapter.IPSubnet(1)

$avArray [$n][7] = $objAdapter.ServiceName

$avArray [$n][7] = $objAdapter.InterfaceIndex

You're filling the same container twice. In both blocks. Hurray for cut and paste code.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

Tnx for the remark Blue_Drache ;)

Indeed a copy-paste error :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0