Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

elseif not being recognized – Variable not being assigned correct value

I have written a small script that checks the HostName in a URL for a sharepoint Site Collection and then gives a variable a value based on that HostName but the elseif in the script is not working:

$sites = Get-SPSite https://contoso.domain.cs/sites/sc 
$Logo = $null

if ($sites.HostName -eq "contoso.domain.cs" -or "contoso1.domain.cs" -or "contoso2.domain.cs") 
{
    $Logo = "/path/to/logo.jpg"
}
elseif ($sites.HostName -eq "contosoq.domain.cs" -or "contoso1q.domain.cs" -or "contoso2q.domain.cs") 
{
    $Logo = "/path/to/logo2.jpg"
}
elseif ($sites.HostName -eq "contoso3q.domain.cs") 
{
    $Logo = "/path/to/logo3.jpg"
}
else {}

The Variable $Logo is always getting the first value "/path/to/logo.jpg" even when the hostname is not equal to "contoso.domain.cs" or "contoso1.domain.cs" or "contoso2.domain.cs"

please help me if you see the error im making. thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to alter the way you check the conditions. The entire expression to be evaluated must be repeated after each -or.

For example:

if ($sites.HostName -eq "contoso.domain.cs" -or "contoso1.domain.cs" -or "contoso2.domain.cs")

Should be changed to this:

if ($sites.HostName -eq "contoso.domain.cs" -or $sites.HostName -eq "contoso1.domain.cs" -or $sites.HostName -eq "contoso2.domain.cs")

Or you could do it this way:

if ($sites.HostName -in ("contoso.domain.cs", "contoso1.domain.cs", "contoso2.domain.cs"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading