Jump to content

[Solved] Merge Array and Text to make a complete Array variable / object


Recommended Posts

Interresting one I tried to use the execute on an array did not work out as expected when I read up on the help file it did not seem to have an array limitation, as you can tell i am lacking some basic fundamental knowledge on how to handle variables so forgive my ignorance, that been said your example has been most enlightening and I was able to convert 5 quite large routine into on small one, so once again thanks.

Now the issue below, I want to join the array variable $obj with "[$i][1]" so it will become in this example "$network_printers[$i][1]" for instance, I looked for verious ways to join these toegther but so far to no avail, the important thing is that the end variable "$network_printers[$i][1]" is reconised as an array, is this possible?

Updated: I suppose what may be a more useful question is how can i turn a text variable back into a array variable.

$obj = $obj & "[0][0]"

Current effort to try and get a universal .ini reader into an array below.

Global $network_printers[99][2], $number_of_network_printers

read_array($vars_file, "Printers", "$network_printers")

Func read_array($ini_file, $ini_section, $obj)
If FileExists($ini_file) Then
  $var = IniReadSection($ini_file, $ini_section)
  If @error Then
   MsgBox(4096, "Error", "Unable to read section.")
  Else
  $obj = $obj & "[0][0]"
   For $i = 1 To $var[0][0]
    $obj[$i][1] = $var[$i][1]
    $obj[$i][0] = $var[$i][0]
    ;MsgBox(4096, "Number of Printers :" & $var[0][0], "Key: " & $network_printers[$i][0] & @CRLF & "Value: " & $network_printers[$i][1])
    Exit
   Next
   $dvar = $var[0][0]
   Return $dvar
  EndIf
Else
  $error_code = 1
  _put_event(1, "The INI file " & $ini_file & " may not exist or the section " & $ini_section & " within may not exist", @error)
EndIf
EndFunc   ;==>read_array

Original working script

Global $network_printers[99][2], $number_of_network_printers

Func _add_network_printers()
For $j = 1 To read_array("vars.ini","Printers")
MsgBox(4096, "Number of Printers :" & $number_of_network_printers, "Key: " & $network_printers[$j][0] & @CRLF & "Value: " & $network_printers[$j][1])
$printer= $network_printers[$j][1]
_PrinterAdd($printer, 0)
Next
EndFunc   ;==>_add_network_printers

Func read_file_into_array($ini_file, $ini_section)
If FileExists($ini_file) Then
  $var = IniReadSection($ini_file, $ini_section)
  If @error Then
   MsgBox(4096, "Error", "Unable to read section.")
  Else
   For $i = 1 To $var[0][0]
    $network_printers[$i][1] = $var[$i][1]
    $network_printers[$i][0] = $var[$i][0]
    MsgBox(4096, "Number of Printers :" & $var[0][0], "Key: " & $network_printers[$i][0] & @CRLF & "Value: " & $network_printers[$i][1])
   Next
   $number_of_network_printers = $var[0][0]
   Return $number_of_network_printers
  EndIf
Else
   $error_code = 1
   _put_event(1, "The INI file "  & $ini_file & " may not exist or the section " & $ini_section & " within may not exist", @error)
EndIf
EndFunc   ;==>read_host_file_into_array
Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

Link to comment
Share on other sites

OK looks like this is partially working? for some reason its not updating the $network_printers array put seems to be reading it fine into the $obj array? as the $obj should be the $network_printers array.. and I do not know why..

#include <Array.au3>
#include <EventLog.au3>
#include <string.au3>

Global $vars_file = "\\dc-pri-cfu\NETLOGON\vars.ini"
Global $network_printers[99][2], $number_of_network_printers
Global $obj[99][2], $nobj
_add_network_printers()

Func _add_network_printers()
For $j = 1 To read_array($vars_file, "Printers", "$network_printers")
  MsgBox(4096, "Number of Printers :" & $number_of_network_printers, "Key: " & $network_printers[$j][0] & @CRLF & "Value: " & $network_printers[$j][1])
  $printer = $network_printers[$j][1]
  _PrinterAdd($printer, 0)
Next
EndFunc   ;==>_add_network_printers

Func read_array($ini_file, $ini_section, $obj_var)
;MsgBox(4096, $obj1, $ini_file & @CRLF & $ini_section)
$obj = Execute($obj_var)
If FileExists($ini_file) Then
  $var = IniReadSection($ini_file, $ini_section)
  If @error Then
   MsgBox(4096, "Error", "Unable to read section.")
  Else
   For $i = 1 To $var[0][0]
    $obj[$i][0] = $var[$i][0]
    $obj[$i][1] = $var[$i][1]
    MsgBox(4096, "Number of Printers :" & $var[0][0], "Key: " & $obj[$i][0] & @CRLF & "Value: " & $obj[$i][1] & @CRLF & $obj_var)
   Next
   $nobj = $var[0][0]
   Return $nobj
  EndIf
