Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

During Foreach read another foreach in powershell

$
0
0

Hi all powershell gurus out there. I have a foreach which opens a URL from URLListl.txt and doing a Measure-command on them. The result from Measure-command writes to event-log. In another text file i have country list, like: USA Canada Brazil and so on.... I want in write eventlogs Message to read from this Text file and write it with the Measure-command result in Message part of the eventlog. The below is my script which works fine but it creates two entries for each URL.

function Measure-Site

{

    $URLListFile = "C:\yourname\URLList.txt"

$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue

Foreach ($Uri in $URLList)

       {

 

   $Time = Measure-Command {

C:\yourname\MainScript.ps1}

$Time.TotalSeconds

 

 

 

    $countrycode = (Get-Content c:\yourname\URLListcountry.txt)

      foreach ($SiteCode in $CountryCode)

            {

       $LogFileExist = Get-EventLog -list | Where-Object { $_.LogDisplayName -eq "Scriptcheck" }

if (! $LogFileExist)

{

New-EventLog -LogName "Scriptcheck" -Source "Scripts"

}

if ($Time.Totalseconds -lt 25)

{

Write-EventLog -LogName "Scriptcheck" -Source "Scripts" -EntryType information -EventId 100 -Message " $SiteCode `nTotal Time: $Time"

}

elseif ($Time.Totalseconds -gt 25)

{

Write-EventLog -LogName "Scriptcheck" -Source "Scripts" -EntryType warning -EventId 101 -Message " $SiteCode  `nTotal Time: $Time"

}  

            }

            }

}

 

if (Get-Process -name iexplore -ErrorAction SilentlyContinue)

    {

Stop-Process -Name iexplore

    }

Measure-Site


Viewing all articles
Browse latest Browse all 6937

Trending Articles