none
Removing blank spaces and blank lines from output RRS feed

  • Question

  • Hi All,

    I need a bit of a help with the script I have written . This script works on a SharePoint site, it pulls the list of the users from the group having string "owner" in their name and send them emails . The script is working fine but whenever email ids of the users are pulled from the group a blank line is appended to the output which results in an error . Below is the script  :

    ===============================================================

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    $sitecoll = gc .\Site_list.txt
    ForEach($url in $sitecoll)
    {
    $site = [Microsoft.SharePoint.SPSite] ($url)
    $Web = $site.OpenWeb()
    foreach($group in $Web.Groups)
    {
    if($group.Name -like "*Owner*")
    {
    foreach($user in $group.Users)

    $emailTo=($user.email).trimend()
    $emailTo
    $template = "xxx " 
    $signature = "xxx"
    $carboncopy="<xyz@abc.com>"
    $emailFrom = "<ayz@abc.com>"
    $subject = "xxx :" + $site.url
    $body = $template + $signature 
    $smtpServer = "smtp.abc.com"
    ##send-MailMessage -SmtpServer $smtpserver -To $emailTo -BCC $carboncopy -From $emailFrom -Subject $subject -Body $body -BodyAsHtml
    }
    }
    }
    }

    =============================================================

    Below is the output that is passed to the send-MailMessage cmdlet :

    aaa@abc.com

    Blank Line

    bbb@abc.com

    Blank Line

    ccc@abc.com

    =================================================================

    Can any one please help to remove these blank lines from the output .


    Regards

    Karan Sharma 


    • Edited by karansharma Monday, March 3, 2014 12:32 PM typo
    Monday, March 3, 2014 12:31 PM

Answers

  • Hi Tommy ,

    I tried it , its not working ,

    $emailTo=($user.email).trimend()


    Regards

    Karan Sharma 

    You are not looking at what I post.

    The following has no issues with blank lines.  I have run it on my own servers and it does exactly what you asked it to do.  I added some messages so you can see what is happening.

    $sitecoll=Get-Content .\Site_list.txt
    ForEach($url in $sitecoll){
        $site=[Microsoft.SharePoint.SPSite]$url
        $Web=$site.OpenWeb()
        foreach($group in $Web.Groups){
            if($group.Name -match 'owner'){
                Write-Host $group.Name -Fore green
                foreach($user in $group.Users){
                    if($user.email){
                        Write-Host $user.email -Fore green
                        $mailprops=@{
                            To=$user.email
                            BCC='<xyz@abc.com>'
                            From ='<ksharma5@its.jnj.com>'
                            Subject = "xxx :" + $site.url
                            Body=$template+$signature 
                            BodyAsHtml=$true
                            SmtpServer='smtp.abc.com'
                        }
                        send-MailMessage @mailprops
                    }else{
                        Write-Host 'No email for:' + $user.name -Fore red
    		}
                }
            }
        }
    }


    ¯\_(ツ)_/¯




    • Edited by jrv Monday, March 3, 2014 4:49 PM
    • Marked as answer by karansharma Monday, March 3, 2014 5:05 PM
    Monday, March 3, 2014 4:46 PM

