Check inputs for bad characters, NaN, length - powershell

I am searching for a general input check and I already found some things like NaN or if it is to short but how can you check if there are characters like ($§?=) which you do not want in your input to be?
My second question is what is important to check if you wanna rename a file using an input. Except length.
EDIT:
if($ParID -lt 6) {
$specific_error = "Par ID is is too short!"
} else { #else 1
if(!($ParID -match "[1-999999]")) {
$specific_error = "Par ID must only contain numbers!"
} else { #else 2
}#else 2
} #else 1
EDIT 2:
$ParIDInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$ParIDInbox.Location = New-Object System.Drawing.Size(10,30) #Location -> where the label is located in the window
$ParIDInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox
$ParIDInbox.MaxLength = 6 #sets max. length of the input box to 6

To check whether the filename contains any unwanted character you can use a regex:
$invalidCharacter = '$§?='
[regex]$invalidCharacter = '[{0}]' -f ([regex]::Escape($invalidCharacter))
if ($invalidCharacter.IsMatch($yourFileName))
{
# filename contains some invalid character ...
}
To ensure the filename is valid, you could use the GetInvalidFileNameChars .NET method to retrieve all invalid character and again use a regex to check whether the filename is valid:
[regex]$containsInvalidCharacter = '[{0}]' -f ([regex]::Escape([System.IO.Path]::GetInvalidFileNameChars()))
$yourFileName = 'invali?idFilename.txt'
if ($containsInvalidCharacter.IsMatch($yourFileName))
{
# filename is invalid...
}

Related

Powershell cannot Get data from Generated Textbox

Making a small Budgeting app that populates bills from a list (in my Settings tab, this works fine).
Depending what you have for bills, it will generate what you see in screenshot (1 Label, 2 Textboxes, Calendar, and a Checkbox)
My Issue,
I can not seem to get data from any of these generated components. The goal is for all the Payment Due textboxes to add up their Sum to have a total. Here is what the generated function looks like.
function Generate-Bills
{
$Due = #() # <- Array for Due Bills
$coord = 40
foreach ($bill in Get-Content "$path\House.txt") #<- list of bills stored in this text file
{
$BillName = $bill
$BillDue = $bill + "Due"
$BillNext = $bill + "Next"
$BillDate = $bill + "Date"
$BillCheck = $bill + "Check"
Write-Host $BillName
Write-Host $BillDue
Write-Host $BillNext
Write-Host $BillDate
Write-Host $BillCheck
Write-Host $coord
$BillCheck = New-Object 'System.Windows.Forms.CheckBox'
$BillDate = New-Object 'System.Windows.Forms.DateTimePicker'
$BillNext = New-Object 'System.Windows.Forms.TextBox'
$BillDue = New-Object 'System.Windows.Forms.TextBox'
$BillName = New-Object 'System.Windows.Forms.Label'
#
# Checkbox
#
$BillCheck.Left = '478'
$BillCheck.Top = $coord
$BillCheck.Name = $BillCheck
$BillCheck.Size = '104, 24'
$BillCheck.TabIndex = 4
$BillCheck.Text = 'Paid'
$BillCheck.ForeColor = $textcolor
$BillCheck.UseCompatibleTextRendering = $True
$BillCheck.UseVisualStyleBackColor = $True
$BillCheck.Anchor = 'Top'
#
# Due Date
#
$BillDate.Left = '360'
$BillDate.Top = $coord
$BillDate.Name = $BillDate
$BillDate.Size = '110, 20'
$BillDate.TabIndex = 3
$BillDate.Format = 'Short'
$BillDate.Font = 'Microsoft Sans Serif, 12pt '
$BillDate.Anchor = 'Top, Bottom'
#
# Next Payment
#
$BillNext.Left = '238'
$BillNext.Top = $coord
$BillNext.Name = $BillNext
$BillNext.Size = '100, 20'
$BillNext.TabIndex = 2
$BillNext.Backcolor = '33, 35, 50'
$BillNext.BorderStyle = 'None'
$BillNext.ForeColor = 'White'
$BillNext.Font = 'Microsoft Sans Serif, 18pt, style=Bold'
$BillNext.Anchor = 'Top, Bottom'
#
# Due Payment
#
$BillDue.Left = '108'
$BillDue.Top = $coord
$BillDue.Name = $BillDue
$BillDue.Size = '100, 20'
$BillDue.TabIndex = 1
$BillDue.Backcolor = '33, 35, 50'
$BillDue.BorderStyle = 'None'
$BillDue.ForeColor = 'White'
$BillDue.Font = 'Microsoft Sans Serif, 18pt, style=Bold'
$BillDue.Anchor = 'Top, Bottom'
#Fail Attempt :(
$BillDue.Add_TextChanged({Write-Host $BillDue.Text})
#
# Name of Bill
#
$BillName.AutoSize = $True
$BillName.Left = '18'
$BillName.Top = $coord
$BillName.Name = $BillName
$BillName.Size = '35, 17'
$BillName.TabIndex = 0
$BillName.Text = $bill
$BillName.Font = 'Microsoft Sans Serif, 10pt, style=Bold'
$BillName.ForeColor = 'White'
$BillName.UseCompatibleTextRendering = $True
$BillName.Anchor = 'Top, Bottom'
#
# Controls
#
$HouseGenPanel.Controls.Add($BillCheck)
$HouseGenPanel.Controls.Add($BillDate)
$HouseGenPanel.Controls.Add($BillNext)
$HouseGenPanel.Controls.Add($BillDue)
$HouseGenPanel.Controls.Add($BillName)
$data += $BillDue.Text
$coord += 50
$HouseGenPanel.Height += 45
}
Write-Host $Due "data"
}
I have tried getting it to store the textbox data into an array, but it will not let me pull it,
I have also tried making it so when the textbox updates, it reports the new numbers, but I cant seem to find a way for it to update each textbox properly.
Any Guidance on this is much appreciated as this is the only thing stopping me from finishing this app. (source code will be released on completion.)
== Solution ===
$global:controls = $HousePanel.controls
$output.Text = $global:controls['TextBox1'].text
$global:controls['TextBox1'].Add_TextChanged({ $output.Text = $global:controls['TextBox1'].text})
Add controls to a global variable, then call the generated .Name
By throwing the controls into a global variable, I was able to access generated variables, and get their updated text with the following snippet.
$global:controls = $HousePanel.controls
$output.Text = $global:controls['TextBox1'].text
$global:controls['TextBox1'].Add_TextChanged({ $output.Text = $global:controls['TextBox1'].text})

Use Powershell to split one column of an excel workbook by a delimiter

Hello all I am still trying to learn power shell. the issue I have is that I am trying to use Power shell to look at one column in a workbook and split it into several columns by a delimiter (=) horizontally 3 times. I have also looked into using the split option but I do not understand exactly how to do either. I can not post the either sheet but the line is
Hash_tag
This is what I would like to see
The below is that I have
$SRReport="C:\Users\ddpierce\Desktop\Metrics\u_service_request.csv"
$OBJExcel=New-Object -ComObject Excel.Application
$OBJexcel.visible=$true
$WorkBook=$objExcel.Workbooks.Open($SRReport)
$worksheets=$workbook.worksheets
$sheet=$worksheets.item(1)
$sheet.activate
$range=$sheet.usedrange
$a = $countrange
$colA=$sheet.range("A1").EntireColumn
$colrange=$sheet.range("A1")
$xlDelimited = 1
$xlTextQualifier = -4142
$xlTextFormat = 1
$xlConsecutiveDelimiter = $true
$xlTab = $flase
$xlSemicolon = $true
$xlComma = $true
$xlSpace = $true
$xlOther = $flase
$xlOtherChar = $true,':'
#Convert Text To Columns
#$colA.texttocolumns($colrange,$xlDelimited,$xlTextQualifier,$true,$false,$false,$false,$false)# = nothing 1
#$colA.texttocolumns($colrange,$xlDelimited,$xlTextQualifier,$false,$true,$false,$false,$false)# = nothing 2
#$colA.texttocolumns($colrange,$xlDelimited,$xlTextQualifier,$false,$false,$true,$false,$false)# = split on simi 3
#$colA.texttocolumns($colrange,$xlDelimited,$xlTextQualifier,$false,$false,$false,$true,$false)# = split on comma 4
#$colA.texttocolumns($colrange,$xlDelimited,$xlTextQualifier,$false,$false,$false,$false,$true)# = split on space 5
#$colA.texttocolumns($colrange,$xlDelimited,$xlTextQualifier,$true,$true,$true,$true,$true,$true,$true)
$colA.texttocolumns($colrange,$xlDelimited,$xlTextQualifier,$xlConsecutiveDelimiter,$xlTab,$xlSemicolon,$xlComma,$xlSpace,$xlOther,$xlOtherChar)
#$sheet.columns.autofit()
#Save as XLSX
#$workbook.saveas("C:\Users\ddpierce\Desktop\Metrics\u_service_request2.csv")
#$WorkBook.save=$true
#$workbook.close($true)
#$objExcel.Quit()

Cannot convert the "System.Reflection.Missing" value of type "System.Reflection.Missing" to type "System.Int32". - PowerShell PPT Printing

I'm trying to put in default/null/missing values for .printout in PowerPoint but I can't get it to work. The value's that are giving me troubles are defined as integers in this page.
Usually I have been able to use [System.Type]::Missing or '' but neither of those work and I get the following:
Cannot convert the "System.Reflection.Missing" value of type "System.Reflection.Missing" to type "System.Int32".
How can I put in null values for those two params of .printout for PowerPoint?
Here is what I have for code so far:
###PPT
IF ($FILETYPE -eq "PPT") {
ECHO "PPT_FOUND"
$msoFalse = [Microsoft.Office.Core.MsoTriState]::msoFalse
$msoTrue = [Microsoft.Office.Core.MsoTriState]::msoTrue
$ObjPPT = New-Object -comobject PowerPoint.Application
$ObjPPT.Visible = $msoTrue
#$ObjPPT.DisplayAlerts = $msoFalse
#Exception setting "DisplayAlerts": "Cannot convert value "msoFalse" to type "Microsoft.Office.Interop.PowerPoint.PpAlertLevel" due to enumeration values that are not valid. Specify one of the following
#enumeration values and try again. The possible enumeration values are "ppAlertsNone,ppAlertsAll"."
#.Open
$FILENAME = $FILEPATH
IF (!$ReadOnly){$ReadOnly = $msoTrue}
IF (!$Untitled){$Untitled = $missing}
IF ((!$WithWindow) -and ($OPENUI -eq "FALSE")){$WithWindow = $msoFalse} ELSE {$WithWindow = $msoTrue}
$ObjPresentation=$ObjPPT.Presentations.open($FILENAME,$ReadOnly,$Untitled,$WithWindow)
#.Print
#.PrintOut (From[Integer], To[Integer], PrintToFile[String], Copies [Integer], Collate[Bool]{msoTrue/False})
IF (!$From){[int]$From = $missing}
IF (!$To){[int]$To = $missing}
IF (!$PrintToFile){[string]$PrintToFile = $missing}
#Copies Defined Earlier
IF (!$Collate){$Collate = $msoTrue}
#$ObjPresentation.printout($From,$To,$PrintToFile,$COPIES,$Collate)
$ObjPresentation.printout($From,$To)# , ,$COPIES,$Collate)
#All Done
Sleep 2
$ObjPresentation.Close()
$ObjPPT.Quit()
Do-CloseIfRunning -Process $PROCESS
}
[gc]::collect()
[gc]::WaitForPendingFinalizers()
#Get-Variable
#PAUSE
Turns out using 0 caused an error, but using -1 for From/To values worked as expected (null values).

Odd and even pages footer - page X of Y in powershell

I would like to generate word documents in Powershell with different footer on even and odd pages.
I would like to have text with data on left side, and Page X of Y on right side on one page and reverse combination on next page.
First problem is in X of Y format. And second problem I have with placing text with field in one footer. If I put field, the text was disappear, and when I put text, field was disappearing.
PowerShell add field codes to ms word footer - does not work in my case.
I will be grateful for your help.
$dat = Get-Date -Format yyyy-MM-dd
$word = New-Object -ComObject word.application
$word.Visible = $false
$doc = $word.documents.add()
$doc.PageSetup.OddAndEvenPagesHeaderFooter = $true
$selection = $word.Selection
# ...
# content
# ...
$section = $doc.sections.item(1)
$footer = $section.Footers(1)
$Range = $footer.Range
$Range.Text = "Some text($dat)"
$Range.ParagraphFormat.Alignment = 0
$footer.PageNumbers.Add(2)
$Range.Font.Size = 10
$footer = $section.Footers(3)
$Range = $footer.Range
$Range.Text = "Some text($dat)"
$Range.ParagraphFormat.Alignment = 2
$footer.PageNumbers.Add(0)
$Range.Font.Size = 10
$outputPath = 'C:\FileToDocx.docx'
$doc.SaveAs($outputPath)
$doc.Close()
$word.Quit()
The problem is also that the $footer.PageNumbers.Add() is global and it does not depend from my odd and even footer range.
Also I was tried resolve my problem by swap PageNumbers to combination of $Range.Fields.Add($Range, 26) and $Range.Fields.Add($Range, 33), e.g.
$fieldsPage = $Range.Fields.Add($Range, 33)
$sumField = $fieldsPage.Result.Text
$fieldsNumPages = $Range.Fields.Add($Range, 26)
$sumField += ' of ' + $fieldsNumPages.Result.Text
$range.Text = "Some text($dat)`t`tPage $sumField"
but in this case I have static string. I don't know how to use this two type of fields with static text with $dat in footer without conversion to string. The footer forces me to use fields, because only they can be different on each page.

PowerShell HashTable - self referencing during initialization

I have a theoretical problem - how to reference a hash table during its initialization, for example, to compute a member based other already stated members.
Remove-Variable myHashTable -ErrorAction Ignore
$myHashTable =
#{
One = 1
Two= 2
Three = ??? # following expressions do not work
# $This.One + $This.Two or
# $_.One + $_.Two
# $myHashTable.One + $myHashTable.Two
# ????
}
$myHashTable.Three -eq 3 # make this $true
Any ideas how to do it? Is it actually possible?
Edit:
This was my solution:
$myHashTable =
#{
One = 1
Two= 2
}
$myHashTable.Three = $myHashTable.One + $myHashTable.Two
This won't be possible using the object initializer syntax I'm afraid. While it is possible to use variables, you'll have to compute the values before creating the object.
I cannot recommend this, but you can iterate the initializer twice or more:
(0..1) | %{
$a = #{
One = 1
Two = $a.One + 1
}
}
(0..2) | %{
$b = #{
One = 1
Two = $b.One + 1
Three = $b.Two + 1
}
}
Make sure all calculations are idempotent, i.e. do not depend on a number of iterations.
You can also recur to this...
sometimes when the hashtable is very long
and can be defined only in 2 or three recurrences...
works fine:
$AAA = #{
DAT = "C:\MyFolderOfDats"
EXE = "C:\MyFolderOfExes"
}
$AAA += #{
Data = $AAA.DAT + "\#Links"
Scripts = $AAA.EXE + "\#Scripts"
ScriptsX = $AAA.EXE + "\#ScriptsX"
}
Note in the second part we are just adding ( += ) more items to the first part... but now... we can refer the items in first part
of the hashtable