Pad with leading Zeros in PowerShell

26 Views Asked by At

I've written a PowerShell script which imports a CSV file and converts each row in the CSV to an XML file. but I've just realised that one of the fields is an 8 character number field and needs padding with leading zeros when the number length is less 8 characters long.

$($FileName.DocNumber) comes out as "1234" but i need it to come out as "00001234" I have tried using pad left and the format fucntion but it just seems to come out as text on my xml output.

where am i going wrong?

below is my code,

#Script Version 1.01

#Variables
#Set Site variables from ServerScriptVariables ini file
###########################################################################################################################################
$content = Get-Content D:\openacc\etst6090\IMIR_Generator\Configuration\ServerScriptVariables.ini
$DataURL = $content[1].Substring($content[1].IndexOf("=") + 1)
$Logfolder = $content[2].Substring($content[2].IndexOf("=") + 1)
$FilePath = $content[3].Substring($content[3].IndexOf("=") + 1)
$XMLPostURL = $content[4].Substring($content[4].IndexOf("=") + 1)
$eBISFAURL = $content[5].Substring($content[5].IndexOf("=") + 1)
$eBISCURLURL = $content[6].Substring($content[6].IndexOf("=") + 1)
$eBISPostMethod = $content[7].Substring($content[7].IndexOf("=") + 1)
$XMLDirectory = $content[8].Substring($content[8].IndexOf("=") + 1)
$DebugMode = $content[9].Substring($content[9].IndexOf("=") + 1)
$CURLCleardown = $content[10].Substring($content[10].IndexOf("=") + 1)
$IMIRFormID = $content[11].Substring($content[11].IndexOf("=") + 1)
############################################################################################################################################


#old Method of setting variables 
############################################################################################################################################
#$Logfolder = 'D:\openacc\etst5080\IMIR_Config\Spool\'               #Location of Spool directory

#$FilePath = 'D:\openacc\etst5080\IMIR_Config\IMIR_ExporterTEST1.csv'                                       #file location for the CSV to import
#$XMLPostURL = 'http://mripf203mcy:8084/e.tst'                           #http address for CURL post
#$eBISFAURL = 'D:\openacc\etst5080\IMIR_Config\fileactioner\'       #URL for eBIS FileActioner
#$eBISCURLURL = 'D:\openacc\etst5080\IMIR_Config\curl\'
#$eBISPostMethod = 'curl'                                                #How is the data to post into eBIS set to either "CURL" or "Actioner"
#$XMLDirectory = ''                                                      #temp directory where XML files will post
#$DebugMode = 'ON'   
#$CURLCleardown = 'YES' #delete files from CURL directory after running? should be set to 'YES' only set to 'NO' when debugging or testing                                                          #Enable debugging to output txt file of results.


#Set Hard coded variables
############################################################################################################################################
$logDate = get-date -Format "MM-dd-yyyyHHmmss"
$LogName = $logFolder + "PowerShellLog_" + $LogDate +'.log'
############################################################################################################################################

Start-Transcript -path $LogName

#DebugVariables
############################################################################################################################################
Write-Host "Variables:"
Write-Host "Log File =" $LogFile
Write-Host "File Path ="  $FilePath
Write-Host "XMLPostURL ="  $XMLPostURL
Write-Host "eBISFAURL =" $eBISFAURL
Write-Host "eBISCURLURL =" $eBISCURLURL
Write-Host  "eBISPostMethod =" $eBISPostMethod
Write-Host  "XMLDirectory = " $XMLDirectory
Write-Host  "IMIRFormID = " $IMIRFormID
############################################################################################################################################

if ($eBISPostMethod -eq 'CURL') 
{
$XMLDirectory = $eBISCURLURL
}
else 
{
$XMLDirectory = $eBISFAURL
}

$docTemplate = @'
<objItem>
$($invoice -join "`n")
</objItem>
'@

