0

My aim is to send an automated message which contains all the strings from my loop.

I got a $body variable which is something like

This message shows you how much $size of your $project is used

I want to merge these strings into one variable which I can send through mail, but either it sends in separated mails or only the last loop remains.

Code:

Enter-PSSession *computer*

$attributes = (Get-FsrmQuota | select Description, Size, PeakUsage)

foreach ($attribute in $attributes)
{
    $counter = $attributes.Count
    $descr = $attribute.Description
    $size = $attribute.size
    $tomb = @($descr.Split(";"))
    $mail = $tomb["1"]   # user mail adress
    $project = $tomb["0"] # user project
    $name = $mail.Split("@")[0] #username
    $firstName = $name.Split(".")[0]  
    $lastName = $name.Split(".")[1]
    $size = [Math]::Round(($attribute.Size)/1Gb,1).ToString() + " Gb" #storage size
    $usage = [Math]::Round(($attribute.Usage)/1Gb,1).ToString() + " Gb" #storage usage
    $percent = [Math]::Round((($attribute.Usage / $attribute.Size)*100),2).ToString() + "%"
    $projname = $array["2"] #project name

    
    for($i=1; $i -le $counter; $i++)
    {
        $ITBody = "The $projname project uses $size."
        $array+=$ITBody 
    }
}

write-host $array

Got the results duplicated soo many times

4
  • 1
    Share your code and we will be able to help you. Commented Feb 16, 2018 at 8:13
  • foreach $attribute in $attributes { $descr = $attribute.Description $array = @$descr.split(";") $proj = $array["1"] $size = $attribute.Size $body = "The $project project uses $size data" } I need the $body-s into one string Commented Feb 16, 2018 at 8:20
  • Putting code in comments isn't a good thing to do as it's hard to read. Instead, edit your question and add code. Commented Feb 16, 2018 at 9:29
  • What's the purpose of the for loop? Commented Feb 16, 2018 at 9:54

2 Answers 2

6
$array = @()

foreach ($attribute in $attributes){
    $projname = $attribute.ProjectName
    $size = $attribute.Size


    $array += "The $projname project uses $size GB"
}

$body = $array -join "`n"

The $array = @() part initializes the $array variable as an empty array.

In your foreach-loop, you can directly add the built string to that array.

With $array -join "`n" you concatenate all elements from the array with a newline between the elements.

Sign up to request clarification or add additional context in comments.

Comments

0

You can just combine them in to one variable by adding it to one another.

$combinedVariable = $body + $size + $project

Or you can combine them as a message

$combinedVariable = "This message shows you how much $size of your $project is used"

You can also add every variable to an array to pile up in the loop and later on use them separately

$array += $combinedVariable

If this is not what you ask for, please provide further details of your code and we can help you better.

6 Comments

I added $array+=$combinedBody into my code. It is closer to working, but somehow the loop doesn't stop when all of the datas are stored
You have to decide when do you want the loop to stop. if you want to stop when $combinedBody is $true, then put that as a condition , if ($combinedBody){break} for example
I tried something like $i=0 for($i=1 $i -le $counter $i++){ $combinedBody } break but it still shows way too many data
currently I have 6 bodies which needs to be merged to one email (kind of a report), but it's gonna be about 100-200 in the near future
"$i=0 for($i=1 $i -le $counter $i++){ $combinedBody } break" - This will loop through incrementing $i from 1 until it equals less than the number in $counter. Well it would but the format is wrong, it should be: "for($i=1; $i -le $counter; $i++).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.