Jump to content
Sign in to follow this  
ioliver

$Variables in Functions

Recommended Posts

ioliver

; System Information.AU3
; Ian Oliver
; September 23, 2004

$host = InputBox("System Information (AU3)", "Host Name?")
Call("PSInfo")
Call("MakeModel")
$txt_Report = FileOpen("c:\SIR.txt", 2)
FileWriteLine($txt_Report, "Host Name: " & $host & "")
FileWriteLine($txt_Report, "")
FileWriteLine($txt_Report, "Make:      " & $make & "")
FileWriteLine($txt_Report, "Model:     " & $model & "")
FileWriteLine($txt_Report, "Processor: " & $processortype & "")
FileWriteLine($txt_Report, "Speed:     " & $processorspeed & "")
FileWriteLine($txt_Report, "Memory:    " & $memory & "")
FileClose($txt_Report)
Call("Cleanup")
MsgBox(0, "System Information (AU3 + a little VBS)", "Complete!")

Func PSInfo()
  FileInstall("c:\psinfo\psinfo.exe", "c:\psinfo.exe")
  FileInstall("c:\psinfo\pdh.dll", "c:\pdh.dll")
  RunWait(@ComSpec & ' /c c:\psinfo.exe -c "\\' & $host & '" > c:\psinfo.txt', 'c:\', @SW_HIDE)
  $txt_psinfo = FileReadLine("c:\psinfo.txt")
  $array_psinfo = StringSplit($txt_psinfo, ",")
 ; PSInfo Variables
  $processorspeed = $array_psinfo[17]
  $processortype  = $array_psinfo[18]
  $memory         = $array_psinfo[19]
EndFunc

Func MakeModel()
  $vbs = FileOpen("c:\MakeModel.vbs", 2)
  FileWriteLine($vbs, "")
  FileWriteLine($vbs, "On Error Resume Next")
  FileWriteLine($vbs, 'Set FSO = CreateObject("Scripting.FileSystemObject")')
  FileWriteLine($vbs, 'Set MakeModel = FSO.CreateTextFile("c:\MakeModel.txt", True)')
  FileWriteLine($vbs, 'strComputer = "' & $host & '"')
  FileWriteLine($vbs, 'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")')
  FileWriteLine($vbs, 'Set colItems2 = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)')
  FileWriteLine($vbs, "")
  FileWriteLine($vbs, "For Each objItem in colItems2")
  FileWriteLine($vbs, "")
  FileWriteLine($vbs, "Make = objItem.Manufacturer")
  FileWriteLine($vbs, "Model = objItem.Model")
  FileWriteLine($vbs, "MakeModel.WriteLine Make")
  FileWriteLine($vbs, "MakeModel.WriteLine Model")
  FileWriteLine($vbs, "")
  FileWriteLine($vbs, "Next")
  FileClose($vbs)
  Run("wscript.exe c:\MakeModel.vbs")
  $txt_MakeModel = FileOpen("c:\MakeModel.txt", 0)
 ; MakeModel Variables
  $make  = FileReadLine($txt_MakeModel, 1)
  $model = FileReadLine($txt_MakeModel, 2)
  FileClose($txt_MakeModel)
EndFunc

Func Cleanup()
  FileDelete("c:\psinfo.exe")
  FileDelete("c:\pdh.dll")
  FileDelete("c:\psinfo.txt")
  FileDelete("c:\MakeModel.vbs")
  FileDelete("c:\MakeModel.txt")
EndFunc

I did check the help like on this... But I don't understand how to use Return()

After I run the Functions, I need to use the Variables that they generate to create a report. But, I don't think the Variables cary over from the Functions? I'm sorry if that's not very clear. Please let me know if you need more information.

-> Ohh, check the $make Variable, it's the first example of the problem.

Thanks,

Ian


"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Share this post


Link to post
Share on other sites
RDSchaefer

; System Information.AU3
; Ian Oliver
; September 23, 2004

$host = InputBox("System Information (AU3)", "Host Name?")
Call("PSInfo")
Call("MakeModel")
$txt_Report = FileOpen("c:\SIR.txt", 2)
FileWriteLine($txt_Report, "Host Name: " & $host & "")
FileWriteLine($txt_Report, "")
FileWriteLine($txt_Report, "Make:      " & $make & "")
FileWriteLine($txt_Report, "Model:     " & $model & "")
FileWriteLine($txt_Report, "Processor: " & $processortype & "")
FileWriteLine($txt_Report, "Speed:     " & $processorspeed & "")
FileWriteLine($txt_Report, "Memory:    " & $memory & "")
FileClose($txt_Report)
Call("Cleanup")
MsgBox(0, "System Information (AU3 + a little VBS)", "Complete!")

Func PSInfo()
  FileInstall("c:\psinfo\psinfo.exe", "c:\psinfo.exe")
  FileInstall("c:\psinfo\pdh.dll", "c:\pdh.dll")
  RunWait(@ComSpec & ' /c c:\psinfo.exe -c "\\' & $host & '" > c:\psinfo.txt', 'c:\', @SW_HIDE)
  $txt_psinfo = FileReadLine("c:\psinfo.txt")
  $array_psinfo = StringSplit($txt_psinfo, ",")
; PSInfo Variables
  $processorspeed = $array_psinfo[17]
  $processortype  = $array_psinfo[18]
  $memory         = $array_psinfo[19]
EndFunc

Func MakeModel()
  $vbs = FileOpen("c:\MakeModel.vbs", 2)
  FileWriteLine($vbs, "")
  FileWriteLine($vbs, "On Error Resume Next")
  FileWriteLine($vbs, 'Set FSO = CreateObject("Scripting.FileSystemObject")')
  FileWriteLine($vbs, 'Set MakeModel = FSO.CreateTextFile("c:\MakeModel.txt", True)')
  FileWriteLine($vbs, 'strComputer = "' & $host & '"')
  FileWriteLine($vbs, 'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")')
  FileWriteLine($vbs, 'Set colItems2 = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)')
  FileWriteLine($vbs, "")
  FileWriteLine($vbs, "For Each objItem in colItems2")
  FileWriteLine($vbs, "")
  FileWriteLine($vbs, "Make = objItem.Manufacturer")
  FileWriteLine($vbs, "Model = objItem.Model")
  FileWriteLine($vbs, "MakeModel.WriteLine Make")
  FileWriteLine($vbs, "MakeModel.WriteLine Model")
  FileWriteLine($vbs, "")
  FileWriteLine($vbs, "Next")
  FileClose($vbs)
  Run("wscript.exe c:\MakeModel.vbs")
  $txt_MakeModel = FileOpen("c:\MakeModel.txt", 0)
; MakeModel Variables
  $make  = FileReadLine($txt_MakeModel, 1)
  $model = FileReadLine($txt_MakeModel, 2)
  FileClose($txt_MakeModel)
EndFunc

Func Cleanup()
  FileDelete("c:\psinfo.exe")
  FileDelete("c:\pdh.dll")
  FileDelete("c:\psinfo.txt")
  FileDelete("c:\MakeModel.vbs")
  FileDelete("c:\MakeModel.txt")
EndFunc

I did check the help like on this... But I don't understand how to use Return()

After I run the Functions, I need to use the Variables that they generate to create a report. But, I don't think the Variables cary over from the Functions? I'm sorry if that's not very clear. Please let me know if you need more information.

-> Ohh, check the $make Variable, it's the first example of the problem.

Thanks,

Ian

First, don't use Call(..., just use the function name as in

.

.

.

PSInfo()

.

.

.

The simplest way to fix this is just make the variables you need global as in:

Global $host

Global $make

Global $model

Global $processortype

Global $processorspeed

Global $memory

.

.

.

$host = InputBox("System Information (AU3)", "Host Name?")

Share this post


Link to post
Share on other sites
poisonkiller

Variables CAN carry over from functions, unless you have declared them as locals. If you declared a variable inside function as local, it'll be destroyed when function returns. Also you said you don't understand Return. It's not a function what you can call. Here's an example how to use Return and Global/Local variables.

Global $input1
$test1 = TestFunction1() ;Calls TestFunction1(), you don't have to use Call()
MsgBox(0, $test1, $input1) ;Notice, MsgBox title and text are the same
$test2 = TestFunction2()
;MsgBox(0, $test2, $input2) ;If I would use it like this, AutoIt would give me an error, because $input2 doesn't exist
Dim $input2
MsgBox(0, $test2, $input2) ;MsgBox title and text are different
Func TestFunction1()
    $input1 = InputBox("Input", "Input something") ;Makes an input
    Return $input1 ;Exits function and gives $test1 $input1 contents
EndFunc
Func TestFunction2()
    #cs
    I could've done it like this too:
    Local $input2
    $input2 = InputBox("Input", "Input something again")
    #ce
    Local $input2 = InputBox("Input", "Input something again")
    Return $input2
EndFunc
Edited by poisonkiller

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  

×