Solved

Powershell to VBS convert

Posted on 2011-09-14
13
2,069 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

751 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