Add file to a site

Hi :slight_smile:
How can I add a file to a site programmatically with a script using API or something else ?
Thanks

You can use the REST API.
Example of how to upload a file here

1 Like

Thanks gearoid :wink:
Do you know if there is an example for powershell ?

Sorry wrong api call in the earlier post
You can use the sites - there’s a example of posting a file here.

https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Tivoli%20Endpoint%20Manager/page/RESTAPI%20Site

I think there might be some powershell examples here

2 Likes

I would recommend avoiding adding a file to a site unless absolutely necessary, and only for very small files.

What is your use case?


Related:

That’s exactly what you wrote: absolutely necessary + very small file :slight_smile:

1 Like

Here I am!
Following this example
https://www.ibm.com/developerworks/community/blogs/edgeCity/entry/still_restless_with_powershell?lang=en
I have some trouble to POST request, in this case, to add a file to a site.
I’m using the command
Invoke-RestMethod -Uri $url -Method POST -Headers $headers -Body $newXml
where
url = http://mysite:52311/api/site/custom/mycustomsite/files
headers = contains ISClientFile: 1 (as suggested in https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Tivoli%20Endpoint%20Manager/page/RESTAPI%20Site )
but I don’t know how to pass a test.txt file :frowning:

… any suggestions ?
Thanks

I’d recommend testing using CURL first.

I don’t believe you send an XML body and instead send raw data with the file contents, but that is just off the top of my head, so I’d need to look into it.

I’m stubborn :grin: … so I continue with Invoke-RestMethod :stuck_out_tongue:

As specified in this page


"For other request types (such as POST), the body is set as the value of the request body in the standard name=value format."
so I set my variable as

$newXml = “file=$(get-content myfile)”

But I get this error message
"Must supply a file in the POST body."

… it’s a file … damn :frowning:
I don’t know where to bang my head :frowning:

1 Like

I believe in this case the body should be the file itself with nothing else.

$rawData = $newXML = $(get-content myfile)

Well, maybe not now that I look into it a bit more.

I would check what your post request actually looks like in its RAW form to see if it matches the format it should take. I think you can output the POST request to a file using CURL instead of actually POSTing to a URL to check it. I’m not certain how you would do this using PowerShell, but presumably you could check it by sending the POST request to a different URL and capturing it there.

I’m banging my head for weeks! No way! :frowning:

… anyone has done it ? :expressionless:

I know it has been done.

Have you tried to get it to work using cURL first?

With curl in Linux environment it works

curl -k -u theuser -H "ISClientFile: 1" -X POST -F "file=@/root/lista.txt" "https://theserver:52311/api/site/master/files"

So you have it working, you just want to do it using PowerShell? I don’t know how to do the equivalent in PowerShell, but there should be examples of how to translate cURL commands to PowerShell. Worst case you could probably use libcurl in powershell, or subprocess cURL from powershell. I do like using PowerShell for Windows specific tasks, but I avoid it for anything that could be done cross platform.

You could actually do it using cURL on Windows/Mac/Linux using a BigFix Task/Fixlet and a secure parameter for the console operator password.

Also, I wouldn’t recommend adding files to the Master Action Site. Adding files to sites in general is bad, adding anything unless absolutely mandatory to the Master Action Site is worse. I would recommend using a custom site that only has the computers subscribed that need the file.