0

I have the following code:

$PreReqFail1 = "- Your computer is not in the office"
$PreReqFail2 = "- Your computer does not have its power cable plugged in"
$PreReqFail3 = "- Your computer does not have a network cable plugged in"

If (-NOT([string]::IsNullOrEmpty($PreReqFail1))) {$PreReqFail = $True}
If (-NOT([string]::IsNullOrEmpty($PreReqFail2))) {$PreReqFail = $True}
If (-NOT([string]::IsNullOrEmpty($PreReqFail3))) {$PreReqFail = $True}

If ($PreReqFail)
{
    $Issues = @($PreReqFail1,$PreReqFail2,$PreReqFail3)
    $Issues
    $PreReqFails = "The following conditions have not been met:`r`n`r`n" + $Issues
    $PreReqFails
}

$Issues = $null
$PreReqFail = $null
$PreReqFail1 = $null
$PreReqFail2 = $null
$PreReqFail3 = $null
$PreReqFails = $null

The idea is that I can comment out any one or more of the $PreReqFailx variables and present a dynamic list of the issues, e.g. 1,2,3 or 1,2 or 1,3 or 2,3, etc.

The Output from $Issues (lines 11 & 12) is as follows:

- Your computer is not in the office
- Your computer does not have its power cable plugged in
- Your computer does not have a network cable plugged in

This is the format I want. However, when I add it to the $PreReqFails variable, as shown in lines 13 & 14, this is what I get:

The following conditions have not been met:

- Your computer is not in the office - Your computer does not have its power cable plugged in - Your computer does not have a network cable plugged in

How I do tweak things so the output shows carriage returns for each issue, for example:

The following conditions have not been met:

- Your computer is not in the office
- Your computer does not have its power cable plugged in
- Your computer does not have a network cable plugged in

Thanks in advance for your assistance.

1 Answer 1

1

Why not use a single array that contains all the values:

$PreReqFail = @(
    # commenting here also works
    "- Your computer is not in the office"
    "- Your computer does not have its power cable plugged in"
    "- Your computer does not have a network cable plugged in"
)

Then you can follow more or less the same logic you already have but without needing to check if each variable is empty and join then by `n (new-line escape character):

If ($PreReqFail) {
    "The following conditions have not been met:`r`n`r`n$($PreReqFail -join "`n")"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh the simplicity of it! Thanks very much, it worked a treat.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.