PowerShell 6 Core Support (#35)
## About This pull request reflects all changes done in the `linuxsupport` branch. ## Content - Enable PowerShell 6 Core support - Use PFX Certificate for encryption ( fixes #32 ) - Updates CI / CD pipeline ( fixes #31 ) - uses portable libressl ( fixes #34 ) - adds `-PassThru` switch for returning current `VIServer` session in `Connect-To` ( fixes #34 ) - adds git lfs for embedded libressl files - restructured internal functions into `Private` dir - added certificate related functions - adds travis build pipeline for tests
This commit is contained in:
61
src/Private/Get-DefaultCredentialStorePath.ps1
Normal file
61
src/Private/Get-DefaultCredentialStorePath.ps1
Normal file
@ -0,0 +1,61 @@
|
||||
function Get-DefaultCredentialStorePath {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Returns the default CredentialStore path based on the current OS.
|
||||
|
||||
.DESCRIPTION
|
||||
This is a low level helper function.
|
||||
|
||||
.INPUTS
|
||||
[None]
|
||||
|
||||
.OUTPUTS
|
||||
[string]
|
||||
|
||||
.EXAMPLE
|
||||
$Path = Get-DefaultCredentialStorePath
|
||||
|
||||
.NOTES
|
||||
File Name : Get-DefaultCredentialStorePath.ps1
|
||||
Author : Marco Blessing - marco.blessing@googlemail.com
|
||||
Requires :
|
||||
|
||||
.LINK
|
||||
https://github.com/OCram85/PSCredentialStore
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
[OutputType([string])]
|
||||
param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[switch]$Shared
|
||||
)
|
||||
|
||||
begin {}
|
||||
|
||||
process {
|
||||
if ($Shared.IsPresent) {
|
||||
if ($IsLinux) {
|
||||
return Join-Path -Path '/var/opt' -ChildPath 'PSCredentialStore/CredentialStore.json'
|
||||
}
|
||||
if ($IsMacOS) {
|
||||
return Join-Path -Path '/var/opt' -ChildPath 'PSCredentialStore/CredentialStore.json'
|
||||
}
|
||||
elseif (($isWindows) -or ($PSVersionTable.PSVersion.Major -lt 6) -or ($PSVersionTable.PSEdition -eq 'Desktop')) {
|
||||
return Join-Path -Path $env:ProgramData -ChildPath 'PSCredentialStore/CredentialStore.json'
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($IsLinux) {
|
||||
return Join-Path -Path $Env:HOME -ChildPath 'CredentialStore.json'
|
||||
}
|
||||
if ($IsMacOS) {
|
||||
return Join-Path -Path $Env:HOME -ChildPath 'CredentialStore.json'
|
||||
}
|
||||
elseif (($isWindows) -or ($PSVersionTable.PSVersion.Major -lt 6) -or ($PSVersionTable.PSEdition -eq 'Desktop')) {
|
||||
return Join-Path -Path $env:AppData -ChildPath 'CredentialStore.json'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end {}
|
||||
}
|
29
src/Private/Get-ModuleBase.ps1
Normal file
29
src/Private/Get-ModuleBase.ps1
Normal file
@ -0,0 +1,29 @@
|
||||
function Get-ModuleBase {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Returns the base path of the current module.
|
||||
|
||||
.DESCRIPTION
|
||||
This is just a wrapper for enabling pester tests.
|
||||
|
||||
|
||||
.OUTPUTS
|
||||
Returns the base path as string
|
||||
|
||||
.NOTES
|
||||
File Name : Get-ModuleBase.ps1
|
||||
Author : Marco Blessing - marco.blessing@googlemail.com
|
||||
Requires :
|
||||
|
||||
.LINK
|
||||
https://github.com/OCram85/PSCredentialStore
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
[OutputType()]
|
||||
param()
|
||||
begin {}
|
||||
process {
|
||||
return $MyInvocation.MyCommand.Module.ModuleBase
|
||||
}
|
||||
end {}
|
||||
}
|
44
src/Private/Get-RandomAESKey.ps1
Normal file
44
src/Private/Get-RandomAESKey.ps1
Normal file
@ -0,0 +1,44 @@
|
||||
function Get-RandomAESKey {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Generate a new 32-byte AES key.
|
||||
|
||||
.DESCRIPTION
|
||||
Uses the System.Security.Cryptography namespace for random aes key generation.
|
||||
|
||||
.INPUTS
|
||||
[None]
|
||||
|
||||
.OUTPUTS
|
||||
[byte[]]
|
||||
|
||||
.EXAMPLE
|
||||
.\Get-RandomAESKey
|
||||
|
||||
.NOTES
|
||||
File Name : Get-RandomAESKey.ps1
|
||||
Author : Marco Blessing - marco.blessing@googlemail.com
|
||||
Requires :
|
||||
|
||||
.LINK
|
||||
https://github.com/OCram85/PSCredentialStore
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
[OutputType([byte[]])]
|
||||
param()
|
||||
|
||||
begin {}
|
||||
|
||||
process {
|
||||
$key = [byte[]]::new(32)
|
||||
$rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
|
||||
$rng.GetBytes($key)
|
||||
Write-Output $key
|
||||
if ($null -ne $key) {
|
||||
[array]::Clear($key, 0, $key.Length)
|
||||
}
|
||||
|
||||
}
|
||||
end {}
|
||||
}
|
44
src/Private/Get-TempDir.ps1
Normal file
44
src/Private/Get-TempDir.ps1
Normal file
@ -0,0 +1,44 @@
|
||||
function Get-TempDir {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Returns the valid temp dir of the current OS
|
||||
|
||||
.DESCRIPTION
|
||||
Returns the valid temp dir of the current OS.
|
||||
|
||||
.INPUTS
|
||||
[None]
|
||||
.OUTPUTS
|
||||
[string]
|
||||
|
||||
.EXAMPLE
|
||||
Get-TempDir
|
||||
|
||||
.NOTES
|
||||
File Name : Get-TempDir.ps1
|
||||
Author : Marco Blessing - marco.blessing@googlemail.com
|
||||
Requires :
|
||||
|
||||
.LINK
|
||||
https://github.com/OCram85/PSCredentialStore
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
[OutputType([string])]
|
||||
param()
|
||||
begin {
|
||||
|
||||
}
|
||||
process {
|
||||
if ($IsLinux) {
|
||||
return (Resolve-Path -Path '/tmp/').Path
|
||||
}
|
||||
if ($IsMacOS) {
|
||||
return (Resolve-Path -Path '/tmp/').Path
|
||||
}
|
||||
elseif (($isWindows) -or ($PSVersionTable.PSVersion.Major -lt 6) -or ($PSVersionTable.PSEdition -eq 'Desktop')) {
|
||||
return (Resolve-Path -Path $env:TEMP).Path
|
||||
}
|
||||
}
|
||||
end {
|
||||
}
|
||||
}
|
88
src/Private/Resolve-Dependency.ps1
Normal file
88
src/Private/Resolve-Dependency.ps1
Normal file
@ -0,0 +1,88 @@
|
||||
function Resolve-Dependency {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Tests defined optional dependencies and returns the result as bool.
|
||||
|
||||
.DESCRIPTION
|
||||
Use this function to test for optional modules. You can use it if you provide functions which needs special
|
||||
modules but you don't want to make them required.
|
||||
|
||||
Place a file called Dependency.json in your module root dir. The default format is:
|
||||
|
||||
{
|
||||
"Version": 0.1,
|
||||
"Mandatory": {},
|
||||
"Optional": [
|
||||
{
|
||||
"Name": "VMware",
|
||||
"Modules": [
|
||||
"VMware.VimAutomation.Core"
|
||||
]
|
||||
},
|
||||
{
|
||||
"Name": "CiscoUCS",
|
||||
"Modules": []
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
.PARAMETER Name
|
||||
Select the dependency item name you defined in the dependency.json.
|
||||
.INPUTS
|
||||
[None]
|
||||
|
||||
.OUTPUTS
|
||||
[bool]
|
||||
|
||||
.EXAMPLE
|
||||
If (-not (Resolve-Dependency -Name 'VMware')) {
|
||||
Write-Error -Message ("Could not resolve the optional dependencies defined for {0}" -f 'VMware') -ErrorAction 'Stop'
|
||||
}
|
||||
|
||||
.NOTES
|
||||
```
|
||||
File Name : ResolveDependency.ps1
|
||||
Author : Marco Blessing - marco.blessing@googlemail.com
|
||||
Requires :
|
||||
```
|
||||
|
||||
.LINK
|
||||
https://github.com/OCram85/PSCredentialStore
|
||||
#>
|
||||
[OutputType([bool])]
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string]$Name
|
||||
)
|
||||
|
||||
begin {
|
||||
$ModuleRootDir = Get-ModuleBase
|
||||
$DepFilePath = Join-Path -Path $ModuleRootDir -ChildPath "Dependency.json"
|
||||
if (Test-Path -Path $DepFilePath) {
|
||||
$Dependency = Get-Content -Path $DepFilePath -Raw -Encoding UTF8 | ConvertFrom-Json
|
||||
}
|
||||
else {
|
||||
Write-Warning ("Could not find the dependency file: {0}" -f $DepFilePath)
|
||||
}
|
||||
}
|
||||
|
||||
process {
|
||||
$SelectedDependency = $Dependency.Optional | Where-Object {$_.Name -match $Name}
|
||||
$res = @()
|
||||
foreach ($Module in $SelectedDependency.Modules) {
|
||||
$res += Test-Module -Name $Module
|
||||
}
|
||||
# return false if there was not module at all
|
||||
if (($res -contains $false) -or ($res.Count -eq 0)) {
|
||||
return $false
|
||||
}
|
||||
else {
|
||||
return $true
|
||||
}
|
||||
}
|
||||
|
||||
end {
|
||||
}
|
||||
}
|
79
src/Private/Test-Module.ps1
Normal file
79
src/Private/Test-Module.ps1
Normal file
@ -0,0 +1,79 @@
|
||||
function Test-Module {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Tests if the given module exists on the local system.
|
||||
|
||||
.DESCRIPTION
|
||||
Tests if the given module is installed on the local system. It returns a bool value as result.
|
||||
|
||||
.PARAMETER Name
|
||||
Define a item name you need to test
|
||||
|
||||
.PARAMETER Type
|
||||
Define the dependency type. This could be a Module or PSnapin.
|
||||
|
||||
.PARAMETER MessagePattern
|
||||
You an optionally adjust the message pattern for the error message itself.
|
||||
The available placeholders are:
|
||||
- {0} : Type
|
||||
- {1} : Name
|
||||
|
||||
.PARAMETER StopIfFails
|
||||
This switch forces the entire script to stop if the given dependency object fails.
|
||||
|
||||
.INPUTS
|
||||
[None]
|
||||
|
||||
.OUTPUTS
|
||||
[Bool]
|
||||
|
||||
.EXAMPLE
|
||||
.\Test-Dependency -Name 'VMware.PowerCLI' -Type 'Module'
|
||||
|
||||
.EXAMPLE
|
||||
.\Test-Dependency -Name 'VMware.PowerCLI' -Type 'Module' -StopIfFails
|
||||
|
||||
.NOTES
|
||||
```
|
||||
File Name : Test-Module.ps1
|
||||
Author : Marco Blessing - marco.blessing@googlemail.com
|
||||
Requires :
|
||||
```
|
||||
|
||||
.LINK
|
||||
https://github.com/OCram85/PSCredentialStore
|
||||
#>
|
||||
[OutputType([bool])]
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string]$Name,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[string]$MessagePattern = @"
|
||||
Could not find the required {0} called {1}. Please install the required {0} to run this function!
|
||||
"@,
|
||||
[Parameter(Mandatory = $false)]
|
||||
[switch]$StopIfFails
|
||||
)
|
||||
begin {}
|
||||
|
||||
process {
|
||||
$Message = $MessagePattern -f $Type, $Name
|
||||
Write-Debug $Message
|
||||
|
||||
if (Get-Module -Name $Name -ListAvailable) {
|
||||
return $true
|
||||
}
|
||||
else {
|
||||
if ($StopIfFails) {
|
||||
Write-Error -Message $Message -ErrorAction Stop -Category NotInstalled
|
||||
}
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
end {}
|
||||
}
|
Reference in New Issue
Block a user