Employee Updater API, Using PowerShell

derekritchison
derekritchison Expensify Customer Posts: 6 Expensify Newcomer

Hey all, I posted this question in a somewhat relevant thread but hadn't received a response, possibly because the thread was a bit old. Anyway, I am working on automating new user creation with my onboarding script, which is written in PowerShell. I've pieced together quite a bit just from reading questions in this community, but I'm now stuck trying to figure out how to properly translate the "send the CSV" portion of the curl. Here's how I'm writing the JSON, but if anyone has any experience with the "data" portion that would be terrific.

$json = [ordered]@{

    "requestJobDescription" = @{

            "type" = "update";

           "credentials" = @{

                    "partnerUserID" = $expensify_id;

                    "partnerUserSecret" = $expensify_secret;

            }

            "inputSettings" = @{

                    "type" = "employees";

                    "policyID" = "$policy_ID";

                    "fileType" = "csv";

            }

        };

    "data" = "C:\expensify.csv";

    } | ConvertTo-Json -Depth 5

Best Answer

Answers

  • derekritchison
    derekritchison Expensify Customer Posts: 6 Expensify Newcomer

    Just bumping this request. I would love it if anyone has had any experience using the Employee Updated API with PowerShell.

  • Julia
    Julia Expensify Customer Posts: 164 Expensify Pro

    Hey @derekritchison , make sure you select 'No' under your response for 'Does this answer your question?'. This will make sure it isn't closed incorrectly as answered. I'm curious about any resolution you might reach because we are having issues with our API as well. I doubt they're directly related, but hoping the extra visibility leads to them uncovering some root issues (Also answer 'no' for my response as well).

  • derekritchison
    derekritchison Expensify Customer Posts: 6 Expensify Newcomer

    Thank you for the tip! And yes, would love to learn how to send this CSV to the API via PowerShell / Invoke-WebRequest! I feel like I'm so close. Happy to share my entire code if anyone familiar with Invoke-WebRequest wants to take a look.

  • Nicole Trepanier
    Nicole Trepanier Expensify Team Posts: 498 Expensify Team

    Hi @derekritchison! One of our other users, @DMuncy, shared this great example of a successful request using PowerShell. Check it out and don't hesitate to ping other users that you know have experience based on other threads. Our community is always happy to help!

  • DMuncy
    DMuncy Expensify Customer Posts: 3 Expensify Newcomer

    I unfortunately never tried this feature set, it felt lacking in details for our use.

    Looks like you may need to use "-infile" to define the path to the file to upload

    However perhaps this is of use?

    or:


  • derekritchison
    derekritchison Expensify Customer Posts: 6 Expensify Newcomer

    Sorry for the delay here. Our HQ relocated this week and I've been slammed with that project for a while. Anyway... no, I still cannot get this to work, and -InFile did not do the trick either. I'm still seeing one of two errors, depending on how I "musical chairs" some of this. I'm either getting the "requestJobDescription not found" error, or using the code below an "Internal Server Error" response. I would REALLY like to figure this out, and I'm quite surprised that nobody on the boards has used the API to automate provisioning for new hires as it seems like such an obvious time save. I'd even be willing to look at doing this in PHP or Ruby if anyone has successfully written this in either of those languages.


    Re-submitting my current code that is giving me the internal server error response.


    $expensify_url = "https://integrations.expensify.com/Integration-Server/ExpensifyIntegrations"

    $expensify_id = "REMOVED"

    $expensify_secret = "REMOVED"


    $dept = "IT"


    if ($dept -eq "FP&A" -or $dept -eq "IT") {

        $expensify_admin = "TRUE"

    } else {

        $expensify_admin = "FALSE"

    }


    $email = "test.user@domain.com"

    $mngr_email = "test.manager@domain.com"

    $fwd_email = "finance@domain.com"


    $expensify_csv = @(

        [pscustomobject]@{

        EmployeeEmail = $email

        ManagerEmail  = $mngr_email

        Admin  = $expensify_admin

        ForwardManagerEmail = $fwd_email

        }

    )


    $expensify_csv | Export-Csv -Path C:\expensify.csv -NoTypeInformation

    $upload = Get-Item -Path C:\expensify.csv


    $csv = "EmployeeEmail,ManagerEmail,Admin,ForwardManagerEmail`r`test.user@domain.com,test.manager@domain.com,TRUE,finance@domain.com`r`n"


    Write-Host $csv


    $json = [ordered]@{

        "requestJobDescription" = @{

            "type" = "update";

            "credentials" = @{

                "partnerUserID" = $expensify_id;

                "partnerUserSecret" = $expensify_secret;

            }

            "inputSettings" = @{

                "type" = "employees";

                "policyID" = "REMOVED";

                "fileType" = "csv";

            }

        };

        "data" = Get-Content($upload) -Raw

    } | ConvertTo-Json -Depth 5


    Write-Host $json


    $check = Invoke-WebRequest `

        -Method Post `

        -Uri $expensify_url `

        -Body $json `

        -ContentType multipart/form-data


    Write-Host $check


    exit

  • Ariel Green
    Ariel Green Expensify Team, Expensify Student Ambassador Posts: 111 Expensify Team

    Thanks a bunch for following up here with your solution, @derekritchison!