Page 1 of 1 1
Topic Options
#176714 - 2007-06-04 10:17 AM Problems outputting information to text file....
jules666 Offline
Fresh Scripter

Registered: 2006-09-29
Posts: 22
Hi I have the following bit of code that returns network adpater information fine in command window but I am trying to output to a txt file.

(Eventually want it to pop up in window when run but figured I would try get the output to a txt file first!)
Main code is -
 Code:
Break On

$strComputer = "."
$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=-1",,48)
For each $objItem in $colItems

  "Adapter Name: " + $objItem.Description ?

  For each $Item in $objItem.IPAddress
    "IPAddress: " + $Item ?
  Next
    For each $Item in $objItem.DefaultIPGateway
    "DefaultIPGateway: " + $Item ?
  Next

  For each $Item in $objItem.DNSServerSearchOrder
    "DNS Servers: " + $Item ?
  Next
  For each $Item in $objItem.IPSubnet
    "IPSubnet: " + $Item ?
     "MACAddress: " + $objItem.MACAddress ?

  "WINSPrimaryServer: " + $objItem.WINSPrimaryServer ?

  "WINSSecondaryServer: " + $objItem.WINSSecondaryServer ?


Problem is though the I have changed the above code to what is below in the attempt to get it out to a txt file but I do not get the full results only the first adapter.
Code is -
 Code:
Break On
   del c:\temp\log.txt
$strComputer = "."
$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=-1",,48)

For each $objItem in $colItems
 if open(3 , "c:\temp\log.txt" , 5 ) = 0
 writeline ( 3 , "Adapter Name: " + $objItem.Description + @CRLF )

  For each $Item in $objItem.IPAddress
   writeline ( 3 , "IPAddress: " + $Item + @CRLF )
  Next
   For each $Item in $objItem.DefaultIPGateway
 writeline ( 3 , "DefaultIPGateway: " + $Item + @CRLF )
  Next

  For each $Item in $objItem.DNSServerSearchOrder
  writeline ( 3 ,  "DNS Servers: " + $Item + @CRLF )
  Next
  For each $Item in $objItem.IPSubnet
writeline ( 3 ,  "IPSubnet: " + $Item + @CRLF )
  writeline ( 3 ,  "MACAddress: " + $objItem.MACAddress + @CRLF )

  writeline ( 3 ,"WINSPrimaryServer: " + $objItem.WINSPrimaryServer + @CRLF )

 writeline ( 3 , "WINSSecondaryServer: " + $objItem.WINSSecondaryServer + @CRLF )
 endif


sure you experts can see what I am doing wrong, if you could point me in the right direction that would be great!

Thanks in advance \:\)


Edited by jules666 (2007-06-04 11:11 AM)

Top
#176715 - 2007-06-04 12:24 PM Re: Problems outputting information to text file.... [Re: jules666]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Take your original code and put
 Code:
Del 'c:\temp\log.txt'
$RC = RedirectOutput('c:\temp\log.txt')

in the beginning of the file and try. You can even wrap that in an if statement so it only redirects when you pass an argument (or vice-versa).

Put
 Code:
$RC = RedirectOutput('')

at the end to close the output file.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#176745 - 2007-06-05 10:20 AM Re: Problems outputting information to text file.... [Re: Glenn Barnas]
jules666 Offline
Fresh Scripter

Registered: 2006-09-29
Posts: 22
Awesome! Works a treat. Thanks Glenn!
Final code for this anyway is below in case it helps anyone! \:\)
 Code:
Break On
    Del 'c:\temp\log.txt'

$strComputer = "."
$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=-1",,48)

For each $objItem in $colItems
$RC = RedirectOutput('c:\temp\log.txt')
  "Adapter Name: " + $objItem.Description ?

  For each $Item in $objItem.IPAddress
    "IPAddress: " + $Item ?
  Next
    For each $Item in $objItem.DefaultIPGateway
    "DefaultIPGateway: " + $Item ?
  Next

  For each $Item in $objItem.DNSServerSearchOrder
    "DNS Servers: " + $Item ?
  Next
  For each $Item in $objItem.IPSubnet
    "IPSubnet: " + $Item ?
     "MACAddress: " + $objItem.MACAddress ?

  "WINSPrimaryServer: " + $objItem.WINSPrimaryServer ?

  "WINSSecondaryServer: " + $objItem.WINSSecondaryServer ?
  @CRLF


  Next
    $RC = RedirectOutput('')


Need to start trying to figure out how to get the output now into a pop up type window....hopefully that is easy to do! \:\)


