PowerShell/docs/new-qrcode.md

114 lines
3.2 KiB
Markdown
Raw Normal View History

2024-01-25 13:37:12 +01:00
Script: *new-qrcode.ps1*
========================
2021-11-08 21:36:42 +01:00
2022-02-10 09:01:07 +01:00
This PowerShell script generates a new QR code image file.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/new-qrcode.ps1 [[-text] <String>] [[-imageSize] <String>] [[-fileFormat] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2024-11-08 12:35:11 +01:00
-text <String>
2021-11-08 21:36:42 +01:00
Specifies the text to use
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
2024-11-08 12:35:11 +01:00
-imageSize <String>
2021-11-08 21:36:42 +01:00
Specifies the image size (width x height)
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
2024-11-08 12:35:11 +01:00
-fileFormat <String>
Specifies the image file format
Required? false
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters? false
2021-11-08 21:36:42 +01:00
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
PS> ./new-qrcode.ps1 "Fasten seatbelt" 500x500 JPG
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2024-11-08 12:35:11 +01:00
Creates a QR code
2022-11-17 20:02:26 +01:00
.DESCRIPTION
This PowerShell script generates a new QR code image file.
2024-11-08 12:35:11 +01:00
.PARAMETER text
2022-11-17 20:02:26 +01:00
Specifies the text to use
2024-11-08 12:35:11 +01:00
.PARAMETER imageSize
2022-11-17 20:02:26 +01:00
Specifies the image size (width x height)
2024-11-08 12:35:11 +01:00
.PARAMETER fileFormat
Specifies the image file format
2022-11-17 20:02:26 +01:00
.EXAMPLE
2024-11-08 12:35:11 +01:00
PS> ./new-qrcode.ps1 "Fasten seatbelt" 500x500 JPG
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-11-08 12:35:11 +01:00
param([string]$text = "", [string]$imageSize = "", [string]$fileFormat = "")
2022-11-17 20:02:26 +01:00
try {
2024-11-08 12:35:11 +01:00
if ($text -eq "") { $text = Read-Host "Enter text or URL" }
if ($imageSize -eq "") { $imageSize = Read-Host "Enter image size, e.g. 500x500" }
if ($fileFormat -eq "") { $fileFormat = Read-Host "Enter the image file format, e.g. JPG" }
2022-11-17 20:02:26 +01:00
$ECC = "M" # can be L, M, Q, H
$QuietZone = 1
$ForegroundColor = "000000"
$BackgroundColor = "ffffff"
if ($IsLinux) {
2024-11-08 12:35:11 +01:00
$pathToPictures = Resolve-Path "$HOME/Pictures"
2022-11-17 20:02:26 +01:00
} else {
2024-11-08 12:35:11 +01:00
$pathToPictures = [Environment]::GetFolderPath('MyPictures')
2022-11-17 20:02:26 +01:00
}
2024-11-08 12:35:11 +01:00
if (-not(Test-Path "$pathToPictures" -pathType container)) { throw "Pictures folder at 📂$Path doesn't exist (yet)" }
$newFile = "$pathToPictures/QR_code.$fileFormat"
2022-11-17 20:02:26 +01:00
2024-11-08 12:35:11 +01:00
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile(("http://api.qrserver.com/v1/create-qr-code/?data=" + $text + "&ecc=" + $ECC +`
"&size=" + $imageSize + "&qzone=" + $QuietZone + `
2022-11-17 20:02:26 +01:00
"&color=" + $ForegroundColor + "&bgcolor=" + $BackgroundColor.Text + `
2024-11-08 12:35:11 +01:00
"&format=" + $fileFormat), $newFile)
2022-11-17 20:02:26 +01:00
2024-11-08 12:35:11 +01:00
"✅ New QR code saved as: $newFile"
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-08 12:35:11 +01:00
*(generated by convert-ps2md.ps1 using the comment-based help of new-qrcode.ps1 as of 11/08/2024 12:34:52)*