/pwshCloudflare

Primary LanguagePowerShellMIT LicenseMIT

pwshCloudflare

Minimum Supported PowerShell Version Cross Platform License

Branch Windows - PowerShell Windows - pwsh Linux MacOS
main Build Status Windows PowerShell Main Build Status Windows pwsh Main Build Status Linux Main Build Status MacOS dev
dev Build Status Windows PowerShell dev Build Status Windows pwsh dev Build Status Linux dev Build Status MacOS dev

Description

PowerShell module for the Cloudflare Client API v4. See similar projects python-cloudflare and cloudflare-go as well as Cloudflare products

Functions

Function ApiOperation
Get-CFAccount Account Details
Get-CFD1Database Get D1 Database
Get-CFZone List Zones
Get-CFZoneRecord List DNS Records
Invoke-CFD1Query Query D1 Database
New-CFD1Database Create D1 Database
New-CFZoneRecord Create DNS Record
Remove-CFD1Database Delete D1 Database
Remove-CFZoneRecord Delete DNS Record
Set-CFZoneRecord Patch DNS Record

Getting Started

Installation

pwshCloudflare will be published to PSGallery in the future. For now clone this repo and import the module.

git clone https://github.com/connorcarnes/pwshCloudflare
Import-Module '.pwshCloudflare/src/pwshCloudflare/pwshCloudflare.psd1'

Authentication

Set-CloudflareSession creates a [Microsoft.PowerShell.Commands.WebRequestSession] object with the appropriate headers, saves it to $Script:cfSession, and uses it make subsequent API calls. By default Set-CloudflareSession only configures authentication for the current session. Use the -SaveToFile and -LoadOnImport parameters to save your configuration and have it load on module import. Your credentials will be stored in plaintext. Examples are included in the Quickstart section below.

You can use API token authentication, API key authentication, or both. See Get Started - Cloudflare Fundamentals for details. Set-CloudflareSession will validate the provided credentials with Test-CloudflareSession.

Quick start

Authenticate for the current PowerShell session

$Splat = @{
    Email    = "user@example.com"
    ApiKey   = "API_KEY"
    ApiToken = "API_TOKEN"
}
Set-CloudflareSession @Splat

Save configuration to a file and have it load on module import

# Your credentials will be stored in plaintext. Default config file location:
# $Folder = [Environment]::GetFolderPath([Environment+SpecialFolder]::UserProfile)
# $FilePath = "$Folder\.pwshCloudflare\config.xml"
$Splat = @{
    Email        = "user@example.com"
    ApiKey       = "API_KEY"
    ApiToken     = "API_TOKEN"
    SaveToFile   = $true
    LoadOnImport = $true
}
Set-CloudflareSession @Splat

List all zones

Get-CfZone

Get DNS records by zone name

Get-CFZoneRecord -ZoneName 'example.com'

Get DNS records by zone id

$ZoneName = 'example.com'
$Zone = Get-CfZone | Where-Object {$_.Name -eq $ZoneName}
Get-CFZoneRecord -ZoneId $Zone.id

Contributors