Scripting Games–how not to output data

I haven’t finished blogging about event 1 yet but this caught my eye.

Things aren’t too bad until we hit the bunch of  write-host calls

$wrks = (Get-Content -path C:\IPList.txt)
foreach ($wrk in $wrks)
{
    $osver = Get-WMIObject -class win32_operatingsystem -ComputerName $wrk
    $procs = @(Get-WMIObject -class win32_processor -ComputerName $wrk)
    $psok=($procs.SocketDesignation).count
    $pcors=(($procs.numberofcores[0])*$psok)
    $plog=($pcors * 2)
    $psped=$procs.MaxClockSpeed[0]
    $mem = Get-WMIObject -class win32_physicalmemory -ComputerName $wrk
    $memtotal = ($mem | Measure-Object -Property capacity -Sum)
    $memgb = $memtotal.sum/1gb
Write-host “*******************************************************”
Write-Host “Machine Name: ” $osver.CSName
Write-Host “OS: “$osver.caption
Write-Host “Service Pack: “$osver.csdversion
Write-Host “Build #: “$osver.version
Write-Host “*********** “
Write-Host “Memory Installed:”
Write-Host “*********** “
Write-Host “Memory (GB): $memgb “
Write-Host “Slots used:” $memtotal.Count
Write-Host “*********** “
Write-Host “Processor(s) Installed:”
Write-Host “*********** “
Write-Host “Sockets:” $psok
Write-Host “Cores:” $pcors
Write-Host “Logical Procs:” $plog
Write-Host “*********** “
Write-Host “Processor Details:”
Write-Host “*********** “
$procs
Write-Host “”
}

The correct way is to create an object and output that

I’ll be blogging a sample answer when the games are over.  for now be aware that write-host is worse than backticks

This entry was posted in Powershell, PowerShell and WMI, Scripting Games. Bookmark the permalink.

2 Responses to Scripting Games–how not to output data

  1. Jon says:

    I use Write-Host all the time. However, usually just when I’m giving myself feedback on where the script is. If it’s a script that I really want to polish off, I’ll convert to using Write-Progress and give myself fancy progress bars. But I guess I disagree that Write-Host is worse than backticks, because I’d say there’s hardly any reason to ever use backticks, while Write-Host does have a place. Especially for the busy admin that doesn’t have time to write fancy scripts, and just wants to know the script is chugging along with some kind of progress… e.g. writing to the host where you’re at in a foreach loop. Since I won’t need to do more with the “progress” indicators for my loop, Write-Host is simple and easy and good enough for me.

    In the example you give above, yes, using Write-Host is bad.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s