[英]Nested IF, moving AD computers
I was writing a PS script to move computers in AD from one OU to another. 我正在编写PS脚本,以将AD中的计算机从一个OU移到另一个OU。 The original code i had was this;
我原来的代码就是这个。
$pc = Get-ADComputer -Filter *
foreach($item in $pc)
{
if($item.operatingsystem -Like "Windows XP*")
{
if($item.name -Like "*-B-*")
{
move-ADObject -Identity $item -TargetPath 'ou=myou,DC=mydomain,dc=com'
}
else if($item.name -Like "*-K-*")
{
move-ADObject -Identity $item -TargetPath 'ou=myou,DC=mydomain,dc=com'
}
}
else if($item.operatingsystem -Like "Windows 7*")
{
if($item.name -Like "*-B-*")
{
move-ADObject -Identity $item -TargetPath 'ou=myou,DC=mydomain,dc=com'
}
else if($item.name -Like "*-K-*")
{
move-ADObject -Identity $item -TargetPath 'ou=myou,DC=mydomain,dc=com'
}
}
}
The code ran but didn't actually do anything. 代码运行了,但是实际上什么也没做。 I edited it to this and it did what i intended it to do;
我对此进行了编辑,并完成了我打算做的事情。
$pcw7 = Get-ADComputer -Filter {operatingsystem -Like "Windows 7*"}
$pcwinxp = Get-ADComputer -Filter {operatingsystem -Like "Windows XP*"}
foreach($item in $pcw7)
{
if($item.Name -Like "*-B-*")
{
move-ADObject -Identity $item -TargetPath 'ou=myou,DC=mydomain,dc=com'
}
elseif($item.Name -Like "*-K-*")
{
move-ADObject -Identity $item -TargetPath 'ou=myou,DC=mydomain,dc=com'
}
}
foreach($item in $pcwinxp)
{
if($item.Name -Like "*-B-*")
{
move-ADObject -Identity $item -TargetPath 'ou=myou,DC=mydomain,dc=com'
}
elseif($item.Name -Like "*-K-*")
{
move-ADObject -Identity $item -TargetPath 'ou=myou,DC=mydomain,dc=com'
}
}
Can someone help me explain why? 有人可以帮我解释原因吗?
I'm assuming $pc has objects. 我假设$ pc有对象。
Check the default properties returned by get-ADComputers, I'm not sure the operatingsystem property is returned by default. 检查get-ADComputers返回的默认属性,我不确定操作系统属性是否默认返回。 Try the first script with :
使用以下命令尝试第一个脚本:
$pc = Get-ADComputer -Filter * -Properties operatingsystem
This will force PowerShell to get you the default properties + the operatingsystem property of the objects. 这将强制PowerShell为您提供对象的默认属性+操作系统属性。
The reason the second script works is, because you use "operatingsystem" as a filter. 第二个脚本起作用的原因是,因为您使用“操作系统”作为过滤器。 It automatically includes this as part of the properties returned.
它自动将其作为返回的属性的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.