try 为我的IT帮助台团队创建一个基本脚本,该脚本查询我们的广告,以查看计算机的主机名是否已在域中.我希望这不需要任何模块,这样它就可以在任何Windows计算机上运行,而无需额外的软件(因此,我使用DirectoryServices
).
此外,公司编码策略阻止我预定义凭据,因此我希望将其设置为用户定义的.然而,似乎只有预定义了脚本,脚本才能成功运行.如果凭据是用户定义的,则获取以下错误.
使用"0"参数调用"FindAll"时出现异常:"用户名或密码不正确."
下面是我所拥有内容的修订版本.
$username = read-host 'Enter username'
$password = read-host 'Enter password' -AsSecureString
$hostname = hostname
Write-Host "Checking if $hostname is on the domain..."
$filter = "(&(ObjectCategory=computer)(Name=$hostname))"
$de = New-Object DirectoryServices.DirectoryEntry("LDAP://contoso.com/DC=contoso,DC=onmicrosoft,DC=com",$username,$password)
$ds = New-Object DirectoryServices.DirectorySearcher $de
$ds.filter = $filter
$ds.findall() |
ForEach-Object -Begin {} -Process{$_.properties.item(“Name”)} -End { [string]$ds.FindAll().Count } | Out-Null
if ($ds.FindAll().Count)
{
Write-Host "true"
}
else
{
Write-Host "false"
}
凭证可以由用户定义还是必须预定义?