PSCredentialStore/src/ChallengeFile/Set-ChallengeFile.ps1
Marco Blessing 64af16cc08 Publish Pre-release (#1)
* adds basic module layout

* fix module manifest encoding

* fix callsign in appveyor helper

* adds challenge file related functions

* adds connection manager functions

* adds Test-ChallengeFile

* adds item related functions

* adds store related functions

* adds cSpell dictionary

* adds CredentialStore related Pester tests

* [WIP] test Pester file

* fix typo

* adds file dependencies

* [WIP] fix pester tests

* fix exception state

* [WIP] add file dependencies

* fix gitkeep filename

* set constant debug module version string

* adds Pester Tests for New-CredentialStoreItem

* adds basic readme file

* adds functions to export; adds meta data

* adds vscode debug config

* adds test for optional dependencies

* [WIP] Implements optional dependency test

* adds taskrunner definitions

* adds CBH

* add gitignore file

* adds basic Build tasks

* typo fixed

* adds build folder to ignore list

* adds Cisco and NetApp opt dependencies

* adds build task

* fix end of line dequence

* remove task.json error

* adds sources for optional modules

* enables Pester and posh-git

* prepare pre-release
2017-09-21 13:32:15 +02:00

73 lines
1.9 KiB
PowerShell

Function Set-ChallengeFile() {
<#
.SYNOPSIS
Writes the given key into the challenge file
.DESCRIPTION
You can use the file content for ConvertTo-SecureString operations.
.PARAMETER Path
The file you wish to create.
.PARAMETER KeySize
Specify the key size for the encryption key.
.PARAMETER Force
Use this switch to override an older file version.
.INPUTS
[None]
.OUTPUTS
[None]
.EXAMPLE
.\Set-ChallengeFile -Path "C:\TMP\myfile.json" -Force
.NOTES
File Name : Set-ChallengeFile.ps1
Author : Marco Blessing - marco.blessing@googlemail.com
Requires :
.LINK
https://github.com/OCram85/PSCredentialStore
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$Path = "{0}\PSCredentialStore\Challenge.bin" -f $env:ProgramData,
[Parameter(Mandatory = $false)]
[ValidateSet(16, 24, 32)]
[string]$KeySize = "24",
[switch]$Force
)
if ((Test-Path -Path $Path)) {
if ($Force -eq $true) {
Remove-Item -Path $Path -Confirm:$false -Force
}
else {
Write-Error "The given file already exists!. Use the -Force switch to override it." -ErrorAction Stop
}
}
$PSCredentialStoreDataDir = Split-Path -Path $Path -Parent
if (-not (Test-Path $PSCredentialStoreDataDir)) {
try {
New-Item -ItemType Directory -Path $PSCredentialStoreDataDir
}
catch {
Write-Error ("Could not create the parent data dir {0}" -f $PSCredentialDataDir) -ErrorAction Stop
}
}
try {
$Keys = Get-RandomKey -Size $KeySize
[io.file]::WriteAllBytes($Path, $Keys)
}
catch {
$_.Exception | Format-List -Force | Out-String | Write-Error -ErrorAction Stop
}
}