Escape characters in Powershell

Thomas_StickelThomas_Stickel Customer IT Monkey ✭
I am trying to import Catalog Models and it seems that it doesn't like certain characters. 
Powershell isn't my background ( prefer C# etc..)  but I'm needing to escape characters that are in a csv such as 

CSV file

Header below (Model) 
Intel(R) Xeon(R) CPU X5670 @ 2.93GHz 

Now this CSV file only has 1 column with the header of "Model"
I noticed that certain keywords are mandatory - I don't know which ones, but it seems that Ordinal is required so i set that to always be a "1" 

How can I escape any characters that are causing errors  ( or @ ?   

   Add-SCSMEnumeration -Parent $enum -Name ($enum.Name + "." + $t.Model.Replace(' ','')) -DisplayName $t.Model -Ordinal 1 -ManagementPack $mp

My Powershell which imports 4 records and choked on the one above  
"Intel(R) Xeon(R) CPU X5670 @ 2.93GHz "

Import-Module smlets
$mpName = "CompanyName.ListValues.HWA.CatalogModels"
$enum = Get-SCSMEnumeration -Name Cireson.AssetManagement.CatalogItemModelEnum$
$types = Import-Csv -Path "E:\AssetFiles\Models.csv"

$mp = New-SCSMManagementPack -ManagementPackName $mpName -FriendlyName $mpName -DisplayName $mpName -PassThru

foreach ($t in $types) {
    #$parent = Get-SCSMEnumeration | ?{$ -eq ($enum.Name + "." + $t.Parent.Replace(' ',''))}
    #if ($parent) {
      #Add-SCSMEnumeration -Parent $enum -Name ($enum.Name + "." + $t.Model.Replace(' ','')) -DisplayName $t.Model -Ordinal 1 -ManagementPack $mp
      Add-SCSMEnumeration -Parent $enum -Name ($enum.Name + "." + $t.Model.Replace(' ','')) -DisplayName $t.Model -Ordinal 1 -ManagementPack $mp


  • Brian_WiestBrian_Wiest Customer Super IT Monkey ✭✭✭✭✭
    If this is just the first data seeding import of the CSV easiest thing would be to perform a find and replace in excel normalizing the CSV before attempting to import.

    I don't believe you can perform the replace string inside the add verb.
    How I typically handle this is some line

    $types = Import-Csv -Path "E:\AssetFiles\Models.csv"
    foreach ($t in $types) {
    $model = New-Object string (,$t.Model.Replace(' ','')) (and what every else replacing)
    #now use the $model for the add-SCSMEnumeration

  • Thomas_StickelThomas_Stickel Customer IT Monkey ✭
    I was wondering how to use perhaps a regex ,  I was trying to just use multiple Replace, but it is ugly and not catching occur characters.  (the spreadsheet to import is too large to have to edit every other row) 
     Add-SCSMEnumeration -Parent $enum -Name ($enum.Name + "." + $t.Model.Replace(' ','').Replace('@','').Replace('(','').Replace(')','')) -DisplayName $t.Model -Ordinal 1 -ManagementPack $mp
Sign In or Register to comment.