Using Powershell to input request details works only once

Using Powershell to input request details works only once

Hi,

I have created a powershell script that sends some details back to a request (it changes the user and the custom fields called 'Mappenavn'). Also it adds an approver to the request.

My problem is that either of these two functions works alone. But the second I add both functions in the same script none of them works. If I then comment out one of the functions, the other starts working again.

Can anyone tell me what I'm doing wrong and how I fix it?


Script:

param (
    $json = "none"
)

$jsondata = Get-Content $json -Encoding UTF8   #Encode if necessary
$obj = ConvertFrom-Json $jsondata

$workorderid = $obj.request.WORKORDERID
$requester = $obj.request.REQUESTER
$subject = $obj.request.SUBJECT
$description = $obj.request.description
$mappe = $obj.request.Mappenavn

$sdphost = "http://servicedeskURL/"
$techkey = "1234....."

$url = $sdphost + "sdpapi/request/" + $workorderid
$method = "POST"
$operation = "EDIT_REQUEST"

Import-Module ActiveDirectory

#Get user's full name
$initialer = ($subject -split "DOMAINNAME\\")[1].substring(0,4)
$fullname = Get-ADUser -id $initialer | Select-Object Name -ExpandProperty Name

#Get folder name
$mappesti = ($description -split 'SHARENAME\$\\')[1]
$mappenavn = ($mappesti -split '\\\\')[0]

#FUNCTION 1: Update request with user name and folder name
$input = @"
    <Operation>
        <Details>
            <requester>$fullname</requester>
            <Mappenavn>$mappenavn</Mappenavn>
        </Details>
    </Operation>
"@
$parameter = @{INPUT_DATA=$input;OPERATION_NAME=$operation;TECHNICIAN_KEY=$techkey}
$result = Invoke-WebRequest -Uri $url -Method POST -Body $parameter

#FUNCTION 2: Add approver
$gruppe = $mappenavn -replace " ","-"
$ADgruppe = "GroupNameInAD"
$approver = Get-ADGroup -id $ADgruppe -Properties ManagedBy | select ManagedBy -ExpandProperty ManagedBy | get-aduser -Properties EmailAddress | select EmailAddress -ExpandProperty EmailAddress
$data = "{'operation': [{'INPUT_DATA': [{'StageOne': ['$approver']}], 'OPERATIONNAME': 'ADD_APPROVAL_STAGE'}], 'message': 'Approver added', 'result': 'success'}"
return $data


It's FUNCTION1 and FUNCTION2 respectively. If I comment out either of them the other starts working immidiately. Having both activated makes none of them work :(

Hope to see some good suggestions/solutions.

/Rasmus

                  New to ADSelfService Plus?