Home > Net >  Output £ as PowerShell String
Output £ as PowerShell String

Time:03-24

I run the following to populate with data from a CSV file:

$csv = Import-Csv -Path C:\Users\RGETH\Downloads\suppliercontracttemplate.csv

foreach ($row in $csv) {
   Add-HbSpmSupplierContract -supplierId $row.supplierContractId -name $row.supplier -description $row.description -type $row.type -startDate $row.startDate -endDate $row.endDate -currency $row.currency -value $row.value -cancellationNoticePeriod $row.cancellationNoticePeriod -costCentre $row.costCentre -invoiceNumber $row.invoiceNumber -orderNumber $row.orderNumber -paymentType $row.paymentType -status $row.status -ownedBy $row.ownedBy -budgetOwner $row.budgetOwner -notes $row.notes -custom0 $row.custom0 -custom1 $row.custom1 -custom2 $row.custom2

I need to populate `-currency $row.currency with a £ symbol, I have tried escaping with a backtick, but the currency field fails to populate as a string.

Example CSV

CodePudding user response:

Making a wild guess and assuming you want to translate a three letter currency code to the according symbol you could use a hashtable ... like this:

$Currency = @{
    GBP = '£'
    EUR = '€'
    USD = '$'
    YEN = '¥'
}

$CSVInput = 'GBP'

$Currency[$CSVInput]

Adapted to your function call this should work then:

$Currency[$($row.currency)]

CodePudding user response:

Continuing from my comment. That function seems to create XML tags and the pound symbol should then be entered as entity like in decimal form £ or as hex £

You can hardcode that of course, but I would use

# put this at the top of your script somewhere
Add-Type -AssemblyName System.Web

# guessing that the 'currency' field in your CSV contains the string "£", convert it to entity like this
Add-HbSpmSupplierContract ... -currency ([System.Web.HttpUtility]::HtmlEncode($row.currency))

If you're using a PowerShell version => 3.0 (.Net 4.0) you can use the [System.Net.WebUtility]::HtmlEncode() method and you don't have to add an assembly first:

Add-HbSpmSupplierContract ... -currency ([System.Net.WebUtility]::HtmlEncode($row.currency))
  • Related