]> BookStack Code Mirror - api-scripts/commitdiff
Added powershell-files-to-pages example
authorDan Brown <redacted>
Wed, 1 Mar 2023 22:50:16 +0000 (22:50 +0000)
committerDan Brown <redacted>
Wed, 1 Mar 2023 22:50:16 +0000 (22:50 +0000)
powershell-files-to-pages/docker-compose.yml [new file with mode: 0644]
powershell-files-to-pages/files-to-pages.ps1 [new file with mode: 0644]
powershell-files-to-pages/files/example-a.html [new file with mode: 0644]
powershell-files-to-pages/files/example-b.html [new file with mode: 0644]
powershell-files-to-pages/readme.md [new file with mode: 0644]

diff --git a/powershell-files-to-pages/docker-compose.yml b/powershell-files-to-pages/docker-compose.yml
new file mode 100644 (file)
index 0000000..13d9d6a
--- /dev/null
@@ -0,0 +1,8 @@
+version: '3'
+services:
+    powershell:
+        image: mcr.microsoft.com/powershell:latest
+        volumes:
+            - .:/app
+        working_dir: /app
+        command: ["pwsh"]
\ No newline at end of file
diff --git a/powershell-files-to-pages/files-to-pages.ps1 b/powershell-files-to-pages/files-to-pages.ps1
new file mode 100644 (file)
index 0000000..d909bc4
--- /dev/null
@@ -0,0 +1,56 @@
+# We receive the book ID as an argument from when the script is ran.
+# This will be the ID of the book we're uploaded pages into.
+param(
+    [Parameter(Mandatory=$true)]
+    [string]$parentBookId
+)
+
+# BookStack API variables
+# Uses values from the environment otherwise could be hardcoded here
+$baseUrl = $env:BS_URL
+$tokenId = $env:BS_TOKEN_ID
+$tokenSecret = $env:BS_TOKEN_SECRET
+
+# Function to create a page in BookStack
+function Create-BookstackPage {
+    param(
+        [Parameter(Mandatory=$true)]
+        [string]$Name,
+
+        [Parameter(Mandatory=$true)]
+        [string]$Html,
+
+        [Parameter(Mandatory=$true)]
+        [int]$BookId
+    )
+
+    # Create the data to send to the API
+    $body = @{
+        name = $Name
+        html = $Html
+        book_id = $BookId
+    } | ConvertTo-Json
+
+    # Ready the HTTP headers, including our auth header
+    $authHeader = "Token {0}:{1}" -f $tokenId, $tokenSecret
+    $headers = @{
+        "Content-Type" = "application/json"
+        "Authorization" = $authHeader
+    }
+
+    # Send the request to our API endpoint as a POST request
+    $url = "{0}/api/pages" -f $baseUrl
+    Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body
+}
+
+# Get the html files in the relative "./files" directory
+$files = Get-ChildItem -Path ".\files" -Filter "*.html" -File
+
+# For each of our HTML files, get the file data and name
+# then create a page with name matching the filename
+# and HTML content using the contents of the file.
+foreach ($file in $files) {
+    $fileContent = Get-Content -Path $file.FullName
+    Create-BookStackPage $file.Name $fileContent $parentBookId
+}
+
diff --git a/powershell-files-to-pages/files/example-a.html b/powershell-files-to-pages/files/example-a.html
new file mode 100644 (file)
index 0000000..dc68fcf
--- /dev/null
@@ -0,0 +1 @@
+<p>This is exmaple A!</p>
\ No newline at end of file
diff --git a/powershell-files-to-pages/files/example-b.html b/powershell-files-to-pages/files/example-b.html
new file mode 100644 (file)
index 0000000..e723bd5
--- /dev/null
@@ -0,0 +1 @@
+<blockquote>This is example B!</blockquote>
\ No newline at end of file
diff --git a/powershell-files-to-pages/readme.md b/powershell-files-to-pages/readme.md
new file mode 100644 (file)
index 0000000..0162725
--- /dev/null
@@ -0,0 +1,42 @@
+# Create BookStack Pages from HTML Files
+
+This script will scan through a local `files/` directory for `*.html` files then create pages for each within BookStack, where the name of the files is used for the name of the page and the contents of the file is used for the BookStack page content.
+
+**This is a very simplistic single-script-file example of sending data via PowerShell** and was written with little prior PowerShell knowledge.
+
+## Requirements
+
+You will need PowerShell available.
+You will also need BookStack API credentials (TOKEN_ID & TOKEN_SECRET) at the ready.
+
+**This script was written using PowerShell (Core) 7.2.10 on Linux".
+
+A `docker-compose.yml` file exists just as a convenient way to run PowerShell, particularly for Linux users. 
+
+## Running
+
+First of all, download the `files-to-pages.ps1` script file.
+Then you'll need to setup your environment to point to your BookStack instance with the right credentials:
+
+```powershell
+# Environment Setup
+# ALTERNATIVELY: Open the script and edit the variables at the top
+Set-Item -Path env:BS_URL -Value "https://bookstack.example.com" # Set to be your BookStack base URL
+Set-Item -Path env:BS_TOKEN_ID -Value "abc123" # Set to be your API token_id
+Set-Item -Path env:BS_TOKEN_SECRET -Value "123abc" # Set to be your API token_secret
+```
+
+Once configured, then create a `files` folder containing HTML files you want to upload as pages. See the `files` directory of this repo as a very basic example.
+Now it's time to run the script like so:
+
+```powershell
+./files-to-pages.ps1 <target_book_id>
+```
+
+## Examples
+
+```powershell
+# Upload HTML files in the relative `files` directory as BookStack pages
+# into the existing BookStack "Book" that has an ID of 5:
+./files-to-pages.ps1 5
+```
\ No newline at end of file
Morty Proxy This is a proxified and sanitized view of the page, visit original site.