vbscript to powershell

Hi everyone,

Thanks in advance.

I am new to Powershell. How to do below in Powershell, any direction?

Here is a vbscript for showing PC NAME, BIOS SN no, PC model, RAM SIZE per line in HTML:

'If something fails, move on
'Option Explicit
On Error Resume Next
DIM objFile, objFSO, filesys, filenew, filetxt, strComputer
DIM strfinishtime, strstarttime

' Constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\all_pc_list.txt", ForReading)

Set filesys=CreateObject("Scripting.FileSystemObject")
strFileOutput = "pc_list.html"

If filesys.FileExists(strFileOutput) Then
  Set filetxt = filesys.OpenTextFile (strFileOutput, ForWriting)
Else
  Set filetxt = filesys.CreateTextFile(strFileOutput, True)
End If
If Err <> 0 Then
  Wscript.Echo "Unable to open " & strFileOutput & " for output."
  WScript.Quit
End If

strfinishtime = 0
strstarttime = 0
timeUsed = 0 
strstarttime = Now()

strHTMLMetaAction = "refresh"
strHTMLRefreshTime = "1"
headerstring = headerstring &_
"<meta http-equiv= " & strHTMLMetaAction & " " &_
"content= " & strHTMLRefreshTime & " " &_
"/><style> .demo {border:1px solid #C0C0C0;border-collapse:collapse;padding:5px;}.demo th {border:1px solid #C0C0C0;padding:5px;background:#F0F0F0;}.demo td {border:1px solid #C0C0C0;padding:5px;}</style>"
filetxt.writeline(headerstring)
headerstring = ""	

headerstring = headerstring &_
"<table class=demo>" &_
	"<thead>" &_
	"<tr>" &_
		"<th>Computer Name</th>" &_
		"<th>IP</th>" &_
		"<th>PC serial</th>" &_
		"<th>PC Model</th>" &_
		"<th>RAM Total MB</th>" &_
	"</tr>" &_
	"</thead>"
	
headerstring = headerstring &_
"<tbody>"
filetxt.writeline(headerstring)
headerstring = ""


Dim arrComputers() , strIndex()
i = 0
'read text file computer name into array, line by line
Do Until objFile.AtEndOfStream
	Redim Preserve arrComputers(i)
	arrComputers(i) = objFile.ReadLine
	i = i + 1
	strIndex(i) = i
Loop
objFile.Close

For Each strComputer in arrComputers

		strPCModel = ""
		strIPAddress = ""
		strRAMSize = 0
		strPing = 0
		
		if fPing2(strComputer) = true then
			strIPAddress = fResolveIP(strComputer)	
			if Left(strIPAddress,3) <> "192" then
			headerstring = headerstring &_
			"<tr><td>" & strComputer & "</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>"	
			else
				strPCModel = fListPCModel(strComputer)	
				strRAMSize = fListRAM(strComputer)	
				headerstring = headerstring &_
				"<tr><td>" & strComputer & "</td>" &_
				"<td>" & strIPAddress & "</td>" &_
				strPCModel &_
				"</td><td>" & strRAMSize & "</td></tr>"
				filetxt.writeline(headerstring)
				headerstring = ""
			end if			
		else
			headerstring = headerstring &_
			"<tr><td>" & strComputer & "</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>"
			
			filetxt.writeline(headerstring)
			headerstring = ""
		end if

	Next

headerstring = headerstring &_
"</tbody></table>" & "<br><br>"

strfinishtime = Now()
timeUsed = (strfinishtime - strstarttime) * 100000

headerstring = headerstring &_
"Start at : " &_
strstarttime & "<br>" &_
"Finish at : " &_
strfinishtime & "<br>" &_
"Time elapsed in seconds : " &_
timeUsed & "<br><br>"

filetxt.writeline(headerstring)
filetxt.Close()

