Variable not being assigned correct value.

huangapple go评论65阅读模式
英文:

elseif not being recognized - Variable not being assigned correct value

问题

我已经编写了一个小脚本,用于检查 URL 中的 HostName 是否为 SharePoint Site Collection,然后根据该 HostName 为一个变量赋值,但脚本中的 elseif 部分不起作用:

$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 {}

变量 $Logo 始终获取第一个值 "/path/to/logo.jpg",即使主机名不等于 "contoso.domain.cs"、"contoso1.domain.cs" 或 "contoso2.domain.cs"。

如果您看到我犯的错误,请帮助我。谢谢!

英文:

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!

答案1

得分: 1

需要更改您检查条件的方式。必须在每个“-or”之后重复要评估的整个表达式。

例如:

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

可以更改为明确检查每个条件:

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

或者,您可以使用“-in”比较来执行此操作:

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

如iRon在评论中提到的,以下技巧也有效:

if ("contoso.domain.cs", "contoso1.domain.cs", "contoso2.domain.cs" -eq $sites.HostName)
英文:

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")

Could be changed to check each condition explicitly:

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 by using the -in comparison:

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

As mentioned in the comments by iRon the following technique also works:

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

huangapple
  • 本文由 发表于 2023年2月8日 23:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定