PowerShell script with input parameters #2394
Replies: 3 comments 7 replies
-
If you're using That will make it throw an exception for missing parameters instead of prompting for them. AFAIK there's no way to disable the prompt during runtime in a non-disruptive way. # RequiredParameters.ps1
param(
[Parameter(Mandatory)]
[string]$Param1,
[Parameter(Mandatory)]
[string]$Param2,
[string]$Param3
)
'Success'
# RequiredParameters.tests.ps1
Describe 'd' {
BeforeAll {
$scriptPath = "$PSScriptRoot\RequiredParameters.ps1"
}
It 'All required parameters' {
& $scriptPath -Param1 'a' -Param2 'b' -Param3 'c' | Should -Be 'Success'
}
It 'Some required parameters' {
{ & $scriptPath -Param1 'a' -Param3 'c' } | Should -Throw '*missing mandatory parameters: Param2.'
}
It 'No required parameters' {
{ & $scriptPath -Param3 'c' } | Should -Throw '*missing mandatory parameters: Param1 Param2.'
}
}
# Demo
pwsh -NonInteractive -Command "Import-Module Pester; Invoke-Pester '/workspaces/Pester/Samples/RequiredParameters.tests.ps1'"
Starting discovery in 1 files.
Discovery found 3 tests in 179ms.
Running tests.
[+] /workspaces/Pester/Samples/RequiredParameters.tests.ps1 577ms (173ms|254ms)
Tests completed in 590ms
Tests Passed: 3, Failed: 0, Skipped: 0 NotRun: 0 Do you really need to test this behavior? If so, you might be better off replacing |
Beta Was this translation helpful? Give feedback.
-
Thanks @fflaten for the reply. Suggested code works fine if the code is within the
Below is the
Get the error as
Please help me how to overcome this error |
Beta Was this translation helpful? Give feedback.
-
thanks for the reply @fflaten , I tried adding the message after Thanks for helping me out |
Beta Was this translation helpful? Give feedback.
-
I'm trying to test a PowerShell script which verifies if the required parameters are passed or not before proceeding with rest.
How can I test this using Pester, please help me 🙏
Beta Was this translation helpful? Give feedback.
All reactions