Skip to content

Instantly share code, notes, and snippets.

@wildone
Last active June 19, 2020 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wildone/93f68c3f21b4336d63508cba7754f5ee to your computer and use it in GitHub Desktop.
Save wildone/93f68c3f21b4336d63508cba7754f5ee to your computer and use it in GitHub Desktop.
Powershell Do Sling Post
function doSlingPost {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[string]$Url="http://localhost:4502",
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[ValidateSet('Post','Delete')]
[string]$Method,
[Parameter(Mandatory=$false)]
[HashTable]$Body,
[Parameter(Mandatory=$false,
HelpMessage="Provide Basic Auth Credentials in following format: <user>:<pass>")]
[string]$BasicAuthCreds="",
[Parameter(Mandatory=$false)]
[string]$UserAgent="",
[Parameter(Mandatory=$false)]
[string]$Referer="",
[Parameter(Mandatory=$false)]
[string]$Timeout="5"
)
$HEADERS = @{
}
if (-not([string]::IsNullOrEmpty($UserAgent))) {
$HEADERS.add("User-Agent",$UserAgent)
}
if (-not([string]::IsNullOrEmpty($Referer))) {
$HEADERS.add("Referer",$Referer)
}
if (-not([string]::IsNullOrEmpty($BasicAuthCreds))) {
$BASICAUTH = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($BasicAuthCreds))
$HEADERS.add("Authorization","Basic $BASICAUTH")
}
Write-Host "Performing action $Method on $Url."
(Invoke-WebRequest -Method Post -Headers $HEADERS -TimeoutSec $Timeout -Uri "$Url" -Body $Body -ContentType "application/x-www-form-urlencoded").Content
Write-Host "Body:"
$Body | ConvertTo-Json
}
$SERVER_PATH_CONFIG_WEBDAV = "/apps/system/config/org.apache.sling.jcr.davex.impl.servlets.SlingDavExServlet"
$LOGIN = "admin:admin"
$SERVER_HOST = "localhost"
$PROTOCOL = "http"
$PORT = 4502
$TIMEOUT = 5
$BODY = @{
"jcr:primaryType"="sling:OsgiConfig"
"alias"="/crx/server"
"dav.create-absolute-uri"="true"
"dav.create-absolute-uri@TypeHint"="Boolean"
"../../jcr:primaryType"="sling:Folder"
"../jcr:primaryType"="sling:Folder"
}
$ADDRESS = "${PROTOCOL}://${SERVER_HOST}:${PORT}"
#Disable WebDav
doSlingPost -Method Delete -Referer $ADDRESS -UserAgent "curl" -Body $BODY -Url ${ADDRESS}${SERVER_PATH_CONFIG_WEBDAV} -BasicAuthCreds $LOGIN -Timeout $TIMEOUT
# curl -u admin:admin -H User-Agent:curl -X DELETE http://localhost:4502/apps/system/config/org.apache.sling.jcr.davex.impl.servlets.SlingDavExServlet
#Enable WebDav
doSlingPost -Body $BODY -Method Post -Referer $ADDRESS -UserAgent "curl" -Url ${ADDRESS}${SERVER_PATH_CONFIG_WEBDAV} -BasicAuthCreds $LOGIN -Timeout $TIMEOUT
# curl -u admin:admin -H User-Agent:curl -F "jcr:primaryType=sling:OsgiConfig" -F "alias=/crx/server" -F "dav.create-absolute-uri=true" -F "dav.create-absolute-uri@TypeHint=Boolean" -F"../../jcr:primaryType=sling:Folder" -F"../jcr:primaryType=sling:Folder" http://localhost:4502/apps/system/config/org.apache.sling.jcr.davex.impl.servlets.SlingDavExServlet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment