]> BookStack Code Mirror - api-scripts/blob - powershell-files-to-pages/files-to-pages.ps1
Added powershell-files-to-pages example
[api-scripts] / powershell-files-to-pages / files-to-pages.ps1
1 # We receive the book ID as an argument from when the script is ran.
2 # This will be the ID of the book we're uploaded pages into.
3 param(
4     [Parameter(Mandatory=$true)]
5     [string]$parentBookId
6 )
7
8 # BookStack API variables
9 # Uses values from the environment otherwise could be hardcoded here
10 $baseUrl = $env:BS_URL
11 $tokenId = $env:BS_TOKEN_ID
12 $tokenSecret = $env:BS_TOKEN_SECRET
13
14 # Function to create a page in BookStack
15 function Create-BookstackPage {
16     param(
17         [Parameter(Mandatory=$true)]
18         [string]$Name,
19
20         [Parameter(Mandatory=$true)]
21         [string]$Html,
22
23         [Parameter(Mandatory=$true)]
24         [int]$BookId
25     )
26
27     # Create the data to send to the API
28     $body = @{
29         name = $Name
30         html = $Html
31         book_id = $BookId
32     } | ConvertTo-Json
33
34     # Ready the HTTP headers, including our auth header
35     $authHeader = "Token {0}:{1}" -f $tokenId, $tokenSecret
36     $headers = @{
37         "Content-Type" = "application/json"
38         "Authorization" = $authHeader
39     }
40
41     # Send the request to our API endpoint as a POST request
42     $url = "{0}/api/pages" -f $baseUrl
43     Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body
44 }
45
46 # Get the html files in the relative "./files" directory
47 $files = Get-ChildItem -Path ".\files" -Filter "*.html" -File
48
49 # For each of our HTML files, get the file data and name
50 # then create a page with name matching the filename
51 # and HTML content using the contents of the file.
52 foreach ($file in $files) {
53     $fileContent = Get-Content -Path $file.FullName
54     Create-BookStackPage $file.Name $fileContent $parentBookId
55 }
56