I need to write a powershell script that will go through an Outlook folder, pull out a uid from a URL in the email and send a GET with the uid in a new URL.
This is fine for emails that are just forwarded to the mailbox, but sometimes these are forwarded as attachments. I would like to open the .msg file to pull out the uid in the same way, but I can't find a way to do it without saving the file down first.
My initial idea was to save the file, open it, pull out the information, then delete the .msg. The problem is that Outlook keeps the instance of the .msg file open, so it cannot be deleted. I could do an $outlook.quit(), but then it will close the instance of Outlook that I'm using to go through the rest of the emails.
I'd prefer to not have to save every single .msg individually in a file and then interate through all of them if possible.
Is it possible to just read the body of the .msg file as if it's just another email message?
Here is my code:
# Mailbox name
$mailbox = "abuse#place.com"
# Name of folder containing reported phishing emails
$folderName = "SelfPhishTest"
# Create the Outlook object to access mailboxes
$Outlook = New-Object -ComObject Outlook.Application;
# Grab the folder containing the emails from the phishing exercise
$Folder = $Outlook.Session.Folders($mailbox).Folders.Item($folderName)
# Grab the emails in the folder
$Emails = $Folder.Items
# Path to save attachments to
$filepath = "C:\SavedPhishEmails\"
# Run through each email in the folder
foreach($email in $Emails){
# Output Sender Name for Testing
Write-Host $email.SenderName
# The number of email attachments
$intCount = $email.Attachments.Count
# If the email has attachments, let's open the .msg email
if($intCount -gt 0) {
# Let's go through those attachments
for($i=1; $i -le $intCount; $i++) {
# The attachment being looked at
$attachment = $email.Attachments.Item($i)
# If this is a .msg, let's open it
if($attachment.FileName -like "*.msg"){
$attachmentPath = $filepath+$attachment.FileName
$attachment.SaveAsFile($attachmentPath)
Get-ChildItem $attachmentPath |
ForEach-Object {
$msg = $Outlook.Session.OpenSharedItem($_.FullName)
$msg.Body
}
Remove-Item $attachmentPath
}
}
}
}
Figured it out.
Adding this will release the .msg file
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($msg) | Out-Null
Related
I have done a PowerShell script where I want to email all the client on the server about their housekeep. I have a document attached for them to read through.
Currently I have it so I have to write in the clients mnemonic and then their email. I want it to be automated -- like have the clients mnemonic and email address stored in a txt folder or excel spread sheet. Have the script read in the details from there?
This is currently what I have.
$FLMDIR = 'E:\FLM_Mailer
Copy-Item E:\FLM_Mailer\E_MSGFILE.TXT -Destination $FLMDIR\msgfile.txt
$MSG_FILE = 'E:\FLM_Mailer\msgfile.txt'
$ClientID = Read-Host -Prompt 'Enter ClientID'
$EmailAddr = Read-Host -Prompt 'Enter Client Email Address'
# Search and Replace clientID
(Get-Content $MSG_FILE) | Foreach-Object {$_ -replace "AAA","$ClientID"} | Out-File $MSG_FILE
E:\FLM_Mailer\FTRMail.exe $MSG_FILE -to $EmailAddr -f myemail#email.com.au -server 10.10.10.311 -s "Repository Housekeeping in folder/ifolder system" -attach "$FLMDIR\folder Housekeeping for Hosted Clients.docx" -log E:\FLM_Mailer\FLMmail_log.txt -debug
#Remove-Item $FLMDIR\msgfile.txt
i am trying to upload the files from my local to Sharepoint via Add-PnPFile.
But everytime i upload the file it overwrites the existing file.. and if the existing file is not modified also still it will upload it again.
I want to check wether the file is already there or not ? and if yes then i want to check the modified date whether it is being modified in last 1 day or not ?
Any Suggestions to check and upload only the modified files ?
Code till Now (i am missing the checking file thing i assume and dont know how to do it.)
#install and import module
Import-Module SharePointPnPPowerShellOnline
#Get Connection to the url
Connect-PnPOnline "Some SharePoint Url" -UseWebLogin
#get the files from a local folder
$Files = Get-ChildItem "C:\Some Local FolderPath" -Recurse
#Now check wether the file is being modified or not ?
foreach($File in $Files){
$LastWriteTimeForThisFile = $File.LastWriteTime.Day
$difference = (Get-Date).Day - $LastWriteTimeForThisFile
if ( $difference -lt 1)
{
$upload = Add-PnPFile -Path $File.FullName -Folder $SharePointFolderPath
#$message = "Successfully Uploaded"
Write-Host "======================Successfully Uploaded==============================="
}
else{
Write-Host "=========Not Modified so not uploading again============"
}
}
You can concatenate your file name with the path uploaded to SharePoint, and use Get-PnPFile to check if the file exists. If the file exists, check the modification time of the file.
try {
$file=Get-PnPFile -Url "/sites/dev/Shared Documents/Facebook.png"-ErrorAction Stop
$file
#do file exist action
}
catch {
Write-Host "$($_.Exception.Message)" -Foregroundcolor Red
#do file not exist action
}
Checking to see if this logic makes any sense or if I'm missing something.
Each IIS Site has their own Site ID. This Site has it's own IIS Log folder based on that Site ID. So if your Site ID is 2385, then your Log folder is W3SVC2385.
You can get the Site ID for each Site in command line by running '%windir%\system32\inetsrv\appcmd list site' which gives you SITE "Site Name" (id:####, bindings, State)
By pulling that data into a file, one should be able to isolate both the Site Name and the ID, then create a variable for each IIS Log folder. Using this information, you can pull in file information from the IIS Logs folders. By looking at the date the last created and/or modified log file was created/updated in each folder, shouldn't that tell us, what day that site was last accessed? Or am I missing something?
So here is the code I came up with. This only works if your site name is also the URL for the site (which is the case for us here). It inserts the Website Name into the first part of the URL stem.
Because this is for a SharePoint farm for us, I also filtered for default.aspx and home.aspx in the LogParser command.
Using Get-Website, I get both the site name & id. I add the name into the url stem and use the id to get into the appropriate log folder and then create a csv file based on the site name.
Hopefully this can be helpful for others!
$Sites = Get-WebSite
foreach ($site in $sites)
{
$id = $SITE.id
$name = $site.name
$name = $name -replace '\s',''
$filename = $name + '.csv'
$logfolder = 'D:\LOGS\IISLOGS\W3SVC' + $id
$logpath = $logfolder + "\*.*"
& "C:\Program Files (x86)\Log Parser 2.2\LogParser.exe" -i:iisw3c "SELECT DISTINCT TO_LOWERCASE(cs-uri-stem),max(date) into "$filename" from $logpath WHERE cs-uri-stem LIKE '%Default.asp%' OR cs-uri-stem LIKE '%home.asp%' group by cs-uri-stem" -o:CSV
$Content = #()
$NewContent = #()
$Content = Get-Content $filename
foreach ($line in $Content)
{
$NewContent += $name + $line
}
$NewContent[0] = "Site URL,DateLastAccessed"
$NewContent | Set-Content $filename
}
I notice the SharePoint tag is on this question. Since this is in the context of SharePoint, you should keep in mind that SharePoint Analytics:
https://technet.microsoft.com/en-us/library/jj219554.aspx
Is tracking the use of each of your SharePoint web applications. With that in mind you can programmatically access Analytics:
https://radutut.wordpress.com/2013/01/27/how-to-get-search-analytics-reports-programmatically-in-sharepoint-2013/
to get recent usage data.
I'm not Powershell guru but do anyone of you have some script which counting files in an folder and automaticlly send mail to user? Our users have an roaming profile
(\\profile-srv\%username%)
Folder name is the same as username. Is it possible to have an script which will count files in every home folder and send email to user?
domain is: FirmaBis.org total users: 150
So count in ex. aaba and send mail to aaba#firmabis.org
Count next aaca and send mail to aaca#firmabis.org
So script will count files and send mail to user based on folder name and + firmabis.org.
Thanks!
# Get just the directories in the user directory share, ignore any files, loop over them
Get-ChildItem -Path '\\server\share' -Directory | ForEach-Object {
# List all the files in the current folder (loop variable $_ is the folder)
$FilesInFolder = #($_ | Get-ChildItem -Recurse -Force -File)
# Count the files
$NumFiles = $FilesInFolder.Count
# Calculate how many MB they take up, and show it to 2 decimal places
$FileSize = $FilesInFolder.Length | Measure-Object -Sum | Select-ExpandProperty Sum
$FileSize = "{0:.0}MB" -f ($FileSize/1MB)
# Build the email message
$Message = #"
Hi,
The folder for account ($($_.Name)) has $($FilesInFolder.Count) files in it.
They add up to $FileSize
"#
# Send the message through an SMTP server which is configured to allow
# unauthenticated relay emails from the computer running the script.
Send-MailMessage -SmtpServer yourmailserver -To "$($_.Name)#FirmaBis.org" -From 'script#FirmaBis.org' -Body $Message
}
Untested, but ...
I have not seen anything that you have tried so far. Just to give you a set off:
You can get the list of Files count using the combination of Get-childitem and .Count method.
( Get-ChildItem D:\FolderName | measure-object).Count
You can store the output in the variable.
Then, You can pass the variable as a BODY in Send-MailMessage with which you can send emails.
I have a list of about 1000 PSTs that do not have an active directory owner assigned, nor is the folder, or file name identifiable to a user.
I would like to know what user has this file open so that I can identify the owner.
Given a list of file names.
C:\asdf.pst
C:\fdsa.pst
And a computer.
Murigar
I believe one could use OpenFiles.exe to produce a list of all open files and then extract this information.
When a user opens a PST file it creates a temporary file using a filename like ~asdf.pst.tmp. You can check the owner of that file to see who has the PST open.
Get-ChildItem "\\server\folder\*.pst.tmp" -Force | ForEach-Object {
$acl = Get-Acl -Path $_.FullName
$owner = $acl.Owner
$file = $_.Name.Replace("~","").Replace(".tmp","")
"$file is currently open by $owner"
}