none
create a folders and subfolders RRS feed

  • Question

  • Hello,

    i have Script we create a folders

    # 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.
    [string]$info = "White"                # Color for informational messages
    [string]$warning = "Yellow"            # Color for warning messages
    [string]$error = "Red"                 # Color for error messages
    [string]$LogFile = "C:\Temp\log\Log2.txt"   # Path of the Log File

    function CreateFolder($MailboxName)
    {
        Write-host "Creating Folder for Mailbox Name:" $MailboxName -foregroundcolor  $info
        Write-host "name parent Folder for Mailbox Name:" $FolderNameparent -foregroundcolor  $info
        Write-host "name Folder for Mailbox Name:" $FolderName -foregroundcolor  $info
        Add-Content $LogFile ("Creating Folder for Mailbox Name:" + $MailboxName)
        #Change the user to Impersonate
        $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailboxName);
        #Create the folder object

        $oFolder = new-object Microsoft.Exchange.WebServices.Data.Folder($service)
        $oFolder.DisplayName = $FolderName
        #Call Save to actually create the folder
        $oFolder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::$foldernameparent)
        Write-host "Folder Created for " $MailboxName -foregroundcolor  $warning
        Add-Content $LogFile ("Folder Created for " + $MailboxName)
        $service.ImpersonatedUserId = $null
    }
    Import-Module -Name "D:\Exchange2013\Bin\Microsoft.Exchange.WebServices.dll"
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
    # Set the Credentials
    $psCred = Get-Credential
    $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
    $service.Credentials = $creds

    # Change the URL to point to your cas server
    $service.Url= new-object Uri("https://xxx.xxx.xx.xxx/EWS/Exchange.asmx")
    # Set $UseAutoDiscover to $true if you want to use AutoDiscover else it will use the URL set above
    $UseAutoDiscover = $false
    #Read data from the UserAccounts.txt
    import-csv -Delimiter ";" -path ".\UserAccounts.txt" | foreach-object {
        $WindowsEmailAddress = $_.WindowsEmailAddress.ToString()
        $FolderNameparent = $_.foldernameparent.ToString()
        $FolderName = $_.foldername.ToString()
        if ($UseAutoDiscover -eq $true) {
            Write-host "Autodiscovering.." -foregroundcolor $info
            $UseAutoDiscover = $false
            $service.AutodiscoverUrl($WindowsEmailAddress)
            Write-host "Autodiscovering Done!" -foregroundcolor $info
            Write-host "EWS URL set to :" $service.Url -foregroundcolor $info
        }
        #To catch the Exceptions generated
        trap [System.Exception]
        {
            Write-host ("Error: " + $_.Exception.Message) -foregroundcolor $error;
            Add-Content $LogFile ("Error: " + $_.Exception.Message);
            continue;
        }
        CreateFolder($WindowsEmailAddress)
    }

    But i want create a Subfolders

    the CSV is

    WindowsEmailAddress,foldernameparent,foldername
    xxxx@xxx0fr,msgfolderroot,Controls-2025,
    xxxx@xxx0fr,Controls-2025,2023-01,
    xxxx@xxx0fr,Controls-2025,2023-02,

    the script create controls-2025 but don't create 2023-01 or 2023-02

    Can You help Me ?

    Tuesday, January 16, 2018 4:19 PM

