Update New-SamyPrinterProfileJson.ps1
This commit is contained in:
@@ -101,29 +101,14 @@ function New-SamyPrinterProfileJson {
|
||||
[SecureString]$GitToken
|
||||
)
|
||||
|
||||
# Helper: safe logging that prefers Write-LogHybrid if available
|
||||
function _WriteLog {
|
||||
param(
|
||||
[string]$Message,
|
||||
[string]$Level = "Info",
|
||||
[string]$Task = "PrinterJson"
|
||||
)
|
||||
|
||||
if (Get-Command Write-LogHybrid -ErrorAction SilentlyContinue) {
|
||||
Write-LogHybrid $Message $Level $Task
|
||||
} else {
|
||||
Write-Host "[$Level] [$Task] $Message"
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
_WriteLog "Starting New-SamyPrinterProfileJson for ClientCode='$ClientCode' Location='$Location'." "Info"
|
||||
Write-Log "Starting New-SamyPrinterProfileJson for ClientCode='$ClientCode' Location='$Location'." "Info" "PrinterJson" -LogToEvent
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 1) Ensure output folder exists and build a safe file name
|
||||
# ------------------------------------------------------------------
|
||||
if (-not (Test-Path $OutputPath)) {
|
||||
_WriteLog "Creating output folder '$OutputPath'." "Info"
|
||||
Write-Log "Creating output folder '$OutputPath'." "Info" "PrinterJson" -LogToEvent
|
||||
New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
|
||||
}
|
||||
|
||||
@@ -137,9 +122,9 @@ function New-SamyPrinterProfileJson {
|
||||
$printers = Get-Printer -ErrorAction SilentlyContinue
|
||||
|
||||
if (-not $printers) {
|
||||
_WriteLog "No printers found on this system. JSON will be empty." "Warning"
|
||||
Write-Log "No printers found on this system. JSON will be empty." "Warning" "PrinterJson" -LogToEvent
|
||||
} else {
|
||||
_WriteLog ("Found {0} printer(s)." -f $printers.Count) "Info"
|
||||
Write-Log ("Found {0} printer(s)." -f $printers.Count) "Info" "PrinterJson" -LogToEvent
|
||||
}
|
||||
|
||||
$profiles = @()
|
||||
@@ -180,101 +165,3 @@ function New-SamyPrinterProfileJson {
|
||||
|
||||
ProfileName = $profileName
|
||||
DisplayName = $displayName
|
||||
|
||||
Type = $type
|
||||
Address = $address
|
||||
PrintServer = $printServer
|
||||
ShareName = $shareName
|
||||
|
||||
DriverName = $driverName
|
||||
|
||||
DriverInfPath = ""
|
||||
DriverPackagePath = ""
|
||||
DriverInfName = ""
|
||||
|
||||
IsDefault = $isDefault
|
||||
|
||||
_comment1 = "Review Type/Address/PrintServer/ShareName before use."
|
||||
_comment2 = "Fill DriverPackagePath and DriverInfName for repo-based install."
|
||||
}
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 3) Write JSON to disk
|
||||
# ------------------------------------------------------------------
|
||||
$json = $profiles | ConvertTo-Json -Depth 5
|
||||
|
||||
$json | Set-Content -Path $filePath -Encoding UTF8
|
||||
_WriteLog ("Wrote {0} profile(s) to '{1}'." -f $profiles.Count, $filePath) "Success"
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 4) Optional: upload to Git (Gitea)
|
||||
# ------------------------------------------------------------------
|
||||
if ($UploadToGit) {
|
||||
if (-not $GitApiBase -or -not $GitRepo -or -not $GitPath -or -not $GitToken) {
|
||||
_WriteLog "UploadToGit requested but GitApiBase, GitRepo, GitPath, or GitToken is missing. Skipping upload." "Warning"
|
||||
}
|
||||
else {
|
||||
_WriteLog "Uploading JSON to Git repo '$GitRepo' (branch '$GitBranch', path '$GitPath')." "Info"
|
||||
|
||||
# Convert SecureString token to plain text (in memory only)
|
||||
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($GitToken)
|
||||
try {
|
||||
$plainToken = [Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
|
||||
}
|
||||
finally {
|
||||
if ($bstr -ne [IntPtr]::Zero) {
|
||||
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
|
||||
}
|
||||
}
|
||||
|
||||
# Prepare API URL and content
|
||||
$apiUrl = "{0}/repos/{1}/contents/{2}" -f $GitApiBase.TrimEnd('/'), $GitRepo, $GitPath
|
||||
$contentB64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($json))
|
||||
|
||||
$headers = @{
|
||||
Authorization = "token $plainToken"
|
||||
}
|
||||
|
||||
# Try to see if file already exists to retrieve its SHA
|
||||
$existingSha = $null
|
||||
try {
|
||||
$existing = Invoke-RestMethod -Uri ($apiUrl + "?ref=$GitBranch") -Headers $headers -Method Get -ErrorAction Stop
|
||||
if ($existing.sha) {
|
||||
$existingSha = $existing.sha
|
||||
}
|
||||
}
|
||||
catch {
|
||||
# 404 is fine (file does not exist yet), anything else we log
|
||||
if ($_.Exception.Response.StatusCode.Value__ -ne 404) {
|
||||
_WriteLog ("Git pre-check failed: {0}" -f $_.Exception.Message) "Warning"
|
||||
}
|
||||
}
|
||||
|
||||
$body = @{
|
||||
message = "Update printers for $ClientCode / $Location"
|
||||
branch = $GitBranch
|
||||
content = $contentB64
|
||||
}
|
||||
if ($existingSha) {
|
||||
$body.sha = $existingSha
|
||||
}
|
||||
|
||||
try {
|
||||
$bodyJson = $body | ConvertTo-Json -Depth 5
|
||||
$null = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method Put -Body $bodyJson -ContentType "application/json" -ErrorAction Stop
|
||||
_WriteLog "Git upload completed successfully." "Success"
|
||||
}
|
||||
catch {
|
||||
_WriteLog ("Git upload failed: {0}" -f $_.Exception.Message) "Error"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $filePath
|
||||
}
|
||||
catch {
|
||||
_WriteLog ("New-SamyPrinterProfileJson failed: {0}" -f $_.Exception.Message) "Error"
|
||||
throw
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user