• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

powershell and IIS help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

SpyderMatrix

Member
Joined
Jan 17, 2002
Location
VA
I need a powershell script to do the following.

i need it to parse the IIS log locally on the server for a soap call if it finds the soap call then the script can stop. if it does not find the soap call i need it to restart IIS. This script will then be run every 30 minutes using a scheduled task.


any help would be appricated, I am still learning powershell and I am not that far in.
 
That's a relatively easy task for Powershell, you ought to be able to piece something together with a half hour's worth of googling.

Not tested, but it ought to be about as easy as this:

Code:
$LOG="C:\path\to\log"
$PATTERN="::SOAP::"
if(Get-Content $LOG | findtr $PATTERN)
{
    Restart-Service iis.exe //Obviously you'd need to put the actual service name here.
}

If it's a rolling log file that doesn't get purged after each restart, then you'll only want to check the last X lines or something, that could get a little tricky, but still easily doable.

Top Google results:
Reset IIS in Powershell
Find a string in a log file
 
ty for the reply and the links yes its a rolling log you got me starting in the right direction kudos to you thats why i love this communiity
 
In that case, I assume there's a timestamp at the beginning of each log entry. So you could calculate the range of time you're looking for, and your pattern could just be a regex to ensure that the string you're looking for starts with a timestamp that's in your window, followed by some characters, and then your SOAP call.
 
i decided to take a different route but no matter what i use for a url it tells me basically the site is down even if i use google which i can browse to with no problem

Code:
#IIS check script to reach out the the application pool check to see if the pool is working if it fails either
#checkpoint it will restart the application pool  and then restart IIS.
#Written 8/2014

# this creates a log file with a date and time stamp

$logfile = "C:\IIS logs\IIS-AppPool-Check_$(get-date -format `"yyyy-MM-dd_hh-mm-sstt`").txt"
 
#this is our logging function

function log($string, $color)
{
   if ($Color -eq $null) {$color = "white"}
   write-host $string -foregroundcolor $color
   $string | out-file -Filepath $logfile -append
}


#Error showing section  with errors currently suppressed 
$ErrorActionPreference = "SilentlyContinue"

#The URL to the server.
[string] $url = "http://google.com"

# First we create the request.
[net.httpWebRequest] $req = [net.webRequest]::create($url)
$req.method = "HEAD"

# We then get a response from the site.
$HTTP_Response = $HTTP_Request.GetResponse()

# We then get the HTTP code as an integer.
$HTTP_Status = [int]$HTTP_Response.StatusCode

If ($HTTP_Status -eq "200") { 
   # Write-Host "IIS is working!" 
	log "IIS is working"
}
Else {
        #$If ping is unsuccessful this section will show
    
   # Write-Host -ForegroundColor red "IIS has malfunctioned, Restarting IIS"
    log "IIS has malfunctioned, Restarting IIS"
    
   # Restart Of IIS and Application pool
 
 #IIS Restart script section
 
    invoke-command -scriptblock {iisreset}
    Start-Sleep -s 10
   # Write-Host -ForegroundColor green "IIS restarted"
    log "IIS restarted"
    
#Application Pool( used for IIS6 only will not work on later versions of IIS)

    # Write-Host "Restarting Application Pool"
    log "Restarting Application Pool"
    $appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | where-object {$_.Name -eq "W3SVC/AppPools/Keyfile"}
$appPool.Start()
   
    Start-Sleep -s 10
    # Write-Host -ForegroundColor green "Application Pool restarted"
    log "Application Pool restarted"
}

# Finally, we clean up the http request by closing it.
$HTTP_Response.Close()
 
Last edited:
Ironically enough, I just implemented something nearly identical to this at work a week ago. Here's a the guts of my code (most of which I shamelessly got from the Google), trimmed down to fit what you're trying to do.

Code:
# List of URLs to test
$URLs = @("http://google.com","http://sitethatdoesntexist.com")

# Empty array to store our results
$Result = @()    

# Loop through our URLs
Foreach($Uri in $URLs) { 
	$time = try{ 
		$request = $null 
		 ## Request the URI, and measure how long the response took. 
		$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri } 
		$result1.TotalMilliseconds 
	}  
	catch 
	{ 
		 <# If the request generated an exception (i.e.: 500 server 
		 error or 404 not found), we can pull the status code from the 
		 Exception.Response property #> 
		 $time = -1 
		 $request = $_.Exception.Response 
	}   
	# Custom object to hold our information
	$result += [PSCustomObject] @{ 
		Time = Get-Date; 
		Uri = $uri; 
		StatusCode = [int] $request.StatusCode; # Status code of our request, 404, 500, etc.
		StatusDescription = $request.StatusDescription; # Brief description of status code
		TimeTaken =  $time;  # Time it took to get a response
		Content = $request.Content; # Page contents
	}
} 

if($result -ne $null) 
{ 
	# Set up some mail variables
	# SMPT settings here
	
	# Loop through the results of our URLs, send notifications if request was unsuccessful.
    Foreach($Entry in $Result) 
    { 
		# If we get any return other than 200, send alert.
        if($Entry.StatusCode -ne "200")
        { 
			Write-Host "IIS is not working at $($Entry.Uri)"
        } 
		else # A valid page was served to us.
		{
			Write-Host "IIS is working at $($Entry.Uri)"
		}
    } 
}

Output:
Code:
PS C:\Users\user\desktop> .\test.ps1
IIS is working at http://google.com
IIS is not working at http://sitethatdoesntexist.com

The forum is apparently stripping out the ") at the end of the $URLs declaration line for some reason, and then it's running on the comment of the line below.. quirky. You'll have to change that to get it to work, but should be fairly obvious what needs to be done.

[edit]
I should note that Invoke-WebRequest requires Powershell 3.0 or higher, if you're on 1 or 2, you'll need to install the latest WMF package.
 
unfortunately this will be used on an old 2003 server which does not allow for powershell 3.0 to be installed. We are working with the vendor to get an updated software package for the actual software so that we can move to 2008 or 2012 but in the mean time we are stuck
 
Ah, that makes sense. In that case, I took a look at your code. Your problem is just that you got your variables mixed up a bit:

Code:
$HTTP_Response = [b]$HTTP_Request.GetResponse()[/b]

Should be
Code:
$HTTP_Response = [b]$req.GetResponse()[/b]
 
Back