Using results from add-content to write data to a string

This topic contains 1 reply, has 2 voices, and was last updated by  Jonathan Warnken 1 year, 4 months ago.

  • Author
    Posts
  • #39898

    Gina Bautista
    Participant

    Legacy code. I'm trying to get this string to write the actual number of files that it wrote out. Right now it writes out {0} for each file. What am I missing? How do i get it to say 1 or how do I cleanly remove this?

    #Printing output for AGCH_FHMC
    $AGCH_FHMC_counter = 0
    foreach ($transfer in $transferResult_AGCH_FHMC.Transfers)
    {
    #Success or error?
    if ($transfer.Error -eq $Null)
    {
    #Print results
    Add-content -Path $logpath -value "Upload of {0} succeeded, moving to archive $($transfer.FileName)"
    $AGCH_FHMC_counter++
    #Upload succeeded, move source file to backup
    Move-Item $transfer.FileName $AGCH_FHMC_Archive
    }
    else
    {
    #Upload Failed, throw error
    Add-content -Path $logpath -value "Upload of {0} failed: {1} $($transfer.FileName) $($transfer.Error.Message)"
    }
    }
    Add-content -Path $logPath -value "$($AGCH_FHMC_counter) files successfully uploaded from AGCH_FHMC."

  • #39900

    Jonathan Warnken
    Participant

    You want to use conditional formatting. The "{0}" is a placeholder for your variable you just have to use format string "-f" to use your variable.
    The Scripting Guy has some great examples

    This is how I tested your concept

    $AGCH_FHMC_counter = 0
    do{
        Add-content -Path .\test.log  -value ("Upload of {0} succeeded" -f $AGCH_FHMC_counter)
        $AGCH_FHMC_counter++
    }
    until ($AGCH_FHMC_counter -gt 10)
    

You must be logged in to reply to this topic.