Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have been working on implementing the warm-up script from Ingo Karstein Warm-Up Script

The script runs without any issues, but the log file (.txt) is not being generated as expected. I have the scripts in a directory c:\Tools\Scripts

# SharePoint Warmup Script
# by Ingo Karstein
# 2011/01/26
# 2011/08/02

 #<---- Improvement starts here
#region MyWebClient
Add-Type -ReferencedAssemblies "System.Net" -TypeDefinition @"
using System.Net;

public class MyWebClient : WebClient
{
    private int timeout = 60000;

    public MyWebClient(int timeout)
    {
        this.timeout = timeout;
    }

    public int Timeout
    {
        get
        {
            return timeout;
        }
        set
        {
            timeout = value;
        }
    }

    protected override WebRequest GetWebRequest(System.Uri webUrl)
    {
        WebRequest retVal = base.GetWebRequest(webUrl);
        retVal.Timeout = this.timeout;
        return retVal;
    }
}
"@
#endregion
#----> Improvement ends here

$urls= @("http://cardinals.local")

New-EventLog -LogName "Application" -Source "SharePoint Warmup Script" -    
ErrorAction      SilentlyContinue | Out-Null

$timeout = 60000 #=60 seconds               #<--- Improvement!

$urls | % {
$url = $_
try {
    $wc = New-Object MyWebClient($timeout)        #<--- Improvement!
    $wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    $ret = $wc.DownloadString($url)
    if( $ret.Length -gt 0 ) {
        $s = "Last run successful for url ""$($url)"": $([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss'))" 
        $filename=((Split-Path ($MyInvocation.MyCommand.Path))+"lastrunlog.txt")
        if( Test-Path $filename -PathType Leaf ) {
            $c = Get-Content $filename
            $cl = $c -split '`n'
            $s = ((@($s) + $cl) | select -First 200)
        }
        Out-File -InputObject ($s -join "`r`n") -FilePath $filename
    }
} catch {
      Write-EventLog -Source "SharePoint Warmup Script"  -Category 0 -ComputerName "." -EntryType Error -LogName "Application" `
        -Message "SharePoint Warmup failed for url ""$($url)""." -EventId 1001

    $s = "Last run failed for url ""$($url)"": $([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss')) : $($_.Exception.Message)" 
    $filename=((Split-Path ($MyInvocation.MyCommand.Path))+"lastrunlog.txt")
    if( Test-Path $filename -PathType Leaf ) {
      $c = Get-Content $filename
      $cl = $c -split '`n'
      $s = ((@($s) + $cl) | select -First 200)
    }
    Out-File -InputObject ($s -join "`r`n") -FilePath $filename
}
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Set the $filename variable to some other location like "c:\lastrunlog.txt". The log file will be generated with complete output.

Try the updated script below:

#<---- Improvement starts here
#region MyWebClient
Add-Type -ReferencedAssemblies "System.Net" -TypeDefinition @"
using System.Net;

public class MyWebClient : WebClient
{
    private int timeout = 60000;

    public MyWebClient(int timeout)
    {
        this.timeout = timeout;
    }

    public int Timeout
    {
        get
        {
            return timeout;
        }
        set
        {
            timeout = value;
        }
    }

    protected override WebRequest GetWebRequest(System.Uri webUrl)
    {
        WebRequest retVal = base.GetWebRequest(webUrl);
        retVal.Timeout = this.timeout;
        return retVal;
    }
}
"@
#endregion
#----> Improvement ends here

$urls= @("http://cardinals.local")
$filename="c:\lastrunlog.txt"
New-EventLog -LogName "Application" -Source "SharePoint Warmup Script" -    
ErrorAction      SilentlyContinue | Out-Null

$timeout = 60000 #=60 seconds               #<--- Improvement!

$urls | % {
$url = $_
try {
    $wc = New-Object MyWebClient($timeout)        #<--- Improvement!
    $wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    $ret = $wc.DownloadString($url)
    if( $ret.Length -gt 0 ) {
        $s = "Last run successful for url ""$($url)"": $([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss'))" 
        if( Test-Path $filename -PathType Leaf ) {
            $c = Get-Content $filename
            $cl = $c -split '`n'
            $s = ((@($s) + $cl) | select -First 200)
        }
        Out-File -InputObject ($s -join "`r`n") -FilePath $filename
    }
} catch {
      Write-EventLog -Source "SharePoint Warmup Script"  -Category 0 -ComputerName "." -EntryType Error -LogName "Application" `
        -Message "SharePoint Warmup failed for url ""$($url)""." -EventId 1001

    $s = "Last run failed for url ""$($url)"": $([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss')) : $($_.Exception.Message)" 
    if( Test-Path $filename -PathType Leaf ) {
      $c = Get-Content $filename
      $cl = $c -split '`n'
      $s = ((@($s) + $cl) | select -First 200)
    }
    Out-File -InputObject ($s -join "`r`n") -FilePath $filename
}
}
share|improve this answer
    
Tested and verified! –  Falak Mahmood Jan 6 '13 at 0:25
    
the script works great! I have created a job in Task Scheduler, but when I run the job the log file is not updating. The command I'm running is C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -command sp2010warmup.ps1 –  Jason Jan 8 '13 at 3:16
    
I found a solution, I created a batch file, and added powershell -command "& 'E:\Tools\Scripts\sp2010warmup.ps1'", then I configured the Task Scheduler to execute the batch file. Everything is working now. Thanks, Jason –  Jason Jan 8 '13 at 3:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.