Else
  $error_code = 1
  _put_event(1, "The INI file " & $ini_file & " may not exist or the section " & $ini_section & " within may not exist", @error)
EndIf
EndFunc   ;==>read_array


;===============================================================================
;
; Function Name:    _PrinterAdd()
; Description:      Connects to a Network Printer.
; Parameter(s):     $sPrinterName - Computer Network name and printer share name ([url="file://\ComputerSharedPrinter"]\\Computer\SharedPrinter[/url]).
;                   $fDefault - Set to 1 if Printer should be set to default (optional).
; Requirement(s):
; Return Value(s):  1 - Success, 0 - Failure, If Printer already exist @error = 1.
; Author(s):        Sven Ullstad - Wooltown
; Note(s):          None.
;
;===============================================================================
Func _PrinterAdd($sPrinterName, $fDefault = 0)
If _PrinterExist($sPrinterName) Then
  SetError(1)
  Return 0
Else
  RunWait("rundll32 printui.dll,PrintUIEntry /in /n" & $sPrinterName & " /q")
  If _PrinterExist($sPrinterName) = 0 Then
   $error_code = 1
   _put_event(1, "Could not install printer :" & $sPrinterName & " on local computer " & @ComputerName & "Does shared printer exist on host machine?", @error)
   Return 0
  EndIf
  If $fDefault = 1 Then
   _PrinterDefault($sPrinterName)
  EndIf
  Return 1
EndIf
EndFunc   ;==>_PrinterAdd

;===============================================================================
;
; Function Name:    _PrinterDefault()
; Description:      Set a printer to default.
; Parameter(s):     $sPrinterName - The name of the printer.
; Requirement(s):
; Return Value(s):  1 - Success, 0 - Failure.
; Author(s):        Sven Ullstad - Wooltown
; Note(s):          None.
;
;===============================================================================
Func _PrinterDefault($sPrinterName)
If _PrinterExist($sPrinterName) Then
  RunWait("rundll32 printui.dll,PrintUIEntry /y /n" & $sPrinterName)
  Return 1
Else
  Return 0
EndIf
EndFunc   ;==>_PrinterDefault

;===============================================================================
;
; Function Name:    _PrinterDelete()
; Description:      Delete a connection to a network printer.
; Parameter(s):     $sPrinterName - Computer Network name and printer share name.
; Requirement(s):
; Return Value(s):  1 - Success, 0 - Failure.
; Author(s):        Sven Ullstad - Wooltown
; Note(s):          None.
;
;===============================================================================
Func _PrinterDelete($sPrinterName)
If _PrinterExist($sPrinterName) Then
  RunWait("rundll32 printui.dll,PrintUIEntry /dn /n" & $sPrinterName)
  If _PrinterExist($sPrinterName) Then
   Return 0
  Else
   Return 1
  EndIf
Else
  Return 0
EndIf
EndFunc   ;==>_PrinterDelete

;===============================================================================
;
; Function Name:    _PrinterExist()
; Description:      Check if a Printer Exist.
; Parameter(s):     $sPrinterName - The name of the printer.
; Requirement(s):
; Return Value(s):  1 - Success, 0 - Failure.
; Author(s):        Sven Ullstad - Wooltown
; Note(s):          None.
;
;===============================================================================
Func _PrinterExist($sPrinterName)
Local $hService, $sPrinter, $sPrinterList
$hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")
If Not @error = 0 Then
  Return 0
EndIf
$sPrinterList = $hService.ExecQuery("Select * From Win32_Printer")
For $sPrinter In $sPrinterList
  If StringUpper($sPrinterName) = StringUpper($sPrinter.name) Then
   Return 1
  EndIf
Next
EndFunc   ;==>_PrinterExist

Func _put_event($value, $text, $error_id_code)
;SUCCESS = 0
;ERROR =1
;WARNING =2
;INFORMATION =4
;AUDIT_SUCCESS =8
;AUDIT_FAILURE =16
Local $hEventLog, $aData[4] = [3, 1, 2, 3]

$hEventLog = _EventLog__Open("", "Logon Script")
_EventLog__Report($hEventLog, $value, 0, $error_id_code, @UserName, @CRLF & @CRLF & $text & @CRLF & @CRLF & "Contact Computer Facilities for more information, contact details can be found at [url="http://www.computer-facilities.com"]www.computer-facilities.com[/url] or e-mail [email="support@computer-facilities.com"]support@computer-facilities.com[/email]", $aData)
_EventLog__Close($hEventLog)
EndFunc   ;==>_put_event
Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...