Solved

Powershell to VBS convert

Posted on 2011-09-14
13
1,859 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
 
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
A brief introduction to what I consider to be the best editor for PowerShell.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

914 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

17 Experts available now in Live!

Get 1:1 Help Now