Edited by jules666 (2007-06-05 10:20 AM)

Top
#176746 - 2007-06-05 10:43 AM Re: Problems outputting information to text file.... [Re: jules666]
jules666 Offline
Fresh Scripter

Registered: 2006-09-29
Posts: 22
Just noticed, not sure if its obvious fix but in my code above when running on various machines, sometimes an adapter is listed twice...most odd.
If anyone has an ideas please let me know!

Glenn - Have come accross a post by you for your NICINFO udf...may have to figure how to use that instead as that looks to be doing what I wanted and more \:\)

Top
#176747 - 2007-06-05 11:48 AM Re: Problems outputting information to text file.... [Re: jules666]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
An example using the NicInfo UDF. You need to include or call the NicInfo UDF in your script. Each UDF has one or more examples in the commented lines above the actual UDF. This way you can always see how to use the UDF.

UDF Library » NICInfo() Return info about all NICs

 Code:
Break on

$Array = NicInfo($COMPUTER)
If UBound($Array) < 0
	?UBound($array)
	@SERROR ?
Else
	UBound($Array) + 1 ' NICs found.' ?
	For $X = 0 to UBound($Array)
		For $Y = 0 to UBound($Array[$X])
			$y '. ' $Array[$X][$Y] ?
		Next
	Next
EndIf
Sleep 5


Edited by Mart (2007-06-05 11:48 AM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#176753 - 2007-06-05 01:35 PM Re: Problems outputting information to text file.... [Re: jules666]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I have a small script that calls and enumerates the data from NicInfo(). I've found it very handy. I'll post the header when I get to the office.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#176779 - 2007-06-05 11:53 PM Re: Problems outputting information to text file.... [Re: Glenn Barnas]
jules666 Offline
Fresh Scripter

Registered: 2006-09-29
Posts: 22
 Originally Posted By: Glenn Barnas
I have a small script that calls and enumerates the data from NicInfo(). I've found it very handy. I'll post the header when I get to the office.

Glenn

cool that would be really handy if you could post it \:\)

Top
#176781 - 2007-06-06 03:56 AM Re: Problems outputting information to text file.... [Re: jules666]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Got hammered with problems at work today.. here's the header code:
 Code:
Break On

Dim $, $aND[19], $aNicInfo, $N, $P

$=setoption("Wrapateol","on")
$=setoption("Explicit","off")
$=setoption("NoVarsinstrings","on")
$=setoption("NoMacrosinstrings","on")

If Not IsDeclared($TARGET)
  Global $TARGET
EndIf

If $TARGET = '' 
  $Target = @WKSTA
EndIf

$aND[0]  = '       Adapter Description: '
$aND[1]  = '              Manufacturer: '
$aND[2]  = '                  NIC GUID: '
$aND[3]  = '        Speed/Duplex value: '
$aND[4]  = '         Speed/Duplex text: '
$aND[5]  = '            Driver Version: '
$aND[6]  = ' DHCP boolean (1=Use DHCP): '
$aND[7]  = 'IP settings  (add,mask,gw): '
$aND[8]  = '               Domain Name: '
$aND[9]  = '           DNS Server list: '
$aND[10] = '  DHCP Server & Lease info: '
$aND[11] = '          WINS Server list: '
$aND[12] = '            Interface Name: '
$aND[13] = '    Static DNS Server List: '
$aND[14] = '   Reserved for future use: '
$aND[15] = '                NIC Status: '
$aND[16] = '               MAC Address: '
$aND[17] = '                 TEAM flag: '
$aND[18] = '     # of adapters in team: '
$aND[19] = '  GUIDs of teamed adapters: '


$aNicInfo = NicInfo($TARGET)

For Each $N in $aNicInfo
  For $P = 0 to 19
    $aND[$P] $N[$P] ?
  Next
  ?
Next 


Just tack the NicInfo UDF to the end of this code and save it as NicInfo.kix. Create a batch file NIC.BAT with the following line in it:
 Code:
@kix32.exe nicinfo.kix $TARGET="%1"

Run NIC, or NIC COMPUTERNAME to get a list of every network adapter and settings. This header was an example of how to use the UDF and became a useful tool to quickly verify remote network settings.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#176802 - 2007-06-07 10:19 AM Re: Problems outputting information to text file.... [Re: Glenn Barnas]
jules666 Offline
Fresh Scripter

Registered: 2006-09-29
Posts: 22
Thanks for that Glenn
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1955 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.067 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org