powershell - outfile question...


hi;

i have line of code writes $computer variable text file , works me; however, i'd add $date + times variables along $computer each line in text file contains hostname along date , time. possible? in advance, doing head in

$computer | out-file c:\vncrestarted.txt -append

i of this.

i've adjusted code include doubled quoted texty bits...

will save copy and email myself ;)

# setup trap catch exceptions
trap [exception]
{
 write-error $("trapped: " + $_.exception.message);
}
 
# read computers text file
$computers = get-content c:\computers.txt;
$start = $true;
 
# setup service array service names want check running
$servicearray = 'uvnc_service';
 
# powershell knows it's array working simple
foreach($computer in $computers)
{
 write-host "checking $computer";
 $objwmiservice = get-wmiobject -class win32_service -computer $computer
 
 foreach($service in $objwmiservice)
 { 
  # check each service specicfied in $servicearray
  foreach($srv in $servicearray)
  {
   if($service.name -eq $srv)
   {
    write-host "$srv present on $computer.";
    if($service.state -eq "running")
    {
     write-host "$srv running on $computer";
    }
    else
    {
     write-host "$srv not running on $computer";
     # if $start true script attempt start service if stopped
     if($start -eq $true)
     {
      # attempt start current service on current computer
                        $serviceinstance = (get-wmiobject -computer $computer win32_service -filter "name='$srv'");
      $name = $serviceinstance.name;
                       
                        # create variable hold date , write variables text file
                        $now = get-date
                        echo $now $computer "the vnc service has now started." >> c:\vncrestarted.txt
                        
                        write-host "attempting start $name on $computer..."
      $serviceinstance.startservice() | out-null;
      
                        # refresh object instance new data
      $serviceinstance = (get-wmiobject -computer $computer win32_service -filter "name='$srv'");
      $state = $serviceinstance.state;
      write-host "$name ""$state"" on  $computer.";  
     }
    }
   }
  }
 }
}




Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

some help on Event 540

WMI Repository 4GB limit - Win 2003 Ent Question

Event ID 1302 (error 1307) DFS replication service encountered an error while writing to the debug log file