diff --git a/.github/linters/.jscpd.json b/.github/linters/.jscpd.json new file mode 100644 index 0000000..e96dc21 --- /dev/null +++ b/.github/linters/.jscpd.json @@ -0,0 +1,6 @@ +{ + "ignore": [ + "**/.github/**", + "**/PSWinBGP/**" + ] +} diff --git a/.github/linters/.markdown-lint.yml b/.github/linters/.markdown-lint.yml index e8b1717..b9ecdfa 100644 --- a/.github/linters/.markdown-lint.yml +++ b/.github/linters/.markdown-lint.yml @@ -8,9 +8,18 @@ ############### # Rules by id # ############### - +MD004: false # Unordered list style +MD007: + indent: 2 # Unordered list indentation +MD013: + line_length: 808 # Line length +MD026: + punctuation: ".,;:!。,;:" # List of not allowed +MD029: false # Ordered list item prefix +MD033: false # Allow inline HTML +MD036: false # Emphasis used instead of a heading ################# # Rules by tags # ################# - +blank_lines: false # Error on blank lines diff --git a/.github/linters/.powershell-psscriptanalyzer.psd1 b/.github/linters/.powershell-psscriptanalyzer.psd1 index 8d3d2cc..c74b2e6 100644 --- a/.github/linters/.powershell-psscriptanalyzer.psd1 +++ b/.github/linters/.powershell-psscriptanalyzer.psd1 @@ -7,7 +7,10 @@ # 'Warning' #) #IncludeDefaultRules=${true} - #ExcludeRules = @() + ExcludeRules = @( + 'PSMissingModuleManifestField', + 'PSReviewUnusedParameter' + ) #IncludeRules = @( # 'PSAvoidUsingWriteHost', # 'MyCustomRuleName' diff --git a/.github/workflows/Process-PSModule.yml b/.github/workflows/Process-PSModule.yml index 43ca948..a01d3a1 100644 --- a/.github/workflows/Process-PSModule.yml +++ b/.github/workflows/Process-PSModule.yml @@ -25,4 +25,6 @@ permissions: jobs: Process-PSModule: uses: PSModule/Process-PSModule/.github/workflows/workflow.yml@v1 - secrets: inherit + #secrets: inherit + secrets: + APIKey: ${{ secrets.APIKEY }} diff --git a/LICENSE b/LICENSE index 92f4384..6b34b1a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 PSModule +Copyright (c) 2024 Webalex System Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/PSWinBGP.psd1 b/src/PSWinBGP.psd1 index d2e888e..289ac9c 100644 --- a/src/PSWinBGP.psd1 +++ b/src/PSWinBGP.psd1 @@ -3,127 +3,83 @@ # @{ + # Script module or binary module file associated with this manifest. + RootModule = 'PSWinBGP.psm1' -# Script module or binary module file associated with this manifest. -RootModule = 'PSWinBGP.psm1' + # Version number of this module. + ModuleVersion = '1.0.0' -# Version number of this module. -ModuleVersion = '0.1.0' + # Supported PSEditions + CompatiblePSEditions = @( + 'PSEdition_Desktop', + 'PSEdition_Core', + 'Windows', + 'Linux', + 'MacOS' + ) -# Supported PSEditions -CompatiblePSEditions = @('PSEdition_Desktop','PSEdition_Core','Windows','Linux','MacOS') + # ID used to uniquely identify this module + GUID = 'a7c4be44-95a4-45d3-bcb3-c9f3deb1d7d4' -# ID used to uniquely identify this module -GUID = 'a7c4be44-95a4-45d3-bcb3-c9f3deb1d7d4' + # Author of this module + Author = 'Alexandre JARDON' -# Author of this module -Author = 'Alexandre JARDON' + # Company or vendor of this module + CompanyName = 'Webalex System' -# Company or vendor of this module -CompanyName = 'Webalex System' + # Copyright statement for this module + # Copyright = '(c) author. All rights reserved.' -# Copyright statement for this module -Copyright = '(c) webalexeu. All rights reserved.' + # Description of the functionality provided by this module + Description = 'WinBGP Remote Management' -# Description of the functionality provided by this module -Description = 'WinBGP Remote Management' + # Minimum version of the PowerShell engine required by this module + PowerShellVersion = '5.1' -# Minimum version of the PowerShell engine required by this module -PowerShellVersion = '5.1' + # 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-WinBGPRoute', + 'Start-WinBGPRoute', + 'Stop-WinBGPRoute', + 'Start-WinBGPRouteMaintenance', + 'Stop-WinBGPRouteMaintenance' + ) -# Name of the PowerShell host required by this module -# PowerShellHostName = '' + # 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 = @('winbgp', 'bgp') -# Minimum version of the PowerShell host required by this module -# PowerShellHostVersion = '' + # A URL to the license for this module. + LicenseUri = 'https://github.com/webalexeu/pswinbgp/blob/master/LICENSE' -# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. -# DotNetFrameworkVersion = '' + # A URL to the main website for this project. + ProjectUri = 'https://github.com/webalexeu/pswinbgp' -# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. -# ClrVersion = '' + # A URL to an icon representing this module. + # IconUri = '' -# Processor architecture (None, X86, Amd64) required by this module -# ProcessorArchitecture = '' + # ReleaseNotes of this module + # ReleaseNotes = '' -# Modules that must be imported into the global environment prior to importing this module -# RequiredModules = @() + # Prerelease string of this module + # Prerelease = '' -# Assemblies that must be loaded prior to importing this module -# RequiredAssemblies = @() + # Flag to indicate whether the module requires explicit user acceptance for install/update/save + RequireLicenseAcceptance = $false -# Script files (.ps1) that are run in the caller's environment prior to importing this module. -# ScriptsToProcess = @() + # External dependent modules of this module + # ExternalModuleDependencies = @() -# Type files (.ps1xml) to be loaded when importing this module -# TypesToProcess = @() + } # End of PSData hashtable + } # End of PrivateData hashtable -# 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-WinBGPRoute','Start-WinBGPRoute','Stop-WinBGPRoute','Start-WinBGPRouteMaintenance','Stop-WinBGPRouteMaintenance') - -# 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 = @('winbgp','bgp') - - # A URL to the license for this module. - LicenseUri = 'https://github.com/webalexeu/pswinbgp/blob/master/LICENSE' - - # A URL to the main website for this project. - ProjectUri = 'https://github.com/webalexeu/pswinbgp' - - # 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 = '' + # HelpInfo URI of this module + # HelpInfoURI = '' + # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. + # DefaultCommandPrefix = '' } - - diff --git a/src/PSWinBGP.psm1 b/src/PSWinBGP.psm1 index a762ce0..ba65728 100644 --- a/src/PSWinBGP.psm1 +++ b/src/PSWinBGP.psm1 @@ -1,5 +1,5 @@ # Dot source public/private functions -$public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Public/*.ps1') -Recurse -ErrorAction Stop) +$public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Public/*.ps1') -Recurse -ErrorAction Stop) $private = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Private/*.ps1') -Recurse -ErrorAction Stop) foreach ($import in @($public + $private)) { try { @@ -13,7 +13,13 @@ Export-ModuleMember -Function $public.Basename # Route Argument Completer $ArgumentCompleterBlock = { - param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) + param( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) # Dynamically generate routes array if ($FakeBoundParameters.ComputerName) { @@ -21,16 +27,25 @@ $ArgumentCompleterBlock = { } else { [Array] $routes = (Get-WinBGPRoute) } - # Return routes as arguments (Intellisense) + # Return routes as arguments (IntelliSense) $routes | ForEach-Object { - New-Object -Type System.Management.Automation.CompletionResult -ArgumentList $_.Name, - #"$($_.ComputerName)_$($_.Name)", - "$(if ($_.ComputerName){"ComputerName: '$($_.ComputerName)' - RouteName: '$($_.Name)'"}else{$_.Name})", - "ParameterValue", - "$(if ($_.ComputerName){"ComputerName: '$($_.ComputerName)' - "})Network: '$($_.Network)' - Status: '$($_.Status)'" + New-Object -Type System.Management.Automation.CompletionResult -ArgumentList + $_.Name, + #"$($_.ComputerName)_$($_.Name)", + "$(if ($_.ComputerName){"ComputerName: '$($_.ComputerName)' - RouteName: '$($_.Name)'"}else{$_.Name})", + "ParameterValue", + "$(if ($_.ComputerName){"ComputerName: '$($_.ComputerName)' - "})Network: '$($_.Network)' - Status: '$($_.Status)'" } + # To review (to avoid syntax error) + $null = $commandName + $null = $parameterName + $null = $wordToComplete + $null = $commandAst } -Register-ArgumentCompleter -CommandName Start-WinBGPRoute,Stop-WinBGPRoute,Start-WinBGPRouteMaintenance,Stop-WinBGPRouteMaintenance -ParameterName RouteName -ScriptBlock $ArgumentCompleterBlock +Register-ArgumentCompleter ` + -CommandName Start-WinBGPRoute, Stop-WinBGPRoute, Start-WinBGPRouteMaintenance, Stop-WinBGPRouteMaintenance ` + -ParameterName RouteName ` + -ScriptBlock $ArgumentCompleterBlock # Declare a module-level variable $PSWinBGP = [ordered]@{ diff --git a/src/private/Invoke-PSWinBGP.ps1 b/src/private/Invoke-PSWinBGP.ps1 index aae560c..329c494 100644 --- a/src/private/Invoke-PSWinBGP.ps1 +++ b/src/private/Invoke-PSWinBGP.ps1 @@ -11,108 +11,110 @@ function Invoke-PSWinBGP() { # Get WinBGP status (Display WinBGP routes status) #> - Param( - [Parameter(Mandatory=$true)] + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] [String[]]$ComputerName, - [Parameter(ParameterSetName='call',Mandatory=$true)] + [Parameter(ParameterSetName = 'call', Mandatory = $true)] [String]$Call, # Call to perform - [Parameter(Mandatory=$false)] + [Parameter(Mandatory = $false)] [String]$RouteName # Select route to control ) if ($ComputerName -eq 'local') { switch ($call) { - 'routes' { Invoke-Command {WinBGP} } + 'routes' { Invoke-Command { WinBGP } } 'startroute' { Send-WinBGPRouteControl -RouteName $RouteName -Control 'start' } - 'stoproute' { Send-WinBGPRouteControl -RouteName $RouteName -Control 'stop'} + 'stoproute' { Send-WinBGPRouteControl -RouteName $RouteName -Control 'stop' } 'startmaintenance' { Send-WinBGPRouteControl -RouteName $RouteName -Control 'start' -Action 'maintenance' } 'stopmaintenance' { Send-WinBGPRouteControl -RouteName $RouteName -Control 'stop' -Action 'maintenance' } } } else { if ($ComputerName -eq 'localhost') { - [Int]$Port=$Script:PSWinBGP.LocalhostApiPort - [String]$Protocol=$Script:PSWinBGP.LocalhostApiProtocol - [Int]$Timeout=$Script:PSWinBGP.LocalhostApiTimeout + [Int]$Port = $Script:PSWinBGP.LocalhostApiPort + [String]$Protocol = $Script:PSWinBGP.LocalhostApiProtocol + [Int]$Timeout = $Script:PSWinBGP.LocalhostApiTimeout } else { - [Int]$Port=$Script:PSWinBGP.ApiPort - [String]$Protocol=$Script:PSWinBGP.ApiProtocol - [Int]$Timeout=$Script:PSWinBGP.ApiTimeout + [Int]$Port = $Script:PSWinBGP.ApiPort + [String]$Protocol = $Script:PSWinBGP.ApiProtocol + [Int]$Timeout = $Script:PSWinBGP.ApiTimeout } # Initialize output variable - $Output=@() - $ErrorOutput=@() + $Output = @() + $ErrorOutput = @() # Initialize error variable - $ErrorCount=0 + $ErrorCount = 0 - ForEach ($Computer in $ComputerName) { + foreach ($Computer in $ComputerName) { # Initialize output variable $ApiOutput = [PSCustomObject]@{} $ErrorOut = [PSCustomObject]@{} - $ApiDefaultRequestURL="$($Protocol)://$($Computer):$($Port)/api" + $ApiDefaultRequestURL = "$($Protocol)://$($Computer):$($Port)/api" $params = @{} $params.add('UseBasicParsing', $true) $params.add('TimeoutSec', $Timeout) - $params.add('ContentType','application/json') + $params.add('ContentType', 'application/json') $params.add('UseDefaultCredentials', $true) # Get if ($call -eq 'routes') { - $ApiRequestURL="$ApiDefaultRequestURL/routes" + $ApiRequestURL = "$ApiDefaultRequestURL/routes" } # Post if (($call -eq 'startmaintenance') -or ($call -eq 'stopmaintenance') -or ($call -eq 'startroute') -or ($call -eq 'stoproute')) { - $ApiRequestMethod='Post' + $ApiRequestMethod = 'Post' if ($call -eq 'startmaintenance') { - $ApiRequestURL="$ApiDefaultRequestURL/startmaintenance?routename=$RouteName" + $ApiRequestURL = "$ApiDefaultRequestURL/startmaintenance?routename=$RouteName" } if ($call -eq 'stopmaintenance') { - $ApiRequestURL="$ApiDefaultRequestURL/stopmaintenance?routename=$RouteName" + $ApiRequestURL = "$ApiDefaultRequestURL/stopmaintenance?routename=$RouteName" } if ($call -eq 'startroute') { - $ApiRequestURL="$ApiDefaultRequestURL/startroute?routename=$RouteName" + $ApiRequestURL = "$ApiDefaultRequestURL/startroute?routename=$RouteName" } if ($call -eq 'stoproute') { - $ApiRequestURL="$ApiDefaultRequestURL/stoproute?routename=$RouteName" + $ApiRequestURL = "$ApiDefaultRequestURL/stoproute?routename=$RouteName" } } # Test if target is reachable if ($PSVersionTable.PSVersion.Major -ge 7) { - $ConnectivityTest=(Test-Connection -TcpPort $Port -TimeoutSeconds $Timeout -TargetName $Computer -Quiet) + $ConnectivityTest = (Test-Connection -TcpPort $Port -TimeoutSeconds $Timeout -TargetName $Computer -Quiet) $params.add('SkipHttpErrorCheck', $true) $params.add('StatusCodeVariable', 'StatusCode') } else { if ($ComputerName -eq 'localhost') { # Bypass connectivity test when localhost (For speed performance) - $ConnectivityTest=$true + $ConnectivityTest = $true } else { - $ConnectivityTest=(Test-NetConnection -ComputerName $computer -Port $port).TcpTestSucceeded + $ConnectivityTest = (Test-NetConnection -ComputerName $computer -Port $port).TcpTestSucceeded } } if ($ConnectivityTest) { - $params.add('uri',$ApiRequestURL) + $params.add('uri', $ApiRequestURL) if ($ApiRequestMethod) { - $params.add('Method',$ApiRequestMethod) + $params.add('Method', $ApiRequestMethod) } # Perform Rest API Call if ($PSVersionTable.PSVersion.Major -ge 7) { - $RestApiCall=Invoke-RestMethod @params + $RestApiCall = Invoke-RestMethod @params } else { # Try/catch because PS5 don't support status code and skip http error check - Try { - $RestApiCall=Invoke-RestMethod @params - } Catch { + try { + $RestApiCall = Invoke-RestMethod @params + } catch { $ErrorOut | Add-member -MemberType NoteProperty -Name 'Result' -Value "API call error: $($_)" $ErrorCount++ } } if ($RestApiCall) { - $ApiOutput=$RestApiCall + $ApiOutput = $RestApiCall $ErrorOut | Add-member -MemberType NoteProperty -Name 'Result' -Value 'API connection OK' } else { if ($StatusCode) { - $ErrorOut | Add-member -MemberType NoteProperty -Name 'Result' -Value "API return code: $([System.Net.HttpStatusCode]$StatusCode) ($StatusCode)" + $Value = "API return code: $([System.Net.HttpStatusCode]$StatusCode) ($StatusCode)" + $ErrorOut | Add-Member -MemberType NoteProperty -Name 'Result' -Value $Value $ErrorCount++ } else { if ($PSVersionTable.PSVersion.Major -ge 7) { @@ -129,12 +131,12 @@ function Invoke-PSWinBGP() { if ($Computer -ne 'localhost') { $ApiOutput | Add-member -MemberType NoteProperty -Name 'ComputerName' -Value "$Computer" } - $Output+=$ApiOutput + $Output += $ApiOutput # Add ComputerName variable to output (except for localhost) if ($Computer -ne 'localhost') { $ErrorOut | Add-member -MemberType NoteProperty -Name 'ComputerName' -Value "$Computer" } - $ErrorOutput+=$ErrorOut + $ErrorOutput += $ErrorOut } # If there is connection error, just return connection table @@ -146,67 +148,3 @@ function Invoke-PSWinBGP() { } } } - -# IPC communication with WinBGP-Engine -Function Send-WinBGPPipeMessage () { - Param( - [Parameter(Mandatory=$true)] - [String]$PipeName, # Named pipe name - [Parameter(Mandatory=$true)] - [String]$Message # Message string - ) - $PipeDir = [System.IO.Pipes.PipeDirection]::Out - $PipeOpt = [System.IO.Pipes.PipeOptions]::Asynchronous - - $pipe = $null # Named pipe stream - $sw = $null # Stream Writer - try { - $pipe = new-object System.IO.Pipes.NamedPipeClientStream(".", $PipeName, $PipeDir, $PipeOpt) - $sw = new-object System.IO.StreamWriter($pipe) - $pipe.Connect(1000) - if (!$pipe.IsConnected) { - throw "Failed to connect client to pipe $pipeName" - } - $sw.AutoFlush = $true - $sw.WriteLine($Message) - } catch { - Write-Log "Error sending pipe $pipeName message: $_" -Level Error - } finally { - if ($sw) { - $sw.Dispose() # Release resources - $sw = $null # Force the PowerShell garbage collector to delete the .net object - } - if ($pipe) { - $pipe.Dispose() # Release resources - $pipe = $null # Force the PowerShell garbage collector to delete the .net object - } - } - } - -function Send-WinBGPRouteControl { - param ( - [Parameter(Mandatory=$false)] - [String]$Action='route', # Action - [Parameter(Mandatory=$true)] - [String]$RouteName, # Route Name - [Parameter(Mandatory=$true)] - [String]$Control # Control - ) - $PipeStatus=$null - # Performing Action - try { - # Temporary - $pipeName='Service_WinBGP' - $Message="$($Action) $($RouteName) $($Control)" - Send-WinBGPPipeMessage -PipeName $pipeName -Message $Message - } - catch { - $PipeStatus=($_).ToString() - } - if ($PipeStatus -like "*Pipe hasn't been connected yet*") { - return "WinBGP not ready" - } else { - # TO BE IMPROVED to get status - return "Success" - } -} diff --git a/src/private/Send-WinBGPPipeMessage.ps1 b/src/private/Send-WinBGPPipeMessage.ps1 new file mode 100644 index 0000000..06416eb --- /dev/null +++ b/src/private/Send-WinBGPPipeMessage.ps1 @@ -0,0 +1,48 @@ +function Send-WinBGPPipeMessage() { + <# + .SYNOPSIS + Send-WinBGPPipeMessage + .DESCRIPTION + Send-WinBGPPipeMessage + .PARAMETER PipeName + PipeName + .PARAMETER Message + Message + .EXAMPLE + Send-WinBGPPipeMessage -PipeName $pipeName -Message $Message + #> + + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [String]$PipeName, + [Parameter(Mandatory = $true)] + [String]$Message + ) + $PipeDir = [System.IO.Pipes.PipeDirection]::Out + $PipeOpt = [System.IO.Pipes.PipeOptions]::Asynchronous + + $pipe = $null # Named pipe stream + $sw = $null # Stream Writer + try { + $pipe = new-object System.IO.Pipes.NamedPipeClientStream(".", $PipeName, $PipeDir, $PipeOpt) + $sw = new-object System.IO.StreamWriter($pipe) + $pipe.Connect(1000) + if (!$pipe.IsConnected) { + throw "Failed to connect client to pipe $pipeName" + } + $sw.AutoFlush = $true + $sw.WriteLine($Message) + } catch { + Write-Log "Error sending pipe $pipeName message: $_" -Level Error + } finally { + if ($sw) { + $sw.Dispose() # Release resources + $sw = $null # Force the PowerShell garbage collector to delete the .net object + } + if ($pipe) { + $pipe.Dispose() # Release resources + $pipe = $null # Force the PowerShell garbage collector to delete the .net object + } + } +} diff --git a/src/private/Send-WinBGPRouteControl.ps1 b/src/private/Send-WinBGPRouteControl.ps1 new file mode 100644 index 0000000..9dc8b62 --- /dev/null +++ b/src/private/Send-WinBGPRouteControl.ps1 @@ -0,0 +1,42 @@ +function Send-WinBGPRouteControl() { + <# + .SYNOPSIS + Send-WinBGPRouteControl + .DESCRIPTION + Send-WinBGPRouteControl + .PARAMETER RouteName + RouteName + .PARAMETER Control + Control + .PARAMETER Action + Action + .EXAMPLE + Send-WinBGPRouteControl -RouteName $RouteName -Control 'start' -Action 'maintenance' + #> + + [CmdletBinding()] + param ( + [Parameter(Mandatory = $false)] + [String]$Action = 'route', + [Parameter(Mandatory = $true)] + [String]$RouteName, + [Parameter(Mandatory = $true)] + [String]$Control + ) + $PipeStatus = $null + # Performing Action + try { + # Temporary + $pipeName = 'Service_WinBGP' + $Message = "$($Action) $($RouteName) $($Control)" + Send-WinBGPPipeMessage -PipeName $pipeName -Message $Message + } catch { + $PipeStatus = ($_).ToString() + } + if ($PipeStatus -like "*Pipe hasn't been connected yet*") { + Write-Output "WinBGP not ready" + } else { + # TO BE IMPROVED to get status + Write-Output "Success" + } +} diff --git a/src/public/Get-WinBGPRoute.ps1 b/src/public/Get-WinBGPRoute.ps1 index 0128cdd..43e399d 100644 --- a/src/public/Get-WinBGPRoute.ps1 +++ b/src/public/Get-WinBGPRoute.ps1 @@ -1,7 +1,7 @@ function Get-WinBGPRoute() { <# .SYNOPSIS - Get WinBGP Route + WinBGP Remote Management - Get WinBGP Route .DESCRIPTION This function retrieve WinBGP Routes .PARAMETER ComputerName @@ -10,9 +10,10 @@ function Get-WinBGPRoute() { Get-WinBGPRoute -ComputerName machine1,machine2 #> - Param( - [Parameter(Mandatory=$false)] - [String[]]$ComputerName='local' + [CmdletBinding()] + param( + [Parameter(Mandatory = $false)] + [String[]]$ComputerName = 'local' ) Invoke-PSWinBGP -ComputerName $ComputerName -Call 'routes' diff --git a/src/public/Set-PSWinBGPConfig.ps1 b/src/public/Set-PSWinBGPConfig.ps1 index 0198eea..4d44ba7 100644 --- a/src/public/Set-PSWinBGPConfig.ps1 +++ b/src/public/Set-PSWinBGPConfig.ps1 @@ -7,25 +7,24 @@ function Set-PSWinBGPConfig { Set PSWinBBGP module configuration, and $PSWinBBGP module variable. .PARAMETER ApiPort - Api Port (Default: 8888) + API Port (Default: 8888) .PARAMETER ApiProtocol - Api Protocol (Default: https) + API Protocol (Default: HTTPS) .PARAMETER ApiTimeout - Api Timeout (Default: 10s) + API Timeout (Default: 10s) .PARAMETER LocalhostApiPort - Localhost Api Port (Default: 8888) + Localhost API Port (Default: 8888) .PARAMETER LocalhostApiProtocol - Localhost Api Protocol (Default: http) + Localhost API Protocol (Default: HTTP) .PARAMETER LocalhostApiTimeout - Localhost Api Timeout (Default: 5s) - + Localhost API Timeout (Default: 5s) #> - [CmdletBinding(SupportsShouldProcess=$true)] + [CmdletBinding(SupportsShouldProcess = $true)] param( [Int]$ApiPort, [string]$ApiProtocol, @@ -35,15 +34,14 @@ function Set-PSWinBGPConfig { [Int]$LocalhostApiTimeout ) - if ($pscmdlet.ShouldProcess('$Script:PSWinBGP','Set config')) { - Switch ($PSBoundParameters.Keys) - { - 'ApiPort' { $Script:PSWinBGP.ApiPort = $ApiPort } - 'ApiProtocol' { $Script:PSWinBGP.ApiProtocol = $ApiProtocol } - 'ApiTimeout' { $Script:PSWinBGP.ApiTimeout = $ApiTimeout } - 'LocalhostApiPort' { $Script:PSWinBGP.LocalhostApiPort = $LocalhostApiPort } - 'LocalhostApiProtocol' { $Script:PSWinBGP.LocalhostApiProtocol = $LocalhostApiProtocol } - 'LocalhostApiTimeout' { $Script:PSWinBGP.LocalhostApiTimeout = $LocalhostApiTimeout } + if ($pscmdlet.ShouldProcess('$Script:PSWinBGP', 'Set config')) { + switch ($PSBoundParameters.Keys) { + 'ApiPort' { $Script:PSWinBGP.ApiPort = $ApiPort } + 'ApiProtocol' { $Script:PSWinBGP.ApiProtocol = $ApiProtocol } + 'ApiTimeout' { $Script:PSWinBGP.ApiTimeout = $ApiTimeout } + 'LocalhostApiPort' { $Script:PSWinBGP.LocalhostApiPort = $LocalhostApiPort } + 'LocalhostApiProtocol' { $Script:PSWinBGP.LocalhostApiProtocol = $LocalhostApiProtocol } + 'LocalhostApiTimeout' { $Script:PSWinBGP.LocalhostApiTimeout = $LocalhostApiTimeout } } } } diff --git a/src/public/Start-WinBGPRoute.ps1 b/src/public/Start-WinBGPRoute.ps1 index d0b9738..e848a89 100644 --- a/src/public/Start-WinBGPRoute.ps1 +++ b/src/public/Start-WinBGPRoute.ps1 @@ -1,25 +1,26 @@ function Start-WinBGPRoute() { <# .SYNOPSIS - WinBGP Remote Management - Route + WinBGP Remote Management - Start Route .DESCRIPTION - WinBGP Remote Management + This function perform Start Route .PARAMETER ComputerName Single or multiple ComputerName (Default: localhost) + .PARAMETER RouteName + RouteName (Currently supporting only one route, IntelliSense availalble) .EXAMPLE - Get-WinBGPRoute -ComputerName machine1,machine2 - # Get WinBGP route + Start-WinBGPRoute -ComputerName machine1,machine2 -RouteName route1.contoso.com #> - [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')] - Param( - [Parameter(Mandatory=$false)] - [String[]]$ComputerName='local', - [Parameter(Mandatory=$true)] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] + param( + [Parameter(Mandatory = $false)] + [String[]]$ComputerName = 'local', + [Parameter(Mandatory = $true)] [String]$RouteName ) # If action is confirmed - if ($pscmdlet.ShouldProcess($RouteName,'Start route')) { + if ($pscmdlet.ShouldProcess($RouteName, 'Start route')) { Invoke-PSWinBGP -ComputerName $ComputerName -call 'startroute' -RouteName $RouteName } } diff --git a/src/public/Start-WinBGPRouteMaintenance.ps1 b/src/public/Start-WinBGPRouteMaintenance.ps1 index ae2a87a..e1d3324 100644 --- a/src/public/Start-WinBGPRouteMaintenance.ps1 +++ b/src/public/Start-WinBGPRouteMaintenance.ps1 @@ -1,25 +1,26 @@ function Start-WinBGPRouteMaintenance() { <# .SYNOPSIS - WinBGP Remote Management - Route + WinBGP Remote Management - Start Route Maintenance .DESCRIPTION - WinBGP Remote Management + This function perform Start Route Maintenance .PARAMETER ComputerName Single or multiple ComputerName (Default: localhost) + .PARAMETER RouteName + RouteName (Currently supporting only one route, IntelliSense availalble) .EXAMPLE - Get-WinBGPRoute -ComputerName machine1,machine2 - # Get WinBGP route + Start-WinBGPRouteMaintenance -ComputerName machine1,machine2 -RouteName route1.contoso.com #> - [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')] - Param( - [Parameter(Mandatory=$false)] - [String[]]$ComputerName='local', - [Parameter(Mandatory=$true)] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] + param( + [Parameter(Mandatory = $false)] + [String[]]$ComputerName = 'local', + [Parameter(Mandatory = $true)] [String]$RouteName ) - if ($pscmdlet.ShouldProcess($RouteName,'Start route maintenance')) { + if ($pscmdlet.ShouldProcess($RouteName, 'Start route maintenance')) { Invoke-PSWinBGP -ComputerName $ComputerName -call 'startmaintenance' -routename $RouteName } } diff --git a/src/public/Stop-WinBGPRoute.ps1 b/src/public/Stop-WinBGPRoute.ps1 index 4383487..28f8857 100644 --- a/src/public/Stop-WinBGPRoute.ps1 +++ b/src/public/Stop-WinBGPRoute.ps1 @@ -1,24 +1,25 @@ function Stop-WinBGPRoute() { <# .SYNOPSIS - WinBGP Remote Management - Route + WinBGP Remote Management - Stop Route .DESCRIPTION - WinBGP Remote Management + This function perform Stop Route .PARAMETER ComputerName Single or multiple ComputerName (Default: localhost) + .PARAMETER RouteName + RouteName (Currently supporting only one route, IntelliSense availalble) .EXAMPLE - Get-WinBGPRoute -ComputerName machine1,machine2 - # Get WinBGP route + Stop-WinBGPRoute -ComputerName machine1,machine2 -RouteName route1.contoso.com #> - [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')] - Param( - [Parameter(Mandatory=$false)] - [String[]]$ComputerName='local', - [Parameter(Mandatory=$true)] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] + param( + [Parameter(Mandatory = $false)] + [String[]]$ComputerName = 'local', + [Parameter(Mandatory = $true)] [String]$RouteName ) - if ($pscmdlet.ShouldProcess($RouteName,'Stop route')) { + if ($pscmdlet.ShouldProcess($RouteName, 'Stop route')) { Invoke-PSWinBGP -ComputerName $ComputerName -call 'stoproute' -routename $RouteName } } diff --git a/src/public/Stop-WinBGPRouteMaintenance.ps1 b/src/public/Stop-WinBGPRouteMaintenance.ps1 index a853335..5add826 100644 --- a/src/public/Stop-WinBGPRouteMaintenance.ps1 +++ b/src/public/Stop-WinBGPRouteMaintenance.ps1 @@ -1,24 +1,25 @@ function Stop-WinBGPRouteMaintenance() { <# .SYNOPSIS - WinBGP Remote Management - Route + WinBGP Remote Management - Stop Route Maintenance .DESCRIPTION - WinBGP Remote Management + This function perform Stop Route Maintenance .PARAMETER ComputerName Single or multiple ComputerName (Default: localhost) + .PARAMETER RouteName + RouteName (Currently supporting only one route, IntelliSense availalble) .EXAMPLE - Get-WinBGPRoute -ComputerName machine1,machine2 - # Get WinBGP route + Stop-WinBGPRouteMaintenance -ComputerName machine1,machine2 -RouteName route1.contoso.com #> - [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')] - Param( - [Parameter(Mandatory=$false)] - [String[]]$ComputerName='local', - [Parameter(Mandatory=$true)] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] + param( + [Parameter(Mandatory = $false)] + [String[]]$ComputerName = 'local', + [Parameter(Mandatory = $true)] [String]$RouteName ) - if ($pscmdlet.ShouldProcess($RouteName,'Stop route maintenance')) { + if ($pscmdlet.ShouldProcess($RouteName, 'Stop route maintenance')) { Invoke-PSWinBGP -ComputerName $ComputerName -call 'stopmaintenance' -routename $RouteName } } diff --git a/tests/PSWinBGP.Tests.ps1 b/tests/PSWinBGP.Tests.ps1 new file mode 100644 index 0000000..4134fd4 --- /dev/null +++ b/tests/PSWinBGP.Tests.ps1 @@ -0,0 +1,11 @@ +[CmdletBinding()] +Param( + # Path to the module to test. + [Parameter()] + [string] $Path +) + +Write-Verbose "Path to the module: [$Path]" -Verbose + +Describe 'PSWinBGP' { +}