# Per-invoice template.
$entryTemplate = @'
    <objID></objID>
    <objType>$IMIRFormID</objType>
    <alphaID></alphaID>
    <rtgStageName>ScriptImport</rtgStageName>
    <uuserID>System</uuserID>
    <fld01>oa$</fld01> 
    <fld02></fld02>
    <fld03></fld03>
    <fld04>$($FileName.SupplierCode)</fld04>  
    <fld05></fld05>
    <fld06>$($FileName.OrderValue)</fld06>
    <fld07></fld07>
    <fld08></fld08>
    <fld09></fld09>
    <fld10></fld10>
    <fld11></fld11>
    <fld12></fld12>
    <fld13>$($FileName.Company)</fld13>
    <fld14>$($FileName.Department)</fld14>
    <fld15></fld15>
    <fld16></fld16>
    <fld17></fld17>
    <fld18>$($FileName.SupplierName)</fld18>
    <fld19></fld19>
    <fld20></fld20>
    <fld21></fld21>
    <fld22></fld22>
    <fld23></fld23>
    <fld24></fld24>
    <fld25>$($FileName.DocumentValue)</fld25>
    <fld26></fld26>
    <fld27></fld27>
    <fld28></fld28>
    <fld29>$($FileName.VATValue)</fld29>
    <fld30></fld30>
    <fld31>$($FileName.Document)</fld31>
    <fld32>$($FileName.OAPOString)</fld32>
    <fld33></fld33>
    <fld34></fld34>
    <fld42>$($FileName.DocType)</fld42>
    <fld43>$($FileName.DocNumber)</fld43>
    <fld44>$($FileName.Reference)</fld44>
    <fld45>$($FileName.DocDate)</fld45>
    <fld46>$($FileName.Description)</fld46>
    <fld47>$($FileName.POObjUnique)</fld47>
    <fld48></fld48>
    <fld49>$($FileName.IMIReBISID)</fld49>
'@



#Step 1 Generate CSV file by calling routine

Write-Host "run extract to generate CSV @ " + $(get-date)

try
{
    $URLParams = $DataURL
    $Response = Invoke-WebRequest -Uri $UrLParams
    # This will only execute if the Invoke-WebRequest is successful.
    $StatusCode = $Response.Content
} catch {
    $StatusCode = $_.Exception.Response.content.value__
}
$StatusCode
if($StatusCode -like "*Thank you*")
{$ResponseText = "Query Ran"}
ELSE
{$ResponseText = "Query Failed"}

if($ResponseText -eq "Query Failed")
{Write-host "Query failed"
Exit
}
Else
{
Write-Host "Extract Complete Status Code =" $StatusCode - $Response.StatusDescription $(get-date)

#Step 2 Import CSV file
Write-Host "Import CSV file @" $(get-date)

Import-Csv -Path $FilePath -Delimiter '|' | where-Object{$_.IMIReBISID -eq "?"}| Group-Object FileName -ov grp | ForEach-Object {
  # $_.Group contains all invoice associated with the user at hand.
  # Create an XML element for each certificate and collect the results
  # in array.
  $invoice = foreach ($FileName in $_.Group) {
    # Instantiate the per-invoice template.
    $ExecutionContext.InvokeCommand.ExpandString($entryTemplate)  
  }
  # Instantiate the per-user template, which encompasses
  # the per-certificate elements, and output the result. 
  Write-host $XMLDirectory $FileName.FileName'.xml generated'
  $ExecutionContext.InvokeCommand.ExpandString($docTemplate)
} | # Write the resulting XML document string to a file named for the user ID-
    Set-Content -LiteralPath { $XMLDirectory + $FileName.FileName + '.xml' } 



#Step 4 Run CURL if required
if ($eBISPostMethod -eq 'CURL'){
$curlDir = $XMLDirectory + $FileName.FileName + '.xml' 
$curlDIR1 = '@'

$Filenames = Get-ChildItem -path $eBISCURLURL -filter *xml

Foreach ( $filenames in Get-ChildItem -path $eBISCURLURL -filter *xml)

{
$PostingURLString = $XMLPostURL + '/xmlpost.w?params=IMIR,TEST,xmlImport,text'

Write-host "CURL called" "-d" $curlDIR1$xmlDirectory$Filenames -H "Content-Type: text/xml" $PostingURLString
curl.exe "-d" $curlDIR1$xmlDirectory$Filenames -H "Content-Type: text/xml"  $PostingURLString
}

}
else {
    $statusMessage = 'Invoices will be processed by eBIS fileActioner'
}

if ($CURLCleardown -eq 'YES'){ 
 Get-ChildItem -path $eBISCURLURL -filter *xml | remove-item
 }
Write-Host 'Script completed @ ' $(get-date)
stop-transcript
}
0

There are 0 best solutions below