Compare commits

..

5 Commits
v1.0 ... main

Author SHA1 Message Date
a0aa0680ef Update readme - reference version 2.0 2021-09-05 19:00:10 -06:00
b968fdf93e chore(manifest): bump magicPacket to version 2.0 2021-09-05 18:50:09 -06:00
d5c3c071cd refactor(magicpacket): manual MAC address format validation
- use manual format validation for MacAddress so that ErrorAction works
correctly instead of throwing ParameterBindingError which requires
Try/Catch blocks

BREAKING CHANGES:
- remove parameter validation check for MacAddress,
validate while processing
- change all warnings to errors

Closes #1
2021-09-05 18:49:00 -06:00
f389999ca3 docs(readme): update with auto-load instructions 2021-09-05 02:13:28 -06:00
1ff4a6bf6a refactor(magicpacket): add manifest and explicit function export 2021-09-05 01:08:20 -06:00
3 changed files with 60 additions and 10 deletions

View File

@ -8,6 +8,7 @@ ## Contents
<!-- toc --> <!-- toc -->
- [Installation and verification](#installation-and-verification) - [Installation and verification](#installation-and-verification)
* [Example: Auto-load for current user](#example-auto-load-for-current-user)
- [Overview](#overview) - [Overview](#overview)
- [Broadcast considerations](#broadcast-considerations) - [Broadcast considerations](#broadcast-considerations)
- [Pipeline](#pipeline) - [Pipeline](#pipeline)
@ -18,12 +19,53 @@ ## Contents
## Installation and verification ## Installation and verification
Downloads are available Downloads are available via [my git server](https://git.asifbacchus.dev/asif/ps-cmdlet-wol/releases/latest)
via [my git server (https://git.asifbacchus.dev/asif/ps-cmdlet-wol)](https://git.asifbacchus.dev/asif/ps-cmdlet-wol) and [GitHub](https://github.com/asifbacchus/ps-cmdlet-wol/releases/latest). You may verify the cmdlet's integrity
and [GitHub (https://github.com/asifbacchus/ps-cmdlet-wol)](https://github.com/asifbacchus/ps-cmdlet-wol). You may using [CodeNotary](https://codenotary.io) via `vcn authenticate` or by dropping the downloaded script and/or manifest
verify the cmdlet's integrity using [CodeNotary](https://codenotary.io) via `vcn authenticate` or by dropping the onto their verification webpage at [https://verify.codenotary.io](https://verify.codenotary.io). Please always try to
downloaded script onto their verification webpage at [https://verify.codenotary.io](https://verify.codenotary.io). verify downloaded scripts and software regardless of the source!
Please always try to verify downloaded scripts and software regardless of the source!
If you are integrating this function with your own project or want to manually load the module as needed, then save the
module and manifest file wherever it is convenient for you. If you want to auto-load this function so it is available
automatically in any PowerShell session then you *must* extract it to a directory named **wol-magicPacket** somewhere
defined in your `PSModulePath` depending on your use-case. More information can be found directly from
Microsoft [here](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-7.1#short-description)
.
### Example: Auto-load for current user
Here's a complete example assuming I want the module automatically available for all sessions running under my user
account:
```powershell
# download version 2.0
Invoke-WebRequest -Uri https://git.asifbacchus.dev/asif/ps-cmdlet-wol/archive/v2.0.zip -OutFile "$Env:DOWNLOADS\ps-cmdlet-wol.zip"
# Get PSModulePath
# You should see a user-level modules path in the form of either:
# C:\Users\Username\Documents\WindowsPowerShell\Modules -OR-
# C:\Users\Username\Documents\PowerShell\Modules
$Env:PSModulePath
# change directory to the appropriate path from above
Set-Location "C:\Users\Username\Documents\WindowsPowerShell\Modules"
# extract files and rename directory
Expand-Archive -Path "$Env:DOWNLOADS\ps-cmdlet-wol.zip" -DestinationPath .\
Rename-Item -Path ps-cmdlet-wol -NewName wol-magicPacket
# confirm: you should see a directory named 'wol-magicPacket'
gci
# confirm: you should see the manifest and module within the wol-magicPacket folder
gci .\wol-magicPacket
```
Now, close and re-open PowerShell and the `Send-MagicPacket` function should be available:
```powershell
Get-Command Send-MagicPacket
```
## Overview ## Overview

BIN
wol-magicPacket.psd1 Normal file

Binary file not shown.

View File

@ -50,7 +50,6 @@ function Send-MagicPacket
ValueFromPipelineByPropertyName, ValueFromPipelineByPropertyName,
HelpMessage = "Please provide one or more MAC addresses. You may use a colon (:) or a hypen (-) to separate hex values." HelpMessage = "Please provide one or more MAC addresses. You may use a colon (:) or a hypen (-) to separate hex values."
)] )]
[ValidatePattern('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$')]
[String[]] [String[]]
$MacAddress, $MacAddress,
@ -83,6 +82,13 @@ function Send-MagicPacket
{ {
foreach ($addr in $MacAddress) foreach ($addr in $MacAddress)
{ {
# validate MAC address format or write error and continue
if (!($addr -match '^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$'))
{
Write-Error "Invalid MAC address: $addr"
continue
}
# convert MAC address to magic packet # convert MAC address to magic packet
try try
{ {
@ -97,7 +103,7 @@ function Send-MagicPacket
} }
catch catch
{ {
Write-Warning "Unable to process MAC address: $thisMacAddress" Write-Error "Unable to process MAC address: $thisMacAddress"
continue continue
} }
@ -112,7 +118,7 @@ function Send-MagicPacket
} }
catch catch
{ {
Write-Warning "Unable to send magic packet for '$addr'" Write-Error "Unable to send magic packet for '$addr'"
continue continue
} }
} }
@ -124,4 +130,6 @@ function Send-MagicPacket
$UdpClient.Close() $UdpClient.Close()
$UdpClient.Dispose() $UdpClient.Dispose()
} }
} }
Export-ModuleMember -Function Send-MagicPacket