none
Powershell ForEach and If statement Question RRS feed

  • Question

  • Good Afternoon!

           I am creating a powershell script that will customize a user's Outlook email signatures. There are many scripts out there but they do not seem to work very well for us. The script I have works great but i am trying to add an If statement in a nested ForEach loop but I haven't quite got the proper syntax down yet. The purpose of the ForEach loop is to read the signature templates (.htm, .rtf, and .txt) and then replace the designated words with the user's AD info, save the file, then move to the next file. Below are 3 outputs; the original template, the modified template after running the code, and the snippet of code in question. Sorry for the noob question!

    Original Template:

    User_Name | User_Title
    Acme Inc.
    O: UserOfficeNumber | M: UserCellNumber
    UserEmailAddress
    www.AcmeInc.com

    Modified Template:

    ForEach ($File in $SigFiles){
    (Get-Content $File) |
        ForEach-Object {
            If ($ADTitle -eq "Contractor") {$_ -replace "User_Title", ""} Else {$_ -replace "User_Title", $ADTitle}`
            $_ -replace "User_Name", $ADDisplayName `
            -replace "UserOfficeNumber", $ADTelePhoneNumber `
            -replace "UserCellNumber", $ADMobile `
            -replace "UserEmailAddress", $ADEmailAddress `
            -replace "UserAcctName", $UserName
            } | Set-Content $File
        }




    • Edited by Chester64.2 Tuesday, September 24, 2019 8:16 PM
    Tuesday, September 24, 2019 8:07 PM

All replies

  • You will need to use the Word object model to do this correctly. Text methods do not work correctly and will damage the files.

    The method is to create a word template and use bookmarks to update the signature file. Once that is done then just reassign thesignatures and the files will be created.

    In any case your question is what?  What is the issue? 

    As far as writing PS code goes this would be the correct approach:

    foreach($file in $sigFiles){
    	Get-Content $file|
    		ForEach-Object {
    			if($ADTitle -eq 'Contractor'){
    				$_ -replace 'User_Title'
    			}else{
    				$_ -replace 'User_Title', $ADTitle
    				$_ -replace 'User_Name', $ADDisplayName
    				$_ -replace 'UserOfficeNumber', $ADTelePhoneNumber
    				$_ -replace 'UserCellNumber', $ADMobile
    				$_ -replace 'UserEmailAddress', $ADEmailAddress
    				$_ -replace 'UserAcctName', $UserName
    			}
    		} |
    		Set-Content $file
    }



    \_(ツ)_/




    • Edited by jrv Tuesday, September 24, 2019 8:33 PM
    Tuesday, September 24, 2019 8:31 PM
  • In the 'Modified Template' picture above, it is duplicating lines and not replacing all of the designated words. Unfortunately in your code If the title is Contractor is should replace 'User_Title but then it doesn't do the other replacements in the bottom part of the code. The part of the code needs to always occur (except for the title replacement, that should occur in the If statement).
    Tuesday, September 24, 2019 8:58 PM
  • foreach($file in $sigFiles){
    	Get-Content $file|
    		ForEach-Object {
    			if($ADTitle -eq 'Contractor'){
    				$_ -replace 'User_Title'
    			}else{
    				$_ -replace 'User_Title', $ADTitle
            	        }
    			$_ -replace 'User_Name', $ADDisplayName
    			$_ -replace 'UserOfficeNumber', $ADTelePhoneNumber
    			$_ -replace 'UserCellNumber', $ADMobile
    			$_ -replace 'UserEmailAddress', $ADEmailAddress
    			$_ -replace 'UserAcctName', $UserName
    		} |
    		Set-Content $file
    }

    In the 'Modified Template' picture above, it is duplicating lines and not replacing all of the designated words. Unfortunately in your code If the title is Contractor is should replace 'User_Title but then it doesn't do the other replacements in the bottom part of the code. The part of the code needs to always occur (except for the title replacement, that should occur in the If statement).

    Well that is not the code you wrote.

    From what you are saying now this is how the code would work.


    \_(ツ)_/



    • Edited by jrv Tuesday, September 24, 2019 9:05 PM
    Tuesday, September 24, 2019 9:04 PM
  • Sorry I'm a bit confused. In your code from line 4 to 8 (in your 2nd post) is the same as my line 4. The only difference is the ` at the end. Below is the output from your code.

    Tuesday, September 24, 2019 9:26 PM
  • Ok now finish this to mask out unwanted lines.

    foreach($file in $sigFiles){
    	Get-Content $file|
    		ForEach-Object {
    			if($ADTitle -eq 'Contractor'){
    				$_ -replace 'User_Title'
    			}else{
    				$_ -replace 'User_Title', $ADTitle
            	}
    			if{$_ -eq 'User_Name'){$_ -replace 'User_Name', $ADDisplayName}
    			fix - >> $_ -replace 'UserOfficeNumber', $ADTelePhoneNumber
    			fix - >> $_ -replace 'UserCellNumber', $ADMobile
    			fix - >> $_ -replace 'UserEmailAddress', $ADEmailAddress
    			fix - >> $_ -replace 'UserAcctName', $UserName
    		} |
    		Set-Content $file
    }


    \_(ツ)_/


    • Edited by jrv Tuesday, September 24, 2019 10:40 PM
    Tuesday, September 24, 2019 9:29 PM
  • Yes i fixed that. I had it the way you posted. Below is the code i used ... exactly as you posted except that i made the If statement on one line.
    	(Get-Content $file)|
    		ForEach-Object {
    			if($ADTitle -eq 'Contractor'){$_ -replace 'User_Title'}else{$_ -replace 'User_Title', $ADTitle}
    			$_ -replace 'User_Name', $ADDisplayName
    			$_ -replace 'UserOfficeNumber', $ADTelePhoneNumber
    			$_ -replace 'UserCellNumber', $ADMobile
    			$_ -replace 'UserEmailAddress', $ADEmailAddress
    			$_ -replace 'UserAcctName', $UserName
    		} |	Set-Content $file

    Tuesday, September 24, 2019 9:40 PM
  • So what is the issue? That does what you seem to be asking.


    \_(ツ)_/

    Tuesday, September 24, 2019 9:43 PM
  • Yes i fixed that. I had it the way you posted. Below is the code i used ... exactly as you posted except that i made the If statement on one line.
    	(Get-Content $file)|
    		ForEach-Object {
    			if($ADTitle -eq 'Contractor'){$_ -replace 'User_Title'}else{$_ -replace 'User_Title', $ADTitle}
    			$_ -replace 'User_Name', $ADDisplayName
    			$_ -replace 'UserOfficeNumber', $ADTelePhoneNumber
    			$_ -replace 'UserCellNumber', $ADMobile
    			$_ -replace 'UserEmailAddress', $ADEmailAddress
    			$_ -replace 'UserAcctName', $UserName
    		} |	Set-Content $file

    You did not change the last code I posted to the correct code.  I just showed you what to do,

    \_(ツ)_/

    Tuesday, September 24, 2019 9:44 PM
  • Did you see the output from your given solution that I posted 17 minutes ago? Each line is duplicated 5 times.
    Tuesday, September 24, 2019 9:45 PM
  • Did you change the code as I requested?


    \_(ツ)_/

    Tuesday, September 24, 2019 10:31 PM
  • Yes i did and i posted the code that i used. Did you see anything that i missed in the code that i posted?
    Tuesday, September 24, 2019 10:36 PM
  • Yes i fixed that. I had it the way you posted. Below is the code i used ... exactly as you posted except that i made the If statement on one line.
    	(Get-Content $file)|
    		ForEach-Object {
    			if($ADTitle -eq 'Contractor'){$_ -replace 'User_Title'}else{$_ -replace 'User_Title', $ADTitle}
    			$_ -replace 'User_Name', $ADDisplayName
    			$_ -replace 'UserOfficeNumber', $ADTelePhoneNumber
    			$_ -replace 'UserCellNumber', $ADMobile
    			$_ -replace 'UserEmailAddress', $ADEmailAddress
    			$_ -replace 'UserAcctName', $UserName
    		} |	Set-Content $file

    That is not the code I posted and the fix is nothing like my example.  Please look closer and think about what is happening.

    if{$_ -eq 'User_Name'){$_ -replace 'User_Name',$ADDisplayName}


    \_(ツ)_/

    Tuesday, September 24, 2019 10:39 PM