Function fListPCModel(strComputer)
	strListPCModel = ""
	a = ""
	b = ""
	c = ""
	Set objWMIService = GetObject("winmgmts:" _
			& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colPCModel =  objWMIService.ExecQuery _
			("Select * from Win32_ComputerSystemProduct")
	For Each objItem in colPCModel
		a = objItem.identifyingnumber
		c = objItem.vendor
		if objItem.name <> "" and c <> "LENOVO" then
			b = objItem.name
		else
			b = objItem.version
		end if
		strListPCModel = "<td>" &_
		a &_
		"</td><td>" &_
		b &_
		"</td>" 		
		fListPCModel = strListPCModel
	Next	
End Function

Function fListRAM(strComputer)
	Dim ramSize
	Set objWMIService = GetObject("winmgmts:" _
			& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")	
	Set colItems = objWMIService.ExecQuery _
		("Select * from Win32_PhysicalMemory",,48)
	ramSize = 0

	For Each objItem in colItems
		ramSize = ramSize + formatnumber(objItem.Capacity/1048576,0)
	Next
	fListRAM = ramSize
End Function

Function fResolveIP(strComputer)

	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")	
		
	Set colAdapters = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

	For Each objAdapter in colAdapters
		ip = trim(objAdapter.IPAddress(0))
		If ip <> "" Then
			fResolveIP = ip						
			exit for
		END IF
	Next
End Function

Function fPing2(strComputer)
      Dim objShell, boolCode, strRetry, strWaitTime
	  strRetry = 1
	  strWaitTime = 1
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n " & strRetry & " -w " & strWaitTime & " " & strComputer, 0, True)
      If boolCode = 0 Then
            fPing2 = True
      Else
            fPing2 = False
      End If
End Function

Open in new window

Ding QBvpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Here's a pretty good read on the subject of converting VBScript to PowerShell.
http://blogs.technet.com/b/heyscriptingguy/archive/2013/06/12/translate-vbscript-script-into-powershell-not.aspx

That said, sometimes it's best to take whatever motivation is at hand to learn PowerShell.  In the end, it's best to learn "the PowerShell way" and apply it to writing a script vs. trying to translate a VBScript line for line.  I think the following covers the main points of your VBScript.
$outFile = "pc_list.html"

$head = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color: #C0C0C0;border-collapse: collapse;}
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: #C0C0C0;background-color:#F0F0F0;text-align:left;font-size:14px}
TD{border-width: 1px;padding: 5px;border-style: solid;border-color: #C0C0C0;font-size:12px}
</style>
"@

Get-Content  "C:\all_pc_list.txt" | ForEach `
{
    $computer = $_
    If ( Test-Connection $computer -Count 1 -Quiet )
    {
        $IP = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled = True" -ComputerName $computer |
         Select -ExpandProperty IPAddress | Where { $_.substring(0,3) -eq "192" }
        $model = Get-WmiObject Win32_ComputerSystemProduct -ComputerName $computer |
         Select @{n="Serial";e={ $_.identifyingnumber }},
                @{n="Model";e={ If ( $_.Name -ne "" -and $_.Vendor -ne "LENOVO" )
                                { Write-Output "$($_.Name)" }
                                Else
                                { Write-Output "$($_.Version)" }
                              }}
        $ram = (Get-WmiObject Win32_PhysicalMemory -ComputerName $computer | ForEach { $_.Capacity } | Measure-Object -Sum).Sum / 1MB
        New-Object PsObject -Property @{ `
                                    "Computer Name" = $computer
                                    "IP" = $IP
                                    "PC serial" = $model.Serial
                                    "PC Model" = $model.Model
                                    "RAM Total MB" = $ram
                                    } |
         Select "Computer Name","IP","PC serial","PC Model","RAM Total MB"
    }
} | ConvertTo-Html -Head $head |
 Out-File $outFile -Encoding ascii

Open in new window

1
Ding QBvpAuthor Commented:
Hi,

Thanks and I am testing with a few host names.

Just concern that, before a long list of host names are input:
If each host has been visited then:
1. any resource burden to each host?
2. anything written in the host eventlog?

Regards
0
footechCommented:
For both questions, nothing different than the VBScript.  We're talking one ping, and three WMI queries so the resource usage will be negligible.  And nothing should be written to event logs.  If I remember right, then only WMI query that I've seen cause anything to be written to event logs is when using the Win32_Product class.
1
Ding QBvpAuthor Commented:
Thanks.

When I run in PowerShell ISE, it prompts that:

File C:\PC_html.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:0 char:0

Then I run:
PS C:\Users\********> Get-ExecutionPolicy
Restricted

Any config should I do? Regards.
0
footechCommented:
Reading the about_signing topic will tell you.  The first two sections in particular.  One of the best habits you can get into for learning (and continuing to use) PS is to read its built-in help.  If you're using PS 3.0+ and you haven't already, run Update-Help from an elevated session.

You'll use Set-ExecutionPolicy to allow running of scripts.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.