Skip to content
Snippets Groups Projects
Commit aacc9a8c authored by Flavio Ackermann's avatar Flavio Ackermann
Browse files

Files added

parent 25aca92c
No related branches found
No related tags found
No related merge requests found
powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File ".\Sources\Invoke-ElectronicsDataPointExtraction.ps1"
\ No newline at end of file
powershell.exe -ExecutionPolicy Bypass -Command "& {[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12}"
powershell.exe -ExecutionPolicy Bypass -Command "& {Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser}"
powershell.exe -ExecutionPolicy Bypass -Command "& {Set-PSRepository -Name PSGallery -InstallationPolicy Trusted}
powershell.exe -ExecutionPolicy Bypass -Command "& {Find-Module -Name 'ImportExcel' | Install-Module -Scope CurrentUser}"
powershell.exe -ExecutionPolicy Bypass -Command "& {$Files = Get-ChildItem -Path %cd%\* -Recurse -Include '*.ps*1'; Unblock-File -Path $Files.FullName}"
\ No newline at end of file
Import-Module -Name "$PSScriptRoot\Module\ElectronicsDataHandling"
Convert-ElectronicsDataFile
\ No newline at end of file
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
function Get-SelectorFromDialog {
<#
.SYNOPSIS
.DESCRIPTION
.NOTES
https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.2
#>
[CmdletBinding()]
param ()
begin {}
process {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(400, 200)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(125, 120)
$okButton.Size = New-Object System.Drawing.Size(75, 23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(200, 120)
$cancelButton.Size = New-Object System.Drawing.Size(75, 23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10, 20)
$label.Size = New-Object System.Drawing.Size(380, 40)
$label.Text = "How many elements should be extracted?`n(Every n-th element)"
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10, 60)
$textBox.Size = New-Object System.Drawing.Size(30, 20)
$form.Controls.Add($textBox)
$form.Topmost = $true
$form.Add_Shown({ $textBox.Select() })
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
[int]$x = $textBox.Text
$x
}
}
end {}
}
function Import-RequiredModules {
[CmdletBinding()]
param ()
begin {}
process {
$RequiredModules = @('ImportExcel')
foreach ($ModuleName in $RequiredModules) {
if (-not (Get-Module $ModuleName)) {
try {
Import-Module -Name $ModuleName -Verbose:$false -ErrorAction Stop
Write-Verbose "Imported module '$ModuleName'"
} catch {
Write-Warning "Module '$ModuleName' could not be imported"
break
}
}
}
}
end {}
}
function Convert-ElectronicsDataFile {
<#
.SYNOPSIS
.DESCRIPTION
.NOTES
#>
[CmdletBinding()]
param (
[Parameter()]
[System.IO.FileInfo]
$SourceFile,
[Parameter()]
[string]
$OutputFilePath
)
begin {
Import-RequiredModules
#region Select input file
if (-not ($Sourcefile)) {
Add-Type -AssemblyName System.Windows.Forms
$Property = @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
Title = 'Select input file'
}
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property $Property
$null = $FileBrowser.ShowDialog()
$SourceFile = Get-Item -Path $FileBrowser.FileName
Write-Verbose "Source file is '$($SourceFile.FullName)'`n$SourceFile"
}
#endregion
#region Select output file
if (-not ($OutputFilePath)) {
$OutputFileName = "extracted-$($SourceFile.Name)"
} else {
$OutputFileName = $OutputFilePath
}
Add-Type -AssemblyName System.Windows.Forms
$SaveProperty = @{
InitialDirectory = $SourceFile.DirectoryName
OverwritePrompt = $True
Filter = "All files (*.*)|*.*"
Title = 'Save as'
FileName = $OutputFileName
}
$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog -Property $SaveProperty
$SaveStatus = $SaveFileDialog.ShowDialog()
if ($SaveStatus -eq 'Cancel') {
Write-Verbose 'Save file dialog got cancelled'
break
}
$OutputFile = New-Item -Path $SaveFileDialog.FileName -Force
#endregion
$Selector = Get-SelectorFromDialog
$FileExtension = $SourceFile.Extension
if ($FileExtension -notmatch '.txt|.xlsx') {
Write-Warning "File format is not supported: '$FileExtension'"
break
}
Write-Output "Importing content from '$($SourceFile.FullName)'"
switch ($FileExtension) {
'.xlsx' { $Data = Import-Excel -Path $SourceFile.FullName -NoHeader }
'.txt' { $Data = Get-Content -Path $SourceFile.FullName }
}
}
process {
$i = 0
$Result = $Data | ForEach-Object {
if ($i % $Selector -eq 0) {
$_
}
$i++
}
}
end {
switch ($FileExtension) {
'.xlsx' {
$Result | Export-Excel -Path $OutputFile -NoHeader
}
'.txt' {
$Result | Set-Content -Path $OutputFile
}
}
Write-Verbose "Created file '$OutputFile'"
Invoke-Item -Path $OutputFile.DirectoryName
}
}
Export-ModuleMember -Function Convert-ElectronicsDataFile
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment