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
Solved

Powershell to VBS convert

Posted on 2011-09-14
13
1,959 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
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

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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 …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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.

837 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