I'm trying to automate publishing the ExtractData task as Geoprocessing Service. To do this, I created a small Python Script which executes the ExtractData Tool and then it should publish the result object as a Geoproccesing Service. The input to the script comes from an ArcGIS Pro Project, I tried it with a layer from my Enterprise Geodatabase and with a layer from the default project file geodatabase. Everytime I run the script from the commandline, it fails with the Error 999999, however, when I run the script from the python console within ArcGIS Pro, it runs fine.

import arcpy, sys, getopt, os
#modify this function to run your toolbox
# Run the tool and set to a result object
inputaprx = 'C:\\Users\\joelh\Documents\\ArcGIS\\Projects\\TestExtract\\TestExtract.aprx'
aprx = arcpy.mp.ArcGISProject(inputaprx)
#aprx = arcpy.mp.ArcGISProject("CURRENT") #voor gebruik in ArcGIS Pro
outputfile = 'C:\\Users\\joelh\Documents\\ArcGIS\\Projects\\TestExtract\\extract.zip'
mp = aprx.listMaps("*")[0]
layers = mp.listLayers("*")
sddraft = "testservice.sddraft"
sd = "testservice.sd"
serviceName = "ExtractData"
connection_file_path = "server.ags"
copyData = True
servicefolder = "Test"
summary = "Extract Data"
tags = "Tag"
executionType = "Asynchronous"
serverURL = "https://my.arcgis.enterprise/server"
arcpy.AddMessage("Executing Extract task")
result = arcpy.server.ExtractData(layers, layers[0], "File Geodatabase - GDB - .gdb", "ESRI GRID - GRID", "Same As Input", None, outputfile)
#result = arcpy.server.ExtractDataTask(layers, layers[0], "File Geodatabase - GDB - .gdb", "ESRI GRID - GRID", outputfile)
arcpy.AddMessage("Extract task message count {}".format(result.messageCount))
arcpy.AddMessage("Messages: {}".format(result.getMessages(0)))
arcpy.AddMessage("Warning Messages: {}".format(result.getMessages(1)))
arcpy.AddMessage("Error Messages: {}".format(result.getMessages(2)))
server_type = "FROM_CONNECTION_FILE"
analyzeMessages = arcpy.CreateGPSDDraft(
result, sddraft, serviceName, server_type=server_type,connection_file_path = connection_file_path,
copy_data_to_server=copyData, folder_name=servicefolder,
summary=summary, tags=tags, executionType=executionType,
resultMapServer=False, showMessages="INFO", maximumRecords=5000,
minInstances=1, maxInstances=1, maxUsageTime=100, maxWaitTime=10,
maxIdleTime=180)
arcpy.AddMessage("Messages {}".format(analyzeMessages))
if analyzeMessages['errors'] == {}:
# Execute StageService
arcpy.StageService_server(sddraft, sd)
arcpy.UploadServiceDefinition_server(sd, serverURL)
Any ideas why this script fails on the command line?