Share via

Pester if statement advice

ZM 211 Reputation points
2022-05-04T11:03:14.013+00:00

Hi, i want to add in my pester script a if statement saying - if not write-error " try again"

how can I do that?

( my script is below)

[Cmdletbinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Regex
)

Describe "Check Parameter" {
It "GUID is Vailed" {
$Regex | should -Match "^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$"
}
}

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Limitless Technology 40,106 Reputation points
2022-05-05T18:31:49.85+00:00

Hi @SakeriyeMohamud-4520

Please follow below steps to run a pester script.

Step 1: Install-Module-Name Pester -Force -SkipPublisherCheck
Step 2: Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Step 3: Save the script as Ps1 file and execute
Step 4: Execute the ps1 file
PS C:\WINDOWS\system32> Invoke-Pester C:\Users\ravi2_000\OneDrive\Desktop\tt.ps1
Starting discovery in 1 files.

cmdlet tt.ps1 at command pipeline position 1
Supply values for the following parameters:
Regex: 96e0b105-f56d-4146-8852-31f946b3a620
Discovery found 1 tests in 2.23s.
Running tests.
[+] C:\Users\ravi2_000\OneDrive\Desktop\tt.ps1 2.39s (5ms|155ms)
Tests completed in 2.41s
Tests Passed: 1, Failed: 0, Skipped: 0 NotRun: 0

Use method option to capture the result of execution and try using the captured value in end statement to repeat the function.

Param (  
[Parameter(Mandatory)]  
[ValidateNotNullOrEmpty()]  
[string]  
$Regex,  
  
[Parameter(  
Mandatory = $False,  
ValueFromPipeline = $True,  
ValueFromPipelineByPropertyName = $True)]  
[Alias("LN")]  
[bool] $val)  
  
Begin{  
Describe "Check Parameter" {  
It "GUID is Vailed" {  
$val = $Regex | should -Match "^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$"  
}  
}  
}  
Process{  
}  
  
End{  
$val  
If ($val -eq 'true')  
{Write-Host = "Files copied from user account: " $UserAccount}  
}  

Here is a helpful page with information about Windows PowerShell

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/powershell

-----------------

--If the reply is helpful, please Upvote and Accept as answer.--

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.