question

SakeriyeMohamud-4520 avatar image
0 Votes"
SakeriyeMohamud-4520 asked LimitlessTechnology-2700 answered

Pester if statement advice

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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://docs.microsoft.com/en-us/windows-server/administration/windows-commands/powershell


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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.