Addition of String Replace Causes Export-Csv to send only Lengths of strings

This topic contains 2 replies, has 3 voices, and was last updated by  postanote 3 weeks ago.

  • Author
    Posts
  • #83035

    Scott McDonald
    Participant

    I'm getting information from a SharePoint list, using the following script.
    The fields Progress, Problems, & Plans are enhanced rich text. The HTML in some records is causing a shift on the export, so I'm trying to clear out the HTML from the strings. If I take out these lines, it works fine. When I add the 3 lines, my output list only "Length".

    So, my question is what am I doing wrong? My understanding is that I need to convert the strings to objects, but that's what I'm doing when I create the PSObject. I'm learning as I go here, so bare with me.

    $Progress -replace "]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>",""
    $Problems -replace "]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>",""	
    $Plans -replace "]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>",""
    
    function Get-3P() {
    $numDays = 5
    $toDate = Get-Date
    $toDate = $toDate.AddDays(-$numDays)
    #$toDate = Get-Date (2016-01-01)
    $site = Get-SPSite http://mysitecollection
    $serverContext=[Microsoft.Office.Server.ServerContext]::GetContext($site)
    $upm=New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serverContext)
    $spWeb = Get-SPWeb http://myweb
    $spList = $spWeb.Lists["Report 3Ps"]    #Get list instance
    $spListItems = $spList.Items
    foreach ($item in $spListItems){
    	if($item["Created"] -gt $toDate){
    		$dateWeekEnding = $item["Week of"]
    		$dateWeekEnding = $dateWeekEnding.Substring($dateWeekEnding.get_Length()-10)
    		$Lookup = new-object Microsoft.SharePoint.SPFieldLookupValue($item["Created By"])
    		$User = $Lookup.LookupValue;
    		$Progress = $item["Progress"]
    		$Progress -replace "]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>",""
    		$Problems = $item["Problems"]
    		$Problems -replace "]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>",""		
    		$Plans = $item["Plans"]
    		$Plans -replace "]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>",""
    
    				
    	        $data = @{
    			"Week Ending" = $dateWeekEnding
    			"Progress" = $Progress 
    			"Problems" = $Problems 
    			"Plans" = $Plans 
    			"Created By" = $User
    			"Created Date" = $item["Created"] 
    			"Modified Date" = $item["Modified"]			
    			}
    		 New-Object PSObject -Property $data | Select "Week Ending", "Progress", "Problems", "Plans", "Created By", "Created Date", "Modified Date"
    		}
            }
    }
    
    Get-3P  | Export-Csv -NoTypeInformation -Path C:\Temp\Export-2016-3Ps.csv
    
  • #83048

    Matt Bloomfield
    Participant

    I'm not sure why replace causes this behaviour but assigning the output of the -replace operator to the variable resolves the problem.

    Compare:

    function foo {
    
        $problem = 'hello'
        $problem = $problem -replace 'h','f'
    
        $data = @{
        "Problem" = $problem
        }
    
        New-Object PSObject -Property $data | Select "Problem"
    
    }
    
    foo | ConvertTo-Csv 

    with

    function foo {
    
        $problem = 'hello'
        $problem -replace 'h','f'
    
        $data = @{
        "Problem" = $problem
        }
    
        New-Object PSObject -Property $data | Select "Problem"
    
    }
    
    foo | ConvertTo-Csv
    

    By the way, as you're creating a hash table, you could make use of the type accelerator to build your object:

    function foo {
    
        $problem = 'hello'
        $problem = $problem -replace 'h','f'
    
        $data = [PSCustomObject] @{
        "Problem" = $problem
        }
    
        Write-Output $data
    
    }
    
    foo | ConvertTo-Csv
    
  • #83126

    postanote
    Participant

    Or just move your replace to hash table..

    function foo
    {
    $problem = 'hello'

    $data = @{
    "Problem" = ($problem) -replace 'h','f'
    }

    New-Object PSObject -Property $data | Select "Problem"
    }
    foo | ConvertTo-Csv -NoTypeInformation

    Results...

    "Problem"
    "fello"

    ...But up to you of course as this is all about no only creation but long term maintenance and updates by not only yourself but to those who will follow you when you move up the ladder to move on.

You must be logged in to reply to this topic.