Jump to content

Convert each csv item to a string and add them to a Hornbill simple list


Recommended Posts

The following code adds a stringValue to a list:

# Define output stream type
    [OutputType([object])]

    # Define runbook input params
    Param
    (
        # API Params
        [string] $application = "com.hornbill.servicemanager",
        [Parameter (Mandatory= $true)]
        [string] $listName,
        [Parameter (Mandatory= $true)]
        [string] $itemValue,
        [string] $defaultItemName,
        [string] $itemNameTranslation
        )

    # Add XMLMC params
    Add-HB-Param "application" $application $false
    Add-HB-Param "listName" $listName $false
    Add-HB-Param "itemValue" $itemValue $false
    Add-HB-Param "defaultItemName" $defaultItemName $false
    Add-HB-Param "itemNameTranslation" $itemNameTranslation $false

   # Invoke XMLMC call, output returned as PSObject
   $xmlmcOutput = Invoke-HB-XMLMC "data" "listAddItem"
   $exceptionName = "" 
   $exceptionSummary = ""
   
    # Read output status
    if($xmlmcOutput.params.itemValue -and $xmlmcOutput.params.itemValue -eq ""){
            $itemValue = $name
            $requestWarnings = $xmlmcOutput.params.warnings
        }
        if($xmlmcOutput.params.exceptionName -and $xmlmcOutput.params.exceptionName -ne ""){
            $exceptionName = $xmlmcOutput.params.exceptionName
            $exceptionSummary = $xmlmcOutput.params.exceptionDescription
        }
    
    # Build resultObject to write to output
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        ExceptionName = $exceptionName
        ExceptionSummary = $exceptionSummary
        itemValue = $itemValue
        }

    if($resultObject.Status -ne "ok" -or $exceptionName -ne ""){
        Write-Error $resultObject
    } else {
        Write-Output $resultObject
       }

This:

$csv = Import-Csv applications.csv'
    foreach($row in $csv)
    {
        $name = $row.Name
   }

Iterates through a column in a csv file and returns 2734 items, but only "Total: 2734" gets added to the list.

How do I convert each csv item to an individual stringValue and add all 2734 items to a Hornbill simple list?

Link to comment
Share on other sites

Hi @Rob Gething,

You might want to have a look at https://github.com/hornbill/goSimpleListImport

As before, we do not provide support/assistance on creating/modifying PowerShell scripts. You might want to ask Google for samples: https://www.google.com/search?q=how+to+read+and+process+csv+using+powershell or turn to a PowerShell specific forum and ask your question there: https://forums.powershell.org/ (there are others).

 

 

Link to comment
Share on other sites

I am posting my final working code for the benefit of others:

# Define output stream type
    [CmdletBinding()]

    # Define runbook input params
    Param
    (
        # API Params
        [string] $application = "com.hornbill.servicemanager",
        [string] $listName,
        [string] $itemNameTranslation
        )

   $csv = Import-Csv -Path 'All applications.csv'
   foreach($row in $csv)
   {
   $name = $row.Name
   Add-HB-Param "application" $application $false
   Add-HB-Param "listName" $listName $false
   Add-HB-Param "itemNameTranslation" $itemNameTranslation $false
   Add-HB-Param "itemValue" $name $false
   Add-HB-Param "defaultItemName" $name $false
   Invoke-HB-XMLMC "data" "listAddItem"
   }

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...