Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each iteration problem

Status
Not open for further replies.

SQLHacker

MIS
Aug 10, 2006
45
US
I'm using a PS script to gather all of the scheduled tasks on a number of servers. I have the server names saved in a .txt file, and am referencing that in the PS script. The problem I'm having is that no matter what order I put the server names in, the output only shows the last server name in the list (i.e. it's not outputing all the tasks on all the servers, just all the tasks on the last server listed).

Here's the PS script I'm using:
Code:
$arrServers = Get-Content -path "C:\PowerShellScripts\ScheduledTasks\ServerList.txt"

ForEach ($strServer in $arrServers){
	schtasks /QUERY /S $strServer /FO CSV /V | Set-Content "C:\PowerShellScripts\ScheduledTasks\ScheduledTasks.csv"

}

Thanks in advance for any suggestions.
 
Hi

I think you want to use the Add-Content cmdlet instead of the Set-Content cmdlet. The Set-Content overwrites the data each time that is why you only see the last server.
 
Lundkvist,

Thanks for your response. I made the change to "Add-Content" and it didn't make a difference. Still only writing the scheduled tasks from the last server in the list.
 
You might want to use Add-Content since Set-Content may be overwriting the file for each computer and therefore only the last entry shows....or use >> to redirect the output which is also valid with PS.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
oops...sorry, i see the add-content was already suggested....maybe try the >> then.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
What if you tried

Code:
$arrServers = Get-Content C:\Temp\Computers.txt
foreach ($strServer in $arrServers)
{
    schtasks /QUERY /S $strServer /FO CSV /V | Out-File C:\Temp\Output.csv -Append
}

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks for your responses. I found a pretty slick solution at ScriptingAnswers.com. Here is the function that a guy gave me (it's pretty cool!).
Code:
Function Get-ScheduledTask {
  Param([string]$computername=$env:computername)
  
  #set to "continue to enable debugging messages"
  $debugPreference="SilentlyContinue"
  
  $tmpFile=Join-Path $env:temp "gsctmp.csv"
  
  Write-Debug "Getting SCHTASKS.EXE for $computername"
  $s=schtasks /query /s $computername /fo csv /v

  if (-not $s) 
  { 
    Write-Debug "No connection to $computername"
    Write-Warning ("Failed to find {0}" -f $computername.ToUpper())
    #exit
    return 
  }
  if ($s -is [string]) 
  {
    Write-Debug "No scheduled tasks found for $computername"
    Write-Warning ("{0} {1}" -f $computername.ToUpper(),$s)
    #exit 
    return
  }
  
  Write-Debug "Sending data to $tmpFile"
  $s[1..($s.count-1)] | Out-File $tmpFile  -force
  
  Write-Debug "Importing $tmpFile"
  
  Import-Csv $tmpFile
  
  Write-Debug "Deleting $tmpFile"
  
  Remove-Item $tmpFile
}
get-content C:\PowerShellScripts\ScheduledTasks\serverlist.txt | foreach {get-scheduledtask $_ } | Select Hostname,Taskname,"Next Run Time","Last Run Time",Creator,"Run As User","Task To Run","Scheduled Task State","Scheduled Type",Schedule | Export-CSV C:\PowerShellScripts\ScheduledTasks\Report.CSV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top