I have a PS script which monitors all websites listed in text file. As of now i i am running it as a job so whenever any of website goes down it throws an alert on registered mail ID with the HTML file as an attachment consisting of all website status. What i want now that it only pick the those websites which goes down and throes me an alert. Can someone help me here please.
Script:-
$URLListFile = "C:\Users\Desktop\URL.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
Foreach($Uri in $URLList) {
$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 #>
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
TimeTaken = $time;
}
}
#Prepare email body in HTML format
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>TimeTaken</B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
send-mailmessage -to "abc#gmail.com" -from "abc#gmail.com" -subject "Test mail" -SmtpServer XXX####.com -Attachments "C:\Users\Desktop\Test.htm"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)<TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport | out-file C:\Users\Desktop\Test.htm
$Outputreport += "</Table></BODY></HTML>"
}
I can think of two ways to do this. First I assume you are trying to capture each item from $results where the status code is anything other than a success code. If this assumption is correct then you already have most of the solution in your code. The following line:
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
send-mailmessage -to "abc#gmail.com" -from "abc#gmail.com" -subject "Test mail" -SmtpServer XXX####.com -Attachments "C:\Users\Desktop\Test.htm"
}
is identifying those records, so you can simply grab them here in the loop. Like this:
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
send-mailmessage -to "abc#gmail.com" -from "abc#gmail.com" -subject "Test mail" -SmtpServer XXX####.com -Attachments "C:\Users\Desktop\Test.htm"
$failedCodeUri += $Entry.uri
}
In the above snippet the variable $failedCodeUri must be declared with the scope necessary to be accessible inside and outside of the foreach loop. This can be done as simply as declaring the variable on a line immediately preceding the beginning of the foreach loop. It also must be a type that can utilize the += operator. This can be done with a simple array like this.
$failedCodeUri = #()
Of course if you need to capture more than just the uri you will need a more sophisticated data type than a simple array of strings.
The other possible solution depends on the 'where' method being available on your $results object. If it is available you can simply do this:
$failedCodeUri = $results.where({$_.StatusCode -ne "200"})
This last technique would not be used inside the loop. Either before or after your foreach loop you can use this line of code to return all of the desired results.
A few things to note about this technique:
Within the 'where' code block the condition is being checked for each item in the result set. You reference the current item with $_ using the dot operator to access a property or method that exists for that item.
In the above example the entire item, with all of its properties will be returned. If you only want the uri you can do something like this:
$failedCodeUri = $results.where({$_.StatusCode -ne "200"}).uri
Related
I have the following PowerShell Script, found and modified some time ago, however after checking and testing it in the past, I put this project away and wanted to check again now. However, the script should check a given website and tell how long it took to reach etc, but when I want to check with an internal website it is not working and constantly showing the cells with red color and error 200, however, external website like google or Facebook are measurable by the script.
Here is the script:
$URLListFile = "C:\Users\***\Desktop\websites.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
Foreach($Uri in $URLList) {
$time = try{
$request = $null
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliseconds
}
catch
{
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file C:\Users\***\Desktop\Test.htm
Invoke-Expression C:\Users\***\Desktop\Test.htm
Also attaching the picture about the result. Would you be able to help me why internal websites can not be measured, how should this script be modified so it can work? Website Availability Report
error
Okay, so adding this line solved my issue, as the webpage is not accepting SSL 1.0 but 1.2 only connection. [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
I created the following script to check the website status and the IIS instance status. When I manually runs it from PowerShell ISE with Adminstrator, it works fine but when I triggered from Task Scheduler, it just didn’t worked! And it was due to GET-WEBSITESTATE as this cmdlet required to run from PS ISE Administrator.
My question, what is the command need to setup in Task Scheduler?
Currently, i setup the task schedule with the following:
Program/Script: Powershell.exe
Add arguments: powershell -executionpolicy bypass -file "D:\PS\CheckPMRWebpage\Code\Monitor PMR webpage 6-1.ps1
## The URI list to test
$URLListFile = "D:\PS\CheckPavcapWebpage\Code\PavCapurlList.DAT"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
Foreach($Uri in $URLList) {
$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 #>
$request = $_.Exception.Response
$time = -1
}
# $lStatusCode = [int] $request.StatusCode
# if ($lStatusCode -eq 401){
# $linfo = "The site is ok."
# }
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
##################################################
#Prepare body in HTML format for URL
##################################################
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>PMR Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> PavCap Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>Additional Info</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD></TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -eq "200")
{
$Outputreport += "<TR bgcolor=lightgreen>"
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center></TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
elseif($Entry.StatusCode -eq "401")
{
$Outputreport += "<TR bgcolor=yellow>" #<TD></TD><TD align=center></TD><TD align=center></TD><TD align=center>$($Entry.AddInfo)</TD><TD align=center></TD><TD align=center></TD></TR>"
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>Website is up but it failed on Authentication!</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
#AddInfo = "Site is ok as it failed on Authentication"
}
else
{
$Outputreport += "<TR bgcolor=red>"
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>Website is not up - please check!</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
#AddInfo = "red"
}
#$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.AddInfo)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file D:\PS\CheckPavcapWebpage\siteavailabilityrpt.html
#Invoke-Expression D:\PS\CheckWebpage_and_IIS\siteavailabilityrpt.html
######################################
# The Instances Name list to check
######################################
$InsNameFile = "D:\PS\CheckPavcapWebpage\Code\PavCapInsName.dat"
$InsList = Get-Content $InsNameFile -ErrorAction SilentlyContinue
$Result1 = #()
Foreach($Ins in $InsList){
$InsNameState = (Get-WebsiteState -Name $Ins).value
$result1 += [PSCustomObject] #{
#Time = Get-Date;
InsName = $ins;
InsStatus = $InsNameState;
}
}
##################################################
#Prepare body in HTML format for Instance
##################################################
if($result1 -ne $null)
{
$Outputreport = "<HTML><TITLE>PMR Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> PavCap Instance Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>Instance Name</B></TD><TD><B>InsStatus</B></TD></TR>"
Foreach($Entry1 in $Result1)
{
if($Entry1.InsStatus -eq "Started")
{
$Outputreport += "<TR bgcolor=lightgreen>"
}
else
{
$Outputreport += "<TR bgcolor=red>"
}
$Outputreport += "<TD align=center>$($Entry1.InsName)</TD><TD align=center>$($Entry1.InsStatus)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
$Outputreport +="<footer><p>This is a system generated email, please do not reply to this email, thank you. For time being, the email addresses are controlling from the script until solution found.</p></footer>"
}
$Outputreport | out-file D:\PS\CheckPavcapWebpage\siteavailabilityrpt.html -Append
$htmlfilename = "D:\PS\CheckPavcapWebpage\siteavailabilityrpt.html"
############################################################################
## Email to users (hardcoding email address on the top of scirpt - declare
###########################################################################
$htmlbodyreport = Get-Content "$htmlfilename" -Raw
Send-MailMessage -to $lrecipients -from $lsender -Subject $lemailsubject -BodyAsHtml -body $htmlbodyreport -SmtpServer "abc.bab.cbb.com"
I got a little powershell problem troubling me for quite a while now.
Im trying to get information from a RSS site. I download the XML and go through it. I just want certain stuff from it. That for I use .document.getElementByID().outerText
The problem is that somehow it pulls the first information correctly but after that everything fails he just picks random text or just keeps the one text from the beginning without refreshing the variable. Also Powershell ISE says "You cannot call a method on a null-valued expression." randomly.
Here is my code:
<#
AUTHOR: KOCH,MICHAEL [GRE-IT]
DESCRIPTION: RSS READER
DATE: 28.06.17
DATE LAST WRITTEN: 19.07.17
LAST CHANGE:
#>
$debug = 1 #DEBUG
$receiver="A#MailAdress.com"
$sender="A#MailAdress.com"
$smtp="A.SMTP.SERVER"
$encoding = [System.Text.Encoding]::UTF8
$path_config = "C:\RSS\Zoll\config.txt"
$output = "C:\RSS\Zoll\meldung.html"
$output_edit_path = "C:\RSS\Zoll\meldung_edit.html"
$nmbr=0
$count=0
Invoke-WebRequest -Uri 'http://www.zoll.de/SiteGlobals/Functions/RSSFeed/DE/RSSNewsfeed/RSSZollImFokus.xml' -OutFile C:\RSS\Zoll\meldungen.xml -ErrorAction Stop
[xml]$content = Get-Content C:\RSS\Zoll\meldungen.xml
$feed = $content.rss.channel
$tag = #()
if($lines=Get-Content $path_config | Measure-Object -Line) #gets the number of lines
{
while($count -ne $lines.Lines)
{
if($entrys=(Get-Content $path_config)[$nmbr]) #gets the entrys from config.txt and goes through line by line
{
$entrys >> $tag[$nmbr]
if ($debug -eq 1)
{
Write-Output "$tag[$nmbr]"
Write-Output "$entrys"
Write-Output "$count"
}
}
$count++
$nmbr++ #jumps into the next line
}
}
$ie = New-Object -ComObject "InternetExplorer.Application"
Foreach($msg in $feed.Item)
{
$link = ($msg.link)
$subject = ($msg.title)
$ie.navigate("$link")
#$return = Invoke-WebRequest -Uri $link -OutFile "C:\RSS\Zoll\link.html"
$return = $ie.document
$innertext = $return.documentElement.document.getElementById("main").outerText
$body = $innertext#.Replace('Ä', 'Ä')
<#
$body = $innertext.Replace('ä', 'ä')
$body = $innertext.Replace('Ö', 'Ö')
$body = $innertext.Replace('ö', 'ö')
$body = $innertext.Replace('Ü', 'Ü')
$body = $innertext.Replace('ü', 'ü')
$body = $innertext.Replace('ß', 'ß')
#>
if ($debug -eq 1)
{
Write-Output "Subject $subject"
Write-Output "Tag $tag"
Write-Output "Link $link"
Write-Output $body
#exit
}
if($link -match "Zigaretten") #searchs in the <link> for the string "Zigaretten"
{
if($subject -match $tag) #searches for the specified tag in config.txt !!! only one argument per line !!!
{
if($mail = Send-MailMessage -From "$sender" -To "$receiver" -Subject "Zoll Meldung: $subject" -Body "$body" -SmtpServer "$smtp" -BodyAsHtml -encoding $encoding)
{
if($debug -eq 1)
{
Write-Output "$tag"
Write-Output "Send. Tag = $tag"
}
Write-Output "Send."
}
}
}
else
{
Write-Host "Empty."
}
}
$ie.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)
Remove-Variable ie
Added a wait if busy loop to make sure IE loads the full html document. Thats the solution of the problem ! :)
<#
AUTHOR: KOCH,MICHAEL [GRE-IT]
DESCRIPTION: RSS READER
DATE: 28.06.17
DATE LAST WRITTEN: 20.07.17
LAST CHANGE: ADDED WAIT IF BUSY !
#>
$debug = 0 #DEBUG
$receiver="A#MailAdress.de"
$sender="A#MailAdress.de"
$smtp="A.SMTP.SERVER"
$encoding = [System.Text.Encoding]::UTF8
$path_config = "C:\RSS\Zoll\config.txt"
$output = "C:\RSS\Zoll\meldung.html"
$output_edit_path = "C:\RSS\Zoll\meldung_edit.html"
$nmbr=0
$count=0
Invoke-WebRequest -Uri 'http://www.zoll.de/SiteGlobals/Functions/RSSFeed/DE/RSSNewsfeed/RSSZollImFokus.xml' -OutFile C:\RSS\Zoll\meldungen.xml -ErrorAction Stop
[xml]$content = Get-Content C:\RSS\Zoll\meldungen.xml
$feed = $content.rss.channel
$tag = #()
if($lines=Get-Content $path_config | Measure-Object -Line) #gets the number of lines
{
while($count -ne $lines.Lines)
{
if($entrys=(Get-Content $path_config)[$nmbr]) #gets the entrys from config.txt and goes through line by line
{
$entrys >> $tag[$nmbr]
if ($debug -eq 1)
{
Write-Output "$tag[$nmbr]"
Write-Output "$entrys"
Write-Output "$count"
}
}
$count++
$nmbr++ #jumps into the next line
}
}
$ie = New-Object -ComObject InternetExplorer.Application #creates new ComObject IE
Foreach($msg in $feed.Item)
{
$link = ($msg.link)
$subject = ($msg.title)
if ($debug -eq 1)
{
$ie.visible = $true
}
$ie.navigate("$link") #navigate with Internetexplorer to the website
while ($ie.busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 200 } # if getting the website from IE.navigate is still .busy wait 200 milliseconds
$return = $ie.document
$innertext = $return.documentelement.document.IHTMLDocument3_getElementById("main").outerText #gets the outer text from the div with the element ID "main"
while ($innertext.busy -and $innertext.ReadyState -ne 4){ sleep -Milliseconds 200 } # if getting Text is .busy wait 200 milliseconds
$body = $innertext
if ($debug -eq 1)
{
Write-Output "Subject $subject"
Write-Output "Tag $tag"
Write-Output "Link $link"
Write-Output "INNERTEXT $innertext"
Write-Output "BODY $body"
#exit
}
if($link -match "Zigaretten") #searchs in the <link> for the string "Zigaretten"
{
if($subject -match $tag) #searches for the specified tag in config.txt !!! only one argument per line !!!
{
if($mail = Send-MailMessage -From "$sender" -To "$receiver" -Subject "Zoll Meldung: $subject" -Body "$body" -SmtpServer "$smtp" -BodyAsHtml -encoding $encoding)
{
Write-Output "Send."
}
}
}
else
{
Write-Host "Empty."
}
}
$ie.Quit() #----|
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie) # ---> Quits the Internet Explorer Session otherwise there are to many IE.exe open and no more ID's left
Remove-Variable ie
I have a script which reads a file of URL's and then performs a webrequest to check the status and latency of a list of site portals. We present this in a html table on an internal webserver. Our urls are not very meaningful to our business users I would like to create a new column in the table which shows the name of the site (http://10.x.x.x:8080/portal would be named 'Manchester')
Preferably reading in another file of names as each line of both files will match up
I have been doing this as a vanity project when I have a bit of free time in work but have limited knowledge.
'## The URI list to test
$URLListFile = "H:\Scripts\web.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
$a = Get-date
Foreach($Uri in $URLList) {
$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 #>
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
$SResult += [PSCustomObject] #{
Store = $Store;
}
}
#Prepare email body in HTML format
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Stores Web Portal Status</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Stores Web Portal Status $($a)</H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR bgcolor=green>"
}
if($Entry.timetaken -ge "2600.000")
{
$Outputreport += "<TR bgcolor=yellow>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file H:\Scripts\Test.htm'
I recommend changing the source file to a CSV format that stores both the URI and the Name side-by-side. In concept, it would look something like this:
$csvText = #"
Uri,SiteName
http://10.0.0.1/foo,Foo
http://10.0.0.2/bar,Bar
"#
$siteRecords = ConvertFrom-Csv $csvText
$siteRecords
In practice, you would use Import-Csv to get the data from your file, and then you would have access to both properties when iterating.
$siteRecords = Import-Csv -Path $pathToYourCsvFile
foreach ($record in $siteRecords)
{
$record.Uri
$record.SiteName
}
ORIGINAL POST:
Goal: an email is sent to a service account every day from a source.
Take the csv file it gives me and put it in a folder for the DBA guy.
Without further adieu, here is my current script:
#define variables
$datestamp = (get-date).AddDays(-1).ToString("MMddyyyy")
$datestamp = $datestamp.ToString()
$path = "C:\MyPath"
$dest = "C:\MyPath\Archive"
$file = "MyFile.csv"
#create outlook session
$objOutlook = New-Object -Com Outlook.Application
$inbox = $objOutlook.Session.GetDefaultFolder(6)
$inbox.Items.Restrict("[UnRead] = True" -and "[SenderEmailAddress] = 'SomePlace#SomeDomain.net'") | select -Expand Attachments | %
{
for ($i = $_.Count; $i; $i--)
{
$_.Item($i).SaveAsFile("C:\MyPath\$($_.Item($i).FileName)")
$_.Parent.Unread = $false
}
}
if (Test-Path "C:\MyPath\*.csv")
{
if(((Get-ChildItem C:\MyPath | Measure-Object ).Count) -gt '1' )
{
Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy#domain.com" -To "PoorITGuy#domain.com" -Subject " FAIL" -Body "FAILED. Too many valid items from mailbox.
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
throw "Too many items to get."
}
else
{
Get-ChildItem $path\*.csv | foreach {Copy-Item $_ "C:\MyPath\$"}
Copy-Item C:\MyPath\*.csv "$path\$file"
Copy-Item C:\MyPath\*.csv "$dest\${datestamp}_$file"
if(Test-Path "$dest\$file")
{
Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy#domain.com" -To "PoorITGuy#domain.com" -Subject "some message”
#cleanup - remove all files from base directory, clean mailbox, close out com object.
Remove-Item "$path\*.csv"
$inbox.Items | select | %
{
for ($i = $_.Count; $i; $i--)
{
$_.Item($i).Delete
}
}
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
}
else
{
Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy#domain.com" -To "PoorITGuy#domain.com" -Subject " failure" -Body "File manipulation failure."
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
throw "File manipulation failure."
}
}
}
else
{
Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy#domain.com" -To PoorITGuy#domain.com -Subject "FAIL" -Body "No item mailbox."
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
throw "No item to get in inbox."
}
What doesn't work:
$inbox.Items.Restrict("[UnRead] = True" -and "[SenderEmailAddress] = 'SomePlace#SomeDomain.net'") | select -Expand Attachments | %
It seems that you cannot restrict the comObject Outlook.Application with more than one filter.
I've done a ton of searching on this and cannot find an answer on how to best perform this task in lieu of this. But I want my script to be spam-proof, so it needs to know that it is sent from the expected sender AND it needs to be unread only (see error catching right below)
In addition, I'm not certain if this would work:
$inbox.Items | select | %
{
for ($i = $_.Count; $i; $i--)
{
$_.Item($i).Delete
}
}
Also would like input on the script itself. Any input to make it more efficient would be appreciated.
The correct code (lots of syntax errors on the above prevented it from doing what I wanted to)
#define variables
$datestamp = (get-date).AddDays(-1).ToString("MMddyyyy")
$datestamp = $datestamp.ToString()
$path = "C:\MyPath"
$dest = "C:\MyPath\Archive"
$importpath = "C:\MyPath\Import"
$file = "MyFile.csv"
$folderExclude = "C:\MyPath\Archive"
#create outlook session
$objOutlook = New-Object -Com Outlook.Application
$inbox = $objOutlook.Session.GetDefaultFolder(6)
$inbox.Items.Restrict("[Unread] = True AND [SenderEmailAddress] = 'SomePlace#SomeDomain.net'") | select -Expand Attachments | % {
for ($i = $_.Count; $i; $i--) {
$_.Item($i).SaveAsFile("\\server\MyPath\$($_.Item($i).FileName)")
$_.Parent.Unread = $false
}
}
if (Test-Path "C:\MyPath\*.csv") {
if(((Get-ChildItem C:\MyPath -Include "*.csv" | Measure-Object ).Count) -gt '1' ) {
Send-MailMessage -SmtpServer smtpserver.mydomain.com -From Powershell#mydomain.com -To someguy#mydomain.com -Subject "FAIL" -Body "FAILED. Too many items from PSAutomationSrv mailbox."
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
throw "Too many items to get."
}
else {
Copy-Item C:\MyPath\*.csv "$importpath\$file"
Copy-Item C:\MyPath\*.csv "$dest\${datestamp}_$file"
if(Test-Path "$dest\${datestamp}_$file") {
Send-MailMessage -SmtpServer smtpserver.mydomain.com -From Powershell#mydomain.com -To someguy#mydomain.com -Subject " successful" -Body "Date is: ${datestamp} File name is: ${dest}\${file}"
#cleanup - remove all files from base directory, clean mailbox, close out com object.
Remove-Item $path\*.csv
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
}
else {
Send-MailMessage -SmtpServer smtpserver.mydomain.com -From Powershell#mydomain.com -To someguy#mydomain.com -Subject " import - failure" -Body "File manipulation failure."
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
throw "File manipulation failure."
}
}
}
else {
Send-MailMessage -To -SmtpServer smtpserver.mydomain.com -From Powershell#mydomain.com -To someguy#mydomain.com -Subject "FAIL" -Body "FAILED. No item in mailbox."
$objOutlook.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
throw "No item to get in inbox."
}
Notes - I had some code in excess.
Get-ChildItem $path\*.csv | foreach {Copy-Item $_ "C:\MyPath\$"}
is incorrect syntax, and unnecessary since the script already knows it is just manipulating one file anyway.
For deleting the inbox I would need to use Export-Mailbox it seems, which I do not want to do; as the service account performing this function is NOT an exchange admin nor do I want it to be. I'm just going to manually delete the inbox periodically or have the exchange guy do it on his end.