All replies

  • You have to first get the ParentFolder EWSId then you can create a SubFolder. This line

     $oFolder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::$foldernameparent)

    would only work for WellKnownFolders (Root,Inbox,SentItems) For user created Folders you need to use FindFolder to find the folder first

    I've published a module that uses EWS that demostrates this https://github.com/gscales/Powershell-Scripts/blob/master/CreateFolder.ps1 

    eg to use it in this instance

    Import-module ./CreateFolder.ps1

    $Creds = Get-Credential
    Create-Folder -MailboxName mailbox@y.com -NewFolderName "Controls-2025" -Credentials $Creds
    Create-Folder -MailboxName mailbox@y.com -ParentFolder \Controls-2025 -NewFolderName "2023-01" -Credentials $Creds
    Create-Folder -MailboxName mailbox@y.com -ParentFolder \Controls-2025 -NewFolderName "2023-02" -Credentials $Creds

    Cheers
    Glen

    Wednesday, January 17, 2018 5:51 AM
  • Thanks I'll test and keep you informed
    Wednesday, January 17, 2018 8:10 AM
  • we don't marche your solution but we have solutions

    # 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.
    [string]$info = "White"                # Color for informational messages
    [string]$warning = "Yellow"            # Color for warning messages
    [string]$error = "Red"                 # Color for error messages
    [string]$LogFile = "C:\Temp\\log\Log2.txt"   # Path of the Log File
    function CreateFolder($MailboxName)
    {
        Write-host "Creating Folder for Mailbox Name:" $MailboxName -foregroundcolor  $info
        Write-host "name Folder for Mailbox Name:" $FolderName -foregroundcolor  $info
        Write-host "list Folder :" $Folderlist -foregroundcolor  $info
        Add-Content $LogFile ("Creating Folder for Mailbox Name:" + $MailboxName)
        #Change the user to Impersonate
        $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailboxName);
        #Create the folder object

        $oFolder = new-object Microsoft.Exchange.WebServices.Data.Folder($service)
        $oFolder.DisplayName = $FolderName
        #Call Save to actually create the folder
        $oFolder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
        if (([string]::IsNullOrEmpty($folderlist)))
            {
             Write-host "pas de sous dossier pour " $MailboxName -foregroundcolor  $warning
           
            }
        else
            {
                $sublijst = $folderlist.Split(",")
                foreach ($subl in $sublijst)
                {
                $folder1 = New-Object Microsoft.Exchange.WebServices.Data.Folder($service)
                $folder1.DisplayName = $subl
                $folder1.Save($ofolder.ID)
                Write-Host "Done for $($subl)"
                }
            }
        Write-host "Folder Created for " $MailboxName -foregroundcolor  $warning
        Write-host "Folder name for " $ofolder -foregroundcolor  $warning
        Add-Content $LogFile ("Folder Created for " + $MailboxName)
        $service.ImpersonatedUserId = $null
    }
    Import-Module -Name "D:\Exchange2013\Bin\Microsoft.Exchange.WebServices.dll"
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
    # Set the Credentials
    $psCred = Get-Credential
    $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
    $service.Credentials = $creds

    # Change the URL to point to your cas server
    $service.Url= new-object Uri("https://xxx.xxx.xx.fr/EWS/Exchange.asmx")
    # Set $UseAutoDiscover to $true if you want to use AutoDiscover else it will use the URL set above
    $UseAutoDiscover = $false
    #Read data from the UserAccounts.txt
    import-csv -Delimiter ";" -path ".\UserAccounts.txt" |
    foreach-object {
        $WindowsEmailAddress = $_.WindowsEmailAddress.ToString()
        $FolderName = $_.foldername.ToString()
        $Folderlist = $_.folderlist#.ToString()
        if ($UseAutoDiscover -eq $true) {
            Write-host "Autodiscovering.." -foregroundcolor $info
            $UseAutoDiscover = $false
            $service.AutodiscoverUrl($WindowsEmailAddress)
            Write-host "Autodiscovering Done!" -foregroundcolor $info
            Write-host "EWS URL set to :" $service.Url -foregroundcolor $info

        }
        #To catch the Exceptions generated
        trap [System.Exception]
        {
            Write-host ("Error: " + $_.Exception.Message) -foregroundcolor $error;
            Add-Content $LogFile ("Error: " + $_.Exception.Message);
            continue;
        }
        CreateFolder($WindowsEmailAddress)
    }

    and TXT

    WindowsEmailAddress;foldername;folderlist
    xxxx@xxxx.fr;Controls-2024;"2018-01",2018-02,2018-03,2018-04,2018-05,2018-06,2018-07,2018-08,2018-09,2018-10,2018-11,2018-12

    thanks for your help

    Wednesday, January 17, 2018 4:14 PM