How Do I format an 'if' conditional into a variable? - powershell

I have a function where if a -Exception parameter is present, I need to use a different if statement. Is there a way I can put in if statement conditionals into a variable?
function Set-ErrorProvider
{
Param (
[Parameter(Mandatory = $true)]
[System.Windows.Forms.ErrorProvider]$Name,
[Parameter(Mandatory = $false)]
[System.Windows.Forms.Control[]]$Controls,
[Parameter(Mandatory = $false)]
[string]$TextBoxException,
[Parameter(Mandatory = $false)]
[ValidateSet("MiddleLeft")]
[string]$Alignment,
[Parameter(Mandatory = $false)]
[string]$Message,
[Parameter(Mandatory = $false)]
[int]$Padding,
[switch]$Clear
)
# Exceptions are written in the format of an if statement condition
# e.g. -and $TextBox -ne "TextBoxAction"
if ($PSBoundParameters.ContainsKey('$TextBoxException').Equals($false))
{
$TextboxIf = $Control.Text -eq $null -or $Control.Text.Trim().Length -eq 0
}
else
{
$TextboxIf = '$Control.Text -eq $null -or $Control.Text.Trim().Length -eq 0 $Exception'
}
I'm having trouble formatting the $TextboxIf variable because I want to use it as below:
if ($TextboxIf)
The $Exception variable will hold something like "-and $Textbox.Text -ne $null" (a custom exception).
Any ideas on how to format the if conditional for the $TextBoxIf variable?

Related

Powershell functions, parameters and arg

I write own powershell func for debug like:
function StartDebug {
param (
[PARAMETER(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
$FunctionName,
[PARAMETER(Mandatory = $false)]
$OtherArg
)
try {& $FunctionName $OtherArg} catch {...} finally {...}
and use it everyway, but i need more arg after $FunctionName. is it realistic to pass many arguments in this case bec use from 0 to 10 arg. do I have to list all the arguments that can be in the parameters of the function? like:
function StartDebug {
param (
[PARAMETER(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
$FunctionName,
[PARAMETER(Mandatory = $false)]
$OtherArg,
[PARAMETER(Mandatory = $false)]
$OtherArg1,
[PARAMETER(Mandatory = $false)]
$OtherArg2,
[PARAMETER(Mandatory = $false)]
$OtherArg3
)
try {& $FunctionName $OtherArg OtherArg1 OtherArg2 OtherArg3 } catch {...} finally {...}
but i dont use positional parameters in code and too many named parameters in code (~100)
Interested in any ideas about this. tnx!
The magic word is Splatting. You can provide an array or a hashtable containing your arguments to a function. The splatter is written with an #VariableName instead of the $:
function StartDebug {
param (
[PARAMETER(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
$FunctionName,
[PARAMETER(Mandatory = $false)]
$OtherArg
)
try {& $FunctionName #OtherArg # Watch out for the # in the OtherArg
} catch {$_} finally {}
}
$FunctionName = 'Get-ChildItem'
$Splatter = #{
Path = 'C:\'
Filter = 'Users'
Directory = $true
}
$Splatter2 = #('c:\')
StartDebug $FunctionName $Splatter
StartDebug $FunctionName $Splatter2
However if you want to use single items as $OtherArg you will have to provide them as single element array as can be seen with $Splatter2. Or extend your function to transform single arguments in arrays automatically, but thats up to you.
I think you better run it using scriptblock:
$result = Invoke-DeepDebug { Get-ChildItem -Path 'C:\' ; Get-Service -InformationAction Continue}
And in Invoke-DeepDebug you can work with $Command.AST as deep and detailed as you want.
Function Invoke-DeepDebug {
param(
[Parameter(Mandatory=$true, Position=0)]
[Scriptblock]$Command
)
Write-Host -f Cyan "Executing " -n
Write-Host -f Magenta $Command.Ast.Extent.Text -n
Write-Host -f Yellow " ... " -n
$result = $null
try {
$result = Invoke-Command $Command -ErrorAction Stop
Write-Host -f Green "OK!"
} catch {
Write-Host -f Red "Error"
Write-Host -f Red "`t$($_.Exception.Message)"
}
return $result
}

PowerShell getting logs from Jobs

I am using a PowerShell module called Logging. I use other Jobs to do some work, but unforunately the logs that I write in them are not received by the command Receive-Job.
function Get-Topic {
[CmdletBinding(DefaultParameterSetName = "Normal")]
[OutputType(ParameterSetName = "Normal")]
[OutputType(ParameterSetName = "AsJob")]
param (
[Parameter(Mandatory = $true, ParameterSetName = "Normal")]
[Parameter(Mandatory = $true, ParameterSetName = "AsJob")]
[string]
$ResourceGroupName,
[Parameter(Mandatory = $true, ParameterSetName = "Normal")]
[Parameter(Mandatory = $true, ParameterSetName = "AsJob")]
[string]
$Namespace,
[Parameter(Mandatory = $true, ParameterSetName = "Normal")]
[Parameter(Mandatory = $true, ParameterSetName = "AsJob")]
[string]
$Topic,
[Parameter(Mandatory = $true, ParameterSetName = "AsJob")]
[switch]
$AsJob
)
if ($AsJob) {
$PSBoundParameters.Remove('AsJob') | Out-Null
return Start-ThreadJob -ScriptBlock {
param(
[string]$myFunction,
[System.Collections.IDictionary]$argTable,
[System.Collections.Concurrent.ConcurrentDictionary[string, hashtable]] $loggingTargets
)
$loggingTargets.Keys | ForEach-Object { Add-LoggingTarget -Name $_ -Configuration $loggingTargets[$_] }
$cmd = [scriptblock]::Create($myFunction)
& $cmd #argTable
} -ArgumentList $MyInvocation.MyCommand.Definition, $PSBoundParameters, (Get-LoggingTarget)
}
$topicObj = Get-AzServiceBusTopic `
-ResourceGroupName $ResourceGroupName `
-Namespace $Namespace `
-Name $Topic
Write-Log -Message "Received topic $($topicObj.Name)" -Level INFO
return $topicObj
}
Is there any way to redirect the output to the parent powershell session? I saw that Write-Host works, but the logger with the Console target doesn't. Any workarounds for this?
The solution is the following:
return Start-ThreadJob -StreamingHost (Get-Host) -ScriptBlock {
param(
[string]$myFunction,
[System.Collections.IDictionary]$argTable,
[System.Collections.Concurrent.ConcurrentDictionary[string, hashtable]] $loggingTargets
)
$loggingTargets.Keys | ForEach-Object { Add-LoggingTarget -Name $_ -Configuration $loggingTargets[$_] }
$cmd = [scriptblock]::Create($myFunction)
& $cmd #argTable
} -ArgumentList $MyInvocation.MyCommand.Definition, $PSBoundParameters, (Get-LoggingTarget)
Thanks god the cmdlet Start-ThreadJob allows for such a functionality, basically the param -StreamingHost solved the deal

Variable Assignment Not Working With Switch Parameter

I'm doing something that seems pretty basic to me but is not working as expected.
If the script is run with the -WhatIf switch then $liveTest should be "Test".
If the script is run with the -Live switch then $liveTest should be "Live".
However both switches are causing $liveTest to be "Test"
param (
[CmdletBinding()]
[Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyTest')]
[Switch]
$users,
[Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyTest')]
[Switch]
$computers,
[Parameter(Mandatory = $true, ParameterSetName = 'AllLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'AllTest')]
[Switch]
$all,
[Parameter(Mandatory=$true)]
[string]
$days,
[switch]
$console,
[Parameter(Mandatory = $true, ParameterSetName = 'AllTest')]
[Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyTest')]
[Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyTest')]
[switch]
$WhatIf,
[Parameter(Mandatory = $true, ParameterSetName = 'AllLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyLive')]
[Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyLive')]
[switch]
$live
)
Process {
# If -WhatIf or -Live switch is passed, creates a hashtable for the -WhatIf parameter.
If($WhatIf) {
$whatIf = #{ WhatIf = $true }
$liveTest = "Test"
}
ElseIf($live) {
$whatIf = #{ WhatIf = $false }
$liveTest = "Live"
}
If($liveTest = "Test"){Write-Output $liveTest}
elseif($liveTest = "Live"){Write-Output $liveTest}
}
Your if and elseif conditions are using the assignment operator = rather than comparison operator -eq. As a result, $liveTest is getting set to Test on each run. Update your code to the following:
if ($liveTest -eq "Test") {
Write-Output $liveTest
}
elseif ($liveTest -eq "Live") {
Write-Output $liveTest
}
Since you are using if and elseif conditions to do variable assignment, $liveTest = "Test" always happens and $liveTest = "Live" never happens.
See About_Comparison_Operators for more information.

Powershell errorhandling on empty parameters

This is a small script I'm calling from an excel spreadsheet:
Param (
[Int] $ID,
[String] $server,
[String] $db,
[String] $uid,
[String] $pw,
[String] $Navn,
[String] $Path
)
Try
{
Add-Type -Path $Path
foreach($i in [BrReader2.BrReader]::Main($ID, $uid, $pw, $db, $server)){if($i.Key -eq $Navn){$Data = $i}}
[BrReader2.BrReader]::WriteToTemporaryTable($Data, $ID, $uid, $pw, $db, $server)
}
Catch
{
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
Read-Host -Prompt "Press Enter to exit"
}
It works mostly pretty well, except for one little thing. If for some reason one of the parameters is entered in the wrong way, like if it's completely empty, it will fail there and not write out an error message. The console window will simply quickly appear and disappear again and the user might not realise what they've done wrong.
Giving a wrong path is fine, it will still enter the try/catch block and write a proper error message to the console. Giving NO path does not keep the console window open so the user can see what went wrong though.
I need to put the 'Param (...' part at the top of the script though, so I can't put it into the try/catch block. Is there any way I can write out an error message and keep the console window open if one of the parameters fail?
I'm not very experienced with powershell so there might be something obvious I'm missing.
If you want the parameters to be set, you can make them mandatory.
If they should contain a certain value, you can also validate them.
For example:
Param (
[Parameter(Mandatory = $true)]
[ValidateRange(10,99)]
[Int] $ID,
[Parameter(Mandatory = $true)]
[String] $server,
[Parameter(Mandatory = $true)]
[ValidateSet("sql01.contoso.local","sql02.contoso.local")]
[String] $db,
[Parameter(Mandatory = $true)]
[String] $uid,
[Parameter(Mandatory = $true)]
[String] $pw,
[Parameter(Mandatory = $true)]
[String] $Navn,
[Parameter(Mandatory = $true)]
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[String] $Path
)

How to reuse/extend functions on Powershell?

I'm trying to develop 2 functions with Powershell. The first, will check my database status (online/offline). The second function should loop on the first function until a certain state is achieve.
function Get-DBStatus
{
<# .. removed help section for brevity .. #>
[CmdletBinding()]
[OutputType([System.Object])]
param
(
[Parameter(Mandatory = $true)]
[String]$ServerName,
[Parameter(Mandatory = $true)]
[String]$ServerUser,
[Parameter(Mandatory = $true)]
[String]$ServerPassword,
[Parameter(Mandatory = $true)]
[String]$DatabaseName,
)
try
{
$params = #{ ... }
$dbStatus = Invoke-SqlConnection #params | Where-Object {$_.Name -match $AltDBName }
}
catch
{
Write-Error -Message ('An error has occured while ...')
}
if ([String]::IsNullOrEmpty($dbStatus) -eq $false)
{
$dbStatus
}
# <<< function Get-DbStatusOnlyIf
# <<< same parameters as the function above
# <<< get the desired status as a new parameter
# <<< loop the function above until the desired status is achieved or a timeout is reached
}
I'm new to Powershell and I think I shouldn't repeat myself rewriting the same parameters from the first function into the second one since they're dependent. However, I might be wrong, thus the question.
Thank you for your assistance!
You have to rewrite this parameters on your second function and pass them through or add another paramter to your first function that will do the looping. I would go with the second solution.
Try something like that
function Get-DBStatus {
<# .. removed help section for brevity .. #>
[CmdletBinding()]
[OutputType([System.Object])]
param
(
[Parameter(Mandatory = $true)]
[String]$ServerName,
[Parameter(Mandatory = $true)]
[String]$ServerUser,
[Parameter(Mandatory = $true)]
[String]$ServerPassword,
[Parameter(Mandatory = $true)]
[String]$DatabaseName,
$WaitForStatus, #or something like that
[int]$Timeout=10
)
do {
try {
#$params = #{ ... }
$dbStatus = Invoke-SqlConnection #params | Where-Object {$_.Name -match $AltDBName }
}
catch {
Write-Error -Message ('An error has occured while ...')
return
}
if ([String]::IsNullOrEmpty($dbStatus) -eq $false) {
if ($WaitForStatus){
if ($dbStatus -eq $WaitForStatus) {
$dbStatus
$EndLoop = $true
}
else {
Write-Host -NoNewline "." #only for test
Start-Sleep -Seconds 1
$Timeout -= 1
}
}
else{
$dbStatus
$EndLoop = $true
}
}
}
until ($EndLoop -or $Timeout -eq 0)
}
or with recursion
function Get-DBStatus {
<# .. removed help section for brevity .. #>
[CmdletBinding()]
[OutputType([System.Object])]
param
(
[Parameter(Mandatory = $true)]
[String]$ServerName,
[Parameter(Mandatory = $true)]
[String]$ServerUser,
[Parameter(Mandatory = $true)]
[String]$ServerPassword,
[Parameter(Mandatory = $true)]
[String]$DatabaseName,
$WaitForStatus, #or something like that
[int]$timeout = 3
)
if ($WaitForStatus) {
$start = Get-Date
while (((get-date) - $start).TotalSeconds -lt $timeout) {
$res = Get-DBStatus -ServerName $ServerName -ServerUser $ServerUser -ServerPassword $ServerPassword -DatabaseName $DatabaseName
if ($WaitForStatus -eq $res) {
return $res
}
Start-Sleep -Seconds 1
}
}
else {
try {
$params = #{ ... }
$dbStatus = Invoke-SqlConnection #params | Where-Object {$_.Name -match $AltDBName }
}
catch {
Write-Error -Message ('An error has occured while ...')
}
if ([String]::IsNullOrEmpty($dbStatus) -eq $false) {
$dbStatus
}
}
}