Substitute entires into a powershell script from text file

Posted on 2010-01-08
Last Modified: 2012-05-08
I've got this code to check the size of a remote folder on our Exchange mailbox server. I've also got a txt file with a list of client names. What I'd like to do is a foreach so that I can substitute the names of the clients into the code  (eventually will have a lot) and output the results. This will give a report of how much space each client is using on the Exchange server.

Obviously my powershell skillz are limited. Any help is appreciated.
$path = "\\mbx01\f$\mdbdata\Client1MBX01"

$colItems = (Get-ChildItem $path -recurse | Measure-Object -property length -sum)

$path +"       " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB" >>c:\log.txt

$path = "\\smbx02\f$\mdbdata\Client2MBX01"

$colItems = (Get-ChildItem $path -recurse | Measure-Object -property length -sum)

$path +"       " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB" >>c:\log.txt

Open in new window

Question by:mrpez1
    LVL 70

    Expert Comment

    by:Chris Dent

    Would you mind posting an example of the formatting you have in the client file? And an indication of the output you'd like to see?

    Naturally you should make up / obscure any sensitive information.

    LVL 5

    Author Comment

    The text file will have client names separated by carriage returns:



    The client names are uniform in the database paths on the Exchange server:



    The output I'd like to see is:

    \\mbx02\f$\mdbdata\CLIENT1MBX01       67,727.84 MB
    \\mbx02\f$\mdbdata\CLIENT2MBX01       5,238.17 MB

    Thanks for the help.

    LVL 5

    Accepted Solution

    Think I worked out. Here is the end result. Output is emailed to me via blat.
    $LogPath = "e:\scripts\month_end\log.txt"
    $Recipient = ""
    $CmdLine="$LogPath -to $Recipient"
    #Check size of mbx db folder
    Get-Content e:\scripts\clients.txt | Foreach-Object -process {
    $FolderPath = "\\mbx02\f$\mdbdata\" + $_ + "MBX01"
    $colItems = (Get-ChildItem $FolderPath -recurse | Measure-Object -property length -sum)
    $FolderPath +"       " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB" >>$LogPath
    #Check Size of Public folder
    #Send Email Report
    invoke-expression "$tool $cmdLine" | out-null

    Open in new window

    LVL 70

    Expert Comment

    by:Chris Dent

    Sorry, got a bit busy. All set now?


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
    Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now