Error Running Powershell Script to Get Service Pack Version

$ServersName = (Get-Content C:\WinUpVer\Updates2.txt)

{

 	$build = @{n="Build";e={$_.BuildNumber}}
	$SPNumber = @{n="SPNumber";e={$_.CSDVersion}}
	$sku = @{n="SKU";e={$_.OperatingSystemSKU}}
	$hostname = @{n="HostName";e={$_.CSName}}

   $Win32_OS = Get-WmiObject Win32_OperatingSystem -computer $ServerName | select $build,$SPNumber,Caption,$sku,$hostname, servicepackmajorversion

   ## Get the Service pack level

   $servicepack = $Win32_OS.servicepackmajorversion

      { $os = 'Server 2008'}; break
     

   "Operating System: $os Service Pack: $servicepack"
   "Operating System: $os Service Pack: $servicepack" | out-file -filepath C:\Boxing\ServicePack.txt

   }

Open in new window

I am getting an error running a script to get the Service Pack version from a list of Windows servers read from a text file.  'I am getting the code I scripted as output'. Nor is the output file as specified being created. See the attached input text file. See below. This is what I am getting as output to the screen when executed within the ISE:

 $build = @{n="Build";e={$_.BuildNumber}}
    $SPNumber = @{n="SPNumber";e={$_.CSDVersion}}
    $sku = @{n="SKU";e={$_.OperatingSystemSKU}}
    $hostname = @{n="HostName";e={$_.CSName}}

   $Win32_OS = Get-WmiObject Win32_OperatingSystem -computer $ServerName |
select $build,$SPNumber,Caption,$sku,$hostname, servicepackmajorversion

   ## Get the Service pack level

   $servicepack = $Win32_OS.servicepackmajorversion

      { $os = 'Server 2008'}; break
     

   "Operating System: $os Service Pack: $servicepack"
   "Operating System: $os Service Pack: $servicepack" | out-file -filepath
C:\Boxing\ServicePack.txt

If anyone has a solution to this script or a script that will generate a Service Pack report by reading from a CSV or TXT file, it would greatly appreciated.

See code snippet to review the script I am attempting to execute.

Lipotech
Updates2.txt
lipotechSys EngAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

lipotechSys EngAuthor Commented:
I created a simpler version of the script, but I am still getting the source code as output to the ISE screen and in the output file.  I want to discard the original version of the script previously posted and work on this latest version of the script.  Once again, if anyone  has a script that will read a list of servers from a text file, determine the Service Pack level and output the results of the query to a .csv file I greatly appreciate it.

See latest version below:

$sServer = (Get-Content C:\WinUpVer\Updates2.txt)

{ $sOS =Get-WmiObject -class Win32_OperatingSystem -computername $sServer

foreach($sProperty in $sOS)
 {
    write-host $sProperty.Description
    write-host $sProperty.Caption
    write-host $sProperty.OSArchitecture
    write-host $sProperty.ServicePackMajorVersion
 } 
 
 } | Export-Csv -NoTypeInformation -Path "C:\WinUpVer\servicePack.csv"

Open in new window

lipotechSys EngAuthor Commented:
I used "SilentlyContinue", but I continue to get the source output to the output file and the ISE screen.

Any suggestions?

Lipotech
$sServer = (Get-Content C:\WinUpVer\Updates2.txt)

{ $sOS =Get-WmiObject -class Win32_OperatingSystem -computername $sServer

foreach($sProperty in $sOS)
 {
    write-host $sProperty.Description
    write-host $sProperty.Caption
    write-host $sProperty.OSArchitecture
    write-host $sProperty.ServicePackMajorVersion
 } 
 
 } | Export-Csv -NoTypeInformation -Path "C:\WinUpVer\servicePack.csv"

Open in new window

footechCommented:
Just referencing your original - You have to loop through each of your server names that you read in on line 1.  After that you have a scriptblock (within "{ }"), but you're not executing it - that's why it just outputs to screen.

The modification to loop is rather simple.  But then you have change how you're writing to a file - you either need to use -append within a loop (which is inefficient, but fine for small tasks), or use the ForEach-Object cmdlet instead of the foreach statement and make use of the pipeline (the foreach statement doesn't work directly with the pipeline).
$ServersName = (Get-Content C:\WinUpVer\Updates2.txt)
foreach ($ServerName in $ServersName)
{

 	$build = @{n="Build";e={$_.BuildNumber}}
	$SPNumber = @{n="SPNumber";e={$_.CSDVersion}}
	$sku = @{n="SKU";e={$_.OperatingSystemSKU}}
	$hostname = @{n="HostName";e={$_.CSName}}

   $Win32_OS = Get-WmiObject Win32_OperatingSystem -computer $ServerName | select $build,$SPNumber,Caption,$sku,$hostname, servicepackmajorversion

   ## Get the Service pack level

   $servicepack = $Win32_OS.servicepackmajorversion

      { $os = 'Server 2008'}; break
     

   "Operating System: $os Service Pack: $servicepack"
   "Operating System: $os Service Pack: $servicepack" | out-file -filepath C:\Boxing\ServicePack.txt -append

   }

Open in new window

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
lipotechSys EngAuthor Commented:
Thank you for the concise instructions.
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
Windows Server 2008

From novice to tech pro — start learning today.