Windows Vista Forums

Body statement in a loop.

  1. #1


    Mark Irwin Guest

    Body statement in a loop.

    Hello all,
    I'm have a couple of things I'm trying to do in a script and I'm having
    problems, I was hoping someone here could help. I'm attempting to get UTC and
    local time which is easy enough with the current time statement, how ever it
    gets the list with out the machine name. I also want to pull the name from
    win32_operatingsystem property csname rather than trust that the server list
    file has the correct name rather than an alias. The problem is when I run
    this it excutes the first command and returns a value then it just prints the
    next command out rather than execute it.

    Any help is much appreciated!



    $TextFile = "C:\ServerList_Test.txt"
    $ServerList = [System.IO.File]::OpenText($TextFile)
    while($Machine = $ServerList.ReadLine())
    { Get-WmiObject win32_operatingsystem -computer $Machine | `
    Select-Object -Property CSName}
    { Get-WmiObject win32_currenttime -computer $Machine | `
    Select-Object -Property Year,Month,Day,Hour,Minute,Second | `
    Format-Table –auto }
    $ServerList.Close()


    Mark Irwin

      My System SpecsSystem Spec

  2. #2


    Brandon Shell Guest

    Re: Body statement in a loop.

    Does this do what your looking for?
    $TextFile = Get-Content "C:\ServerList_Test.txt"
    $result = @()
    foreach($Srv in $textFile)
    {
    Write-Host (Get-WmiObject Win32_OperatingSystem -ComputerName
    $Srv).CSName
    $time = Get-WmiObject Win32_CurrentTime -ComputerName $Srv
    $time | Format-Table Year,Month,Day,Hour,Minute,Second -Auto
    }

    Another option is to Create a custom Object so you can play with it later
    Understand this is a little more verbose, but much more powerful
    $TextFile = Get-Content "C:\ServerList_Test.txt"
    $result = @()
    foreach($Srv in $textFile)
    {
    $sObject = "" | select
    Server,Year,Month,Day,Hour,Minute,Second,utcYear,utcMonth,utcDay,utcHour,utcMinute,utcSecond
    $sObject.Server = (Get-WmiObject Win32_OperatingSystem -ComputerName
    $Srv).CSName
    $time = Get-WmiObject Win32_CurrentTime -ComputerName $Srv
    # Local
    $sObject.Year = $time[0].Year
    $sObject.Month = $time[0].Month
    $sObject.Day = $time[0].Day
    $sObject.Hour = $time[0].Hour
    $sObject.Minute = $time[0].Minute
    $sObject.Second = $time[0].Second
    # UTC Properties
    $sObject.utcYear = $time[1].Year
    $sObject.utcMonth = $time[1].Month
    $sObject.utcDay = $time[1].Day
    $sObject.utcHour = $time[1].Hour
    $sObject.utcMinute = $time[1].Minute
    $sObject.utcSecond = $time[1].Second
    $result += $sObject
    }

    $result | Format-Table Server,Year,Month,Day,Hour,Minute,Second
    $result | Format-Table
    Server,utcYear,utcMonth,utcDay,utcHour,utcMinute,utcSecond


    "Mark Irwin" <Mark Irwin @discussions.microsoft.com> wrote in message
    news:A3CC633C-57AE-4CBA-895F-69CB7CD731CB@microsoft.com...
    > Hello all,
    > I'm have a couple of things I'm trying to do in a script and I'm having
    > problems, I was hoping someone here could help. I'm attempting to get UTC
    > and
    > local time which is easy enough with the current time statement, how ever
    > it
    > gets the list with out the machine name. I also want to pull the name from
    > win32_operatingsystem property csname rather than trust that the server
    > list
    > file has the correct name rather than an alias. The problem is when I run
    > this it excutes the first command and returns a value then it just prints
    > the
    > next command out rather than execute it.
    >
    > Any help is much appreciated!
    >
    > $TextFile = "C:\ServerList_Test.txt"
    > $ServerList = [System.IO.File]::OpenText($TextFile)
    > while($Machine = $ServerList.ReadLine())
    > { Get-WmiObject win32_operatingsystem -computer $Machine | `
    > Select-Object -Property CSName}
    > { Get-WmiObject win32_currenttime -computer $Machine | `
    > Select-Object -Property Year,Month,Day,Hour,Minute,Second | `
    > Format-Table –auto }
    > $ServerList.Close()
    >
    >
    > Mark Irwin



      My System SpecsSystem Spec

Body statement in a loop.

Similar Threads
Thread Thread Starter Forum Replies Last Post
for ... loop through file collection instead of for each ... loop Ranah van Rijswijk VB Script 2 21 Feb 2010
End of statement error Terry VB Script 2 13 Feb 2009
Help with SQL statement Cmor VB Script 3 08 Sep 2008
just a statement Communikator Vista General 7 18 Oct 2007