All replies

  • Guys ,

    I found a work-around to mitigate the errors i was getting when the script was executed . Basically a $emailTo variable was string and it has a property length . This property will be zero for blank spaces , so all that was required was if condition . Below is the updated script :

    ===========================================================================

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    $sitecoll = gc .\Site_list.txt
    ForEach($url in $sitecoll)
    {
    $site = [Microsoft.SharePoint.SPSite] ($url)
    $Web = $site.OpenWeb()
    $url
    foreach($group in $Web.Groups)
    {
    if($group.Name -like "*Owner*")
    {
    foreach($user in $group.Users)

    $emailTo=($user.email).trimend()
    if ($emailTo.Length -gt 0){
    $template = "xxx " 
    $signature = "xxx"
    $carboncopy="<xyz@abc.com>"
    $emailFrom = "<ksharma5@its.jnj.com>"
    $subject = "xxx :" + $site.url
    $body = $template + $signature 
    $smtpServer = "smtp.abc.com"
    send-MailMessage -SmtpServer $smtpserver -To $emailTo -BCC $carboncopy -From $emailFrom -Subject $subject -Body $body -BodyAsHtml
    }
    }
    }
    }
    }

    =================================================

     

    Regards Karan Sharma Whenever you see a reply and if you think is helpful, click "Vote As Helpful"! And whenever you see a reply being an answer to the question of the thread, click "Mark As Answer

    Monday, March 3, 2014 1:39 PM
  • Try making your scripts readable:

    Add-Type -AssemblyName Microsoft.SharePoint
    
    $sitecoll = gc .\Site_list.txt
    ForEach($url in $sitecoll){
        $site=[Microsoft.SharePoint.SPSite]$url
        $Web=$site.OpenWeb()
        foreach($group in $Web.Groups){
            if($group.Name -like '*Owner*'){
                foreach($user in $group.Users){ 
                $emailTo=$user.email
                if($emailTo.Length -gt 0){
                    $template = "xxx " 
                    $signature = "xxx"
                    $mailprops=@{
                        To=$emailTo
                        BCC='<xyz@abc.com>'
                        From ='<ksharma5@its.jnj.com>'
                        Subject = "xxx :" + $site.url
                        Body=$template+$signature 
                        BodyAsHtml=$true
                        SmtpServer='smtp.abc.com'
    		}
                    send-MailMessage @mailprops
                    }
                }
            }
        }
    }
    


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, March 3, 2014 1:54 PM
    Monday, March 3, 2014 1:53 PM
  • Thanks Jrv!!!

    Your point is well taken..............I will try to improve the readability of the script in my further posts :)

    I am still wondering, how to remove the blank line from the output ?

    Can you please help me with that ??

     

    Regards
    Karan Sharma 

    Monday, March 3, 2014 3:32 PM
  • Can you let us know which variable holds the extra blank space/line and give us an example of the contents of that variable? Have you tried using the .Trim() method?

    PS C:\> $a = '                   spaces before and after this text                '
    PS C:\> $a
                       spaces before and after this text
    PS C:\> $a = $a.Trim()
    PS C:\> $a
    spaces before and after this text


    Monday, March 3, 2014 3:55 PM
  • Can you let us know which variable holds the extra blank space/line and give us an example of the contents of that variable? Have you tried using the .Trim() method?

    PS C:\> $a = '                   spaces before and after this text                '
    PS C:\> $a
                       spaces before and after this text
    PS C:\> $a = $a.Trim()
    PS C:\> $a
    spaces before and after this text


    I have no idea what you are trying to say.

    The $a variable is likely and array of lines.  Each will be a se4parate line.

    PS C:\scripts> $a='hello','','world','','end'
    PS C:\scripts> $a
    hello
    
    world
    
    end
    PS C:\scripts> $a|?{$_}
    hello
    world
    end

    TO string-ify for Send-MailMessage

    $a=$a | Out-String


    ¯\_(ツ)_/¯

    Monday, March 3, 2014 4:19 PM
  • This is the easiest way to test fro a zero length string:

    if($user.email)){
       .... other code
    }else{
        'user email blank'
    }

    Zero length strings and umeric zero always test to $false.  This is by design and it is why ?{$_} works to filter out blank lines.


    ¯\_(ツ)_/¯

    Monday, March 3, 2014 4:27 PM
  • Now as to the statement about output, what is it that you are calling output?  I see no output in the script.  The script sends a mail message.

    If you use my version you will not see an issue with blank lines.  The blank lines are not being sent to sendmail.  They are being output by your code to the console.

    Filter out the empty email addresses and you will not see empty email addresses printed to the console.


    ¯\_(ツ)_/¯

    Monday, March 3, 2014 4:33 PM
  • Hi Tommy ,

    I tried it , its not working ,

    $emailTo=($user.email).trimend()


    Regards

    Karan Sharma 

    Monday, March 3, 2014 4:33 PM
  • Hi Tommy ,

    I tried it , its not working ,

    $emailTo=($user.email).trimend()


    Regards

    Karan Sharma 

    You are not looking at what I post.

    The following has no issues with blank lines.  I have run it on my own servers and it does exactly what you asked it to do.  I added some messages so you can see what is happening.

    $sitecoll=Get-Content .\Site_list.txt
    ForEach($url in $sitecoll){
        $site=[Microsoft.SharePoint.SPSite]$url
        $Web=$site.OpenWeb()
        foreach($group in $Web.Groups){
            if($group.Name -match 'owner'){
                Write-Host $group.Name -Fore green
                foreach($user in $group.Users){
                    if($user.email){
                        Write-Host $user.email -Fore green
                        $mailprops=@{
                            To=$user.email
                            BCC='<xyz@abc.com>'
                            From ='<ksharma5@its.jnj.com>'
                            Subject = "xxx :" + $site.url
                            Body=$template+$signature 
                            BodyAsHtml=$true
                            SmtpServer='smtp.abc.com'
                        }
                        send-MailMessage @mailprops
                    }else{
                        Write-Host 'No email for:' + $user.name -Fore red
    		}
                }
            }
        }
    }


    ¯\_(ツ)_/¯




    • Edited by jrv Monday, March 3, 2014 4:49 PM
    • Marked as answer by karansharma Monday, March 3, 2014 5:05 PM
    Monday, March 3, 2014 4:46 PM
  • Can you give us an example of what is stored in $User.Email? Also, pipe it to Get-Member and let us know the TypeName for the $User variable and the $User.Email variable property.

    $User | Get-Member | Select-Object TypeName -Unique
    $User.Email | Get-Member | Select-Object TypeName -Unique

    Monday, March 3, 2014 4:49 PM
  • Can you give us an example of what is stored in $User.Email? Also, pipe it to Get-Member and let us know the TypeName for the $User variable and the $User.Email variable property.

    $User | Get-Member | Select-Object TypeName -Unique
    $User.Email | Get-Member | Select-Object TypeName -Unique

    $user.email is the users email address as defined in their SharePoint account or it is $null.  It is never a trim-able string.  It will always test to $true or $false.

    This is a SharePoint field and is not set on most internal accounts listed in any "Owners" group.  Trying to deal with this without a good knowledge otf SharePoint and PowerSHell will just have you chasing your tail.

    There are no blank lines in the variable,  The OP is misreading the output due to a lack of knowledge of PowerShell and SharePoint.

    The type of the email property is "string" it is an email address. It, like all SharePoint columns is a string.  That is SharePoint so their is no need to ask.  The point I am trying t get the OP to see is that he/she is asking the wrong question and seeing a non-symptom.  Send -MailMessage sees only what is passed.  Filter out blank email addresses and it works fine. 

    Most of the records in a default installation of SharePoint have blank email addresses.


    ¯\_(ツ)_/¯

    Monday, March 3, 2014 5:01 PM
  • Thanks Jrv ,

    This works very fine ........ 

    I will Use the "if block" that you have used in your script become simply it is better than my" if block" . 


    Regards

    Karan Sharma 


    Monday, March 3, 2014 5:04 PM
  • Its a string  

    Regards

    Karan Sharma 

    Monday, March 3, 2014 5:08 PM
  • Thanks Jrv ,

    This works very fine ........ 

    I will Use the "if block" that you have used in your script become simply it is better than my" if block" . 


    Regards

    Karan Sharma 


    Not that it is so much "better" than yours but trimming is unnecessary because the blanks are not coming from the Send-Message but are output because you have type $emailTo as a single line. This outputs each address including all of the blanks. 

    Learning how to use PowerShell effectively prevents this kind of misery.  Understanding how SharePoint and other systems store objects is also important.


    ¯\_(ツ)_/¯

    Monday, March 3, 2014 5:10 PM