PowerShell 7.x - قسمت هشتم - ماژولها
نویسنده: سیروان عفیفی
تاریخ: ۱۴۰۱/۱۱/۱۵ ۱۹:۱۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
PS /> $env:PSModulePath -Split ":" /Users/sirwanafifi/.local/share/powershell/Modules /usr/local/share/powershell/Modules /usr/local/microsoft/powershell/7/Modules
PS /> Import-Module ./PingModule.psm1
PS /> Get-Module PingModule ModuleType Version PreRelease Name ExportedCommands ---------- ------- ---------- ---- ---------------- Script 0.0 PingModule Get-PingReply
PS /> Import-Module ./PingModule.ps1 PS /> Get-Module PingModule ModuleType Version PreRelease Name ExportedCommands ---------- ------- ---------- ---- ---------------- Script 0.0 PingModule
Function Get-PingReply {
// as before
}
Function Get-PrivateFunction {
Write-Debug 'This is a private function'
}
Export-ModuleMember -Function @(
'Get-PingReply'
) $moduleSettings = @{
Path = './PingModule.psd1'
Description = 'A module to ping a remote system'
RootModule = 'PingModule.psm1'
ModuleVersion = '1.0.0'
FunctionsToExport = @(
'Get-PingReply'
)
PowerShellVersion = '5.1'
CompatiblePSEditions = @(
'Core'
'Desktop'
)
}
New-ModuleManifest @moduleSettings $moduleSettings = @{
Author = 'John Doe'
Description = 'This is a sample module'
}
New-ModuleManifest $moduleSettings New-ModuleManifest : A parameter cannot be found that matches parameter name 'Author'.
#
# Module manifest for module 'PingModule'
#
# Generated by: sirwanafifi
#
# Generated on: 01/01/2023
#
@{
# Script module or binary module file associated with this manifest.
RootModule = './PingModule.psm1'
# Version number of this module.
ModuleVersion = '1.0.0'
# Supported PSEditions
CompatiblePSEditions = 'Core', 'Desktop'
# ID used to uniquely identify this module
GUID = '3f8561fc-c004-4c8e-b2fc-4a4191504131'
# Author of this module
Author = 'sirwanafifi'
# Company or vendor of this module
CompanyName = 'Unknown'
# Copyright statement for this module
Copyright = '(c) sirwanafifi. All rights reserved.'
# Description of the functionality provided by this module
Description = 'A module to ping a remote system'
# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '5.1'
# Name of the PowerShell host required by this module
# PowerShellHostName = ''
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Get-PingReply'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = '*'
# Variables to export from this module
VariablesToExport = '*'
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = '*'
# DSC resources to export from this module
# DscResourcesToExport = @()
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
# FileList = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()
# A URL to the license for this module.
# LicenseUri = ''
# A URL to the main website for this project.
# ProjectUri = ''
# A URL to an icon representing this module.
# IconUri = ''
# ReleaseNotes of this module
# ReleaseNotes = ''
# Prerelease string of this module
# Prerelease = ''
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
# External dependent modules of this module
# ExternalModuleDependencies = @()
} # End of PSData hashtable
} # End of PrivateData hashtable
# HelpInfo URI of this module
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
} PS /> Test-ModuleManifest ./PingModule.psd1 ModuleType Version PreRelease Name ExportedCommands ---------- ------- ---------- ---- ---------------- Script 1.0.0 PingModule Get-PingReply
PS /> Register-PSRepository -Name 'PSLocal' ` >> -SourceLocation "$(Resolve-Path $RepoPath)" ` >> -PublishLocation "$(Resolve-Path $RepoPath)" ` >> -InstallationPolicy 'Trusted'
ProjectRoot
| -- PingModule
| -- PingModule.psd1
| -- PingModule.psm1 PS /> Publish-Module -Path ./PingModule/ -Repository PSLocal
PS /> Find-Module -Name PingModule -Repository PSLocal Version Name Repository Description ------- ---- ---------- ----------- 0.0.1 PingModule PSLocal Get-PingReply is a.
PS /> Install-Module -Name PingModule -Repository PSLocal -Scope CurrentUser
ProjectRoot
| -- PingModule
| -- 1.0.0
| -- PingModule.psd1
| -- PingModule.psm1
| -- 1.0.1
| -- PingModule.psd1
| -- PingModule.psm1 PS /> Publish-Module -Path ./PingModule/1.0.0 -Repository PSLocal PS /> Publish-Module -Path ./PingModule/1.0.1 -Repository PSLocal
PS /> Install-Module -Name PingModule -Repository PSLocal -Scope CurrentUser PS /> Get-InstalledModule -Name PingModule Version Name Repository Description ------- ---- ---------- ----------- 1.0.1 PingModule PSLocal Get-PingReply is a.
ProjectRoot
| -- PingModule
| -- 1.0.0
| -- Public/
| -- Private/
| -- PingModule.psd1
| -- PingModule.psm1 $ScriptList = Get-ChildItem -Path $PSScriptRoot/Public/*.ps1 -Filter *.ps1
foreach ($Script in $ScriptList) {
. $Script.FullName
}
$ScriptList = Get-ChildItem -Path $PSScriptRoot/Private/*.ps1 -Filter *.ps1
foreach ($Script in $ScriptList) {
. $Script.FullName
} PS /> Set-PDFSingature -PdfToSign "./sample_invoice.pdf" -SignatureImage "./sample_signature.jpg"
ProjectRoot
| -- SignPdf
| -- 1.0.0
| -- Public/
| -- dependencies/
| -- BouncyCastle.Crypto.dll
| -- System.Drawing.Common.dll
| -- Microsoft.Win32.SystemEvents.dll
| -- iTextSharp.LGPLv2.Core.dll
| -- Set-PDFSingature.ps1
| -- SignPdf.psd1
| -- SignPdf.psm1 $ScriptList = Get-ChildItem -Path $PSScriptRoot/Public/*.ps1 -Filter *.ps1
foreach ($Script in $ScriptList) {
. $Script.FullName
}
Export-ModuleMember -Function Set-PDFSingature using namespace iTextSharp.text
using namespace iTextSharp.text.pdf
using namespace System.IO
Function Set-PDFSingature {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateScript({
if (Test-Path ([Path]::Join($(Get-Location), $_))) {
return $true
}
else {
throw "Signature image not found"
}
if ($_.EndsWith('.pdf')) {
return $true
}
else {
throw "File extension must be .pdf"
}
})]
[string]$PdfToSign,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateScript({
if (Test-Path ([Path]::Join($(Get-Location), $_))) {
return $true
}
else {
throw "Signature image not found"
}
if ($_.EndsWith('.png') -or $_.EndsWith('.jpg')) {
return $true
}
else {
throw "File extension must be .png or .jpg"
}
})]
[string]$SignatureImage,
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[int]$XPos = 130,
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[int]$YPos = 50
)
Try {
Add-Type -Path "$PSScriptRoot/dependencies/*.dll"
$pdf = [PdfReader]::new("$(Get-Location)/$PdfToSign")
$fs = [FileStream]::new("$(Get-Location)/$PdfToSign-signed.pdf",
[FileMode]::Create)
$stamper = [PdfStamper]::new($pdf, $fs)
$stamper.AcroFields.AddSubstitutionFont([BaseFont]::CreateFont())
$content = $stamper.GetOverContent(1)
$width = $pdf.GetPageSize(1).Width
$image = [Image]::GetInstance("$(Get-Location)/$SignatureImage")
$image.SetAbsolutePosition($width - $XPos, $YPos)
$image.ScaleAbsolute(100, 30)
$content.AddImage($image)
$stamper.Close()
$pdf.Close()
$fs.Dispose()
}
Catch {
Write-Host "Error: $($_.Exception.Message)"
}
} PS /> Publish-Module -Path ./SignPdf/1.0.0 -Repository PSLocal
PS /> Install-Module -Name SignPdf -Repository PSLocal -Scope CurrentUser
PS /> Set-PDFSingature -PdfToSign "./sample_invoice.pdf" -SignatureImage "./sample_signature.jpg"
کدهای ماژول را میتوانید از اینجا دانلود کنید.