Solved

Powershell to VBS convert

Posted on 2011-09-14
13
1,913 Views
Last Modified: 2012-06-21
Can someone convert below powershell script into VBs ??

#----------------Starts here------------------------

$hosts = Get-Content C:\Servers.txt
foreach ($Hostname in $hosts) {
    $PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$Hostname'" | Select-Object StatusCode
    If ($PingStatus.StatusCode -eq 0){
        $url = "http://$Hostname/xmldata?item=All"
        $feed=[xml](new-object system.net.webclient).downloadstring($url)
        $feed.RIMP.mp.fwri + "`t" + $Hostname + "`t" + $feed.RIMP.hsi.spn + "`t" + $feed.RIMP.hsi.SBSN + "`t" + $feed.RIMP.mp.pn + "`t" + $feed.RIMP.mp.BBLK + "`t" + $feed.RIMP.mp.SN
    }}

#----------------Endss here------------------------
0
Comment
Question by:getazhar
  • 7
  • 6
13 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 36536334
Here is part of it:

 
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\Servers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
hosts = Split(strText, vbCrLf)

For Each hostname in hosts
	strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & hostName & "'"
    	Set colItems = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )

    	For Each item In colItems
        	If item.StatusCode = 0 Then
           		url = "http://" & hostname & "/xmldata?item=All"
		 	
        	End If
    	Next

Next

Open in new window


I will post part reading rss fee later
0
 
LVL 35

Expert Comment

by:YZlat
ID: 36536380
try this

 
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\Servers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
hosts = Split(strText, vbCrLf)

For Each hostname in hosts
	strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & hostName & "'"
    	Set colItems = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )

    	For Each item In colItems
        	If item.StatusCode = 0 Then
           		url = "http://" & hostname & "/xmldata?item=All"
			Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")     
			xmlhttp.open "GET", url, False     
			xmlhttp.send     
			feed=xmlhttp.responseText
		 	
        	End If
    	Next

Next

Open in new window

0
 

Author Comment

by:getazhar
ID: 36537162
No Error.. No output... just finishing without any error... Kindly embedded the change in script to redirect the output to output.txt.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 35

Expert Comment

by:YZlat
ID: 36538294
Can you tell me exactly what your script supposed to do besides reading server names from text file , pinging each of them and and getting rss code for each server where ping status code is not 0. What do you want to do next? What should be the final result of the script?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 36538916
I converted all your code except for this line

$feed.RIMP.mp.fwri + "`t" + $Hostname + "`t" + $feed.RIMP.hsi.spn + "`t" + $feed.RIMP.hsi.SBSN + "`t" + $feed.RIMP.mp.pn + "`t" + $feed.RIMP.mp.BBLK + "`t" + $feed.RIMP.mp.SN

and your code does not seem to redirect output to a text file. And you have not mentioned that in your original question. kindly explain what do you want to do
0
 

Author Comment

by:getazhar
ID: 36540883
Basically the PowerShell script pick the server name from servers.txt text file and replace the same in place of hostname at given URL "http://" & hostname & "/xmldata?item=All" and pulls the iLO info.

Attached is the snapshots of powershell script and web output.
1.png
2.png
0
 

Author Comment

by:getazhar
ID: 36540893
if you could redirect the output to TSV file would be good.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 36544223
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\Servers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
hosts = Split(strText, vbCrLf)

For Each hostname in hosts
	strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & hostName & "'"
    	Set colItems = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )

    	For Each item In colItems
        	If item.StatusCode = 0 Then
           		url = "http://" & hostname & "/xmldata?item=All"
			Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")     
			xmlhttp.open "GET", url, False     
			xmlhttp.send     
			feed=xmlhttp.responseText
			Set xmldoc = CreateObject("Microsoft.XMLDOM") 
			xmldoc.async = False 
			xmldoc.preserveWhiteSpace= False ' True 
			xmldoc.loadXml(feed) 

			strText=xmldoc.SelectSingleNode("//FWRI").Text & vbTab & hostname & vbTab & xmldoc.SelectSingleNode("//SPN").Text & 				vbTab & xmldoc.SelectSingleNode("//SBSN").Text & vbTab & xmldoc.SelectSingleNode("//PN").Text & vbTab 						& xmldoc.SelectSingleNode("//BBLK").Text & vbTab & xmldoc.SelectSingleNode("//SN").Text

			MsgBox strText
			Set objFSO = CreateObject("Scripting.FileSystemObject")
			Set objTextFile = objFSO.OpenTextFile ("C\output.txt", 8, True)
			objTextFile.WriteLine(strText)
			objTextFile.Close


			Set xmlDoc = Nothing
		 	
        	End If
    	Next

Next

Open in new window

0
 

Author Comment

by:getazhar
ID: 36549104
works absolutely fine but simple correction needed. it is popping up msgbox for a confirmation everytime. MSGBOX
0
 
LVL 35

Expert Comment

by:YZlat
ID: 36549352
just comment out message box

 
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\Servers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
hosts = Split(strText, vbCrLf)

For Each hostname in hosts
	strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & hostName & "'"
    	Set colItems = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )

    	For Each item In colItems
        	If item.StatusCode = 0 Then
           		url = "http://" & hostname & "/xmldata?item=All"
			Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")     
			xmlhttp.open "GET", url, False     
			xmlhttp.send     
			feed=xmlhttp.responseText
			Set xmldoc = CreateObject("Microsoft.XMLDOM") 
			xmldoc.async = False 
			xmldoc.preserveWhiteSpace= False ' True 
			xmldoc.loadXml(feed) 

			strText=xmldoc.SelectSingleNode("//FWRI").Text & vbTab & hostname & vbTab & xmldoc.SelectSingleNode("//SPN").Text & 				vbTab & xmldoc.SelectSingleNode("//SBSN").Text & vbTab & xmldoc.SelectSingleNode("//PN").Text & vbTab 						& xmldoc.SelectSingleNode("//BBLK").Text & vbTab & xmldoc.SelectSingleNode("//SN").Text

			#MsgBox strText
			Set objFSO = CreateObject("Scripting.FileSystemObject")
			Set objTextFile = objFSO.OpenTextFile ("C\output.txt", 8, True)
			objTextFile.WriteLine(strText)
			objTextFile.Close


			Set xmlDoc = Nothing
		 	
        	End If
    	Next

Next

Open in new window

0
 

Author Comment

by:getazhar
ID: 36549552
Thanks you so much for help on this.

One last thing to check. I would like to have header in the first row. Could you please help me on this ?



 last
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 36550715
try this

   
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\Servers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
hosts = Split(strText, vbCrLf)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("C\output.txt", 8, True)
strHeader="FWRI" & vbTab & "hostname" & vbTab & "SPN" & vbTab & "SBSN" & vbTab & "PN" & vbTab & "BBLK" & vbTab & "SN"
objTextFile.WriteLine(strHeader)
For Each hostname in hosts
	strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & hostName & "'"
    	Set colItems = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )

    	For Each item In colItems
        	If item.StatusCode = 0 Then
           		url = "http://" & hostname & "/xmldata?item=All"
			Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")     
			xmlhttp.open "GET", url, False     
			xmlhttp.send     
			feed=xmlhttp.responseText
			Set xmldoc = CreateObject("Microsoft.XMLDOM") 
			xmldoc.async = False 
			xmldoc.preserveWhiteSpace= False ' True 
			xmldoc.loadXml(feed) 

			strText=xmldoc.SelectSingleNode("//FWRI").Text & vbTab & hostname & vbTab & xmldoc.SelectSingleNode("//SPN").Text & 				vbTab & xmldoc.SelectSingleNode("//SBSN").Text & vbTab & xmldoc.SelectSingleNode("//PN").Text & vbTab 						& xmldoc.SelectSingleNode("//BBLK").Text & vbTab & xmldoc.SelectSingleNode("//SN").Text
			objTextFile.WriteLine(strText)
			
			Set xmlDoc = Nothing	 	
        	End If
    	Next
Next
objTextFile.Close

Open in new window

0
 

Author Closing Comment

by:getazhar
ID: 36550825
Excellent Work.. really helpful and faster response.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question