Solved

Powershell to VBS convert

Posted on 2011-09-14
13
1,813 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
Comment Utility
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
Comment Utility
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
Comment Utility
No Error.. No output... just finishing without any error... Kindly embedded the change in script to redirect the output to output.txt.
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

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

Expert Comment

by:YZlat
Comment Utility
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
Comment Utility
works absolutely fine but simple correction needed. it is popping up msgbox for a confirmation everytime. MSGBOX
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent Work.. really helpful and faster response.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now