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

Consolidating multiple scripts into one

$
0
0

Hello Everyone, I need help consolidating these 3 scripts into a single script with a single global function section, and a separate function for each file download, the order of which is not a requirement, they just need to run consecutive until all awards are downloaded. Thanks for your help

 

Script 1:

# The script requires the EWS managed API, which can be downloaded here:
# http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1
# This also requires PowerShell 2.0
# Make sure the Import-Module command below matches the DLL location of the API.
# This path must match the install location of the EWS managed API. Change it if needed.
#
#Load the EWS DLL - PATH MUST BE EXACT!
Add-Type -Path "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
#Set Exchange Version
$ExchangeVersion=[Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1
#Create Exchange Service Object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
#Set credentails - ENTER CREDENTAILS
$creds = New-Object System.Net.NetworkCredential("ops@home.com","OPS")  
$service.Credentials = $creds    
#Set the URL for the service - ENTER
$serviceURL = New-object Uri("https://mail.hint.com/ews/exchange.asmx")
$service.Url = $serviceURL
#Create and bind to inbox folder object - ENTER NAME of Mailbox
$folderid= New-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,"ops@hint.com") 
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#######
#Find and create variable to move mail items into the ToBeDeleted folder
$fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(100) 
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;
$SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"ToBeDeleted")
$findFolderResults = $InboxFolder.FindFolders($SfSearchFilter,$fvFolderView)
#######
#Create variables to identify the download attachment and a place to put it
#Location of attachment download
$downloadDirectory = "D:\RET\OptimUS"
#Does the email have this in the subject?
$subject = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, "Deans Awards")
#Does the email have an attachment
$attachment = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true)
$sfCollection=newobjectMicrosoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);
$sfCollection.add($subject)
$sfCollection.add($attachment)
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000)
$frFolderResult = $InboxFolder.FindItems($sfCollection,$view)
foreach ($miMailItems in $frFolderResult.Items){
    $miMailItems.Subject
    $miMailItems.Load()
    foreach($attach in $miMailItems.Attachments){
    $attach.Load()
        if($attach.Name.Contains(".xls")){
        $fiFile = new-object System.IO.FileStream(($downloadDirectory + "\" + $attach.Name.ToString()), [System.IO.FileMode]::Create)
        $fiFile.Write($attach.Content, 0, $attach.Content.Length)
        $fiFile.Close()
        }
    }
    #$miMailItems.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
    #Rename the file
    #get-childItem -Path C:\Temp\*.csv | Rename-Item -NewName TOP.csv
    #$miMailItems.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::MoveToDeletedItems)
    #Move the email into the ToBeDeleted items folder
    $miMailItems.Move($findFolderResults.Folders[0].Id)
}

 

Script 2

# The script requires the EWS managed API, which can be downloaded here:
# http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1
# This also requires PowerShell 2.0
# Make sure the Import-Module command below matches the DLL location of the API.
# This path must match the install location of the EWS managed API. Change it if needed.
#
#Load the EWS DLL - PATH MUST BE EXACT!
Add-Type -Path "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
#Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1
#Create Exchange Service Object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
#Set credentails - ENTER CREDENTAILS
$creds = New-Object System.Net.NetworkCredential("ops@home.com","OPS")  
$service.Credentials = $creds    
#Set the URL for the service - ENTER
$serviceURL = New-object Uri("https://mail.hint.com/ews/exchange.asmx")
$service.Url = $serviceURL
#Create and bind to inbox folder object - ENTER NAME of Mailbox
$folderid= New-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,"ops@hint.com") 
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#######
#Find and create variable to move mail items into the ToBeDeleted folder
$fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(100) 
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;
$SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"ToBeDeleted")
$findFolderResults = $InboxFolder.FindFolders($SfSearchFilter,$fvFolderView)
#######
#Create variables to identify the download attachment and a place to put it
#Location of attachment download
$downloadDirectory = "D:\RET\Reports\GREAT"
#Does the email have this in the subject?
$subject = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, "GREAT Report")
#Does the email have an attachment
$attachment = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true)
$sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);
$sfCollection.add($subject)
$sfCollection.add($attachment)
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000)
$frFolderResult = $InboxFolder.FindItems($sfCollection,$view)
$filecount = 0
$today = Get-Date -format M-d-yy
foreach ($miMailItems in $frFolderResult.Items){
    $miMailItems.Subject
    $miMailItems.Load()
    foreach($attach in $miMailItems.Attachments){
    $attach.Load()   
        $filecount ++
        $fiFile = new-object System.IO.FileStream(($downloadDirectory + "\" + $today + "_" + $filecount + "_" + $attach.Name.ToString()), [System.IO.FileMode]::Create)
        $fiFile.Write($attach.Content, 0, $attach.Content.Length)
        $fiFile.Close()
    }
    #$miMailItems.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
    #Rename the file
    #get-childItem -Path C:\Temp\*.csv | Rename-Item -NewName TOP.csv
    #Move the email into the deleted items
    #$miMailItems.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::MoveToDeletedItems)
    #Move the email into the ToBeDeleted items folder
    $miMailItems.Move($findFolderResults.Folders[0].Id)
}

 

Script 3

# The script requires the EWS managed API, which can be downloaded here:
# http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1
# This also requires PowerShell 2.0
# Make sure the Import-Module command below matches the DLL location of the API.
# This path must match the install location of the EWS managed API. Change it if needed.
#
#Load the EWS DLL - PATH MUST BE EXACT!
Add-Type -Path "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
#Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1
#Create Exchange Service Object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
#Set credentails - ENTER CREDENTAILS
$creds = New-Object System.Net.NetworkCredential("ops@HOME.com","OPS")  
$service.Credentials = $creds    
#Set the URL for the service - ENTER
$serviceURL = New-object Uri("https://mail.hint.com/ews/exchange.asmx")
$service.Url = $serviceURL
#Create and bind to inbox folder object - ENTER NAME of Mailbox
$folderid= New-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,"ops@hint.com") 
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
#######
#Find and create variable to move mail items into the ToBeDeleted folder
$fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(100) 
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;
$SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,"ToBeDeleted")
$findFolderResults = $InboxFolder.FindFolders($SfSearchFilter,$fvFolderView)
#######
#Create variables to identify the download attachment and a place to put it
#Location of attachment download
$downloadDirectory = "D:\RET\OptimUS"
#Does the email have this in the subject?
$subject = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, "Twin Oaks Bid Curve and Awards")
#Does the email have an attachment
$attachment = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $true)
$sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);
$sfCollection.add($subject)
$sfCollection.add($attachment)
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000)
$frFolderResult = $InboxFolder.FindItems($sfCollection,$view)
foreach ($miMailItems in $frFolderResult.Items){
    $miMailItems.Subject
    $miMailItems.Load()
    foreach($attach in $miMailItems.Attachments){
    $attach.Load()
        if($attach.Name.Contains(".xls")){
        $fiFile = new-object System.IO.FileStream(($downloadDirectory + "\" + $attach.Name.ToString()), [System.IO.FileMode]::Create)
        $fiFile.Write($attach.Content, 0, $attach.Content.Length)
        $fiFile.Close()
        }
    }
    #$miMailItems.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
    #Rename the file
    #get-childItem -Path C:\Temp\*.csv | Rename-Item -NewName TOP.csv
    #$miMailItems.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::MoveToDeletedItems)
    #Move the email into the ToBeDeleted items folder
    $miMailItems.Move($findFolderResults.Folders[0].Id)
}

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles