简体   繁体   English

import-csv 和 foreach 的问题

[英]Issue for import-csv and foreach

I want to import a csv, then delete from AD several objects我想导入一个 csv,然后从 AD 中删除几个对象

$ImportComputer = "C:\Users\deng\Desktop\ComputerLastlogondateformatBis.csv"

Import-Module ActiveDirectory

foreach ($Computer in(Import-Csv -Path C:\Users\deng\Desktop\ComputerLastlogondateformatBis.csv))
{
Remove-ADObject -Identity $Computer.'Computer'

these two object exist in AD, but I cannot seem to find out why it is not working.这两个对象存在于 AD 中,但我似乎无法找出它不起作用的原因。

see below error message:请参阅以下错误消息:

Remove-ADObject : Cannot find an object with identity: 'fr-borr-mac' under: 'DC=PII,DC=net'.
At C:\Users\deng\OneDrive - Aptus Health\Script\Export.ps1:7 char:1
+ Remove-ADObject -Identity $Computer.'Computer'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (fr-borr-mac:ADObject) [Remove-ADObject], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.RemoveADObject

Remove-ADObject : Cannot find an object with identity: 'jlinmacfr' under: 'DC=PII,DC=net'.
At C:\Users\deng\OneDrive - Aptus Health\Script\Export.ps1:7 char:1
+ Remove-ADObject -Identity $Computer.'Computer'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Content of the CSV below: CSV 的内容如下:

Computer   
--------   
fr-borr-mac
jlinmacfr  

Could anyone give input on this?有人可以对此提供意见吗?

The -Identity parameter on the *-ADObject commands expect either a DistinguishedName or Guid value. *-ADObject命令上的-Identity参数需要DistinguishedNameGuid值。 If you are wanting to work with SamAccountName or some other attribute, you should consider using the *-ADComputer or using -Filter to find your objects.如果你是想用工作SamAccountName或其他一些属性,你应该考虑使用*-ADComputer或使用-Filter找到你的对象。

# Using Remove-ADObject
Remove-ADObject -Filter "SamAccountName -eq '$($Computer.Computer)'"

# Using Remove-ADComputer
Remove-ADComputer -Identity $Computer.Computer

Alternatively, you can use Get-ADComputer or Get-ADObject to retrieve your object first and then pipe that into Remove-ADObject .或者,您可以使用Get-ADComputerGet-ADObject首先检索您的对象,然后将其通过管道Remove-ADObjectRemove-ADObject

Get-ADObject -Filter "SamAccountName -eq '$($Computer.Computer)'" | Remove-ADObject

See the Remove-ADObject documentation for the following excerpt regarding explicitly binding to -Identity :有关显式绑定到-Identity的以下摘录,请参阅Remove-ADObject文档:

Specifies an Active Directory object by providing one of the following property values.通过提供以下属性值之一来指定 Active Directory 对象。 The identifier in parentheses is the Lightweight Directory Access Protocol (LDAP) display name for the attribute.括号中的标识符是该属性的轻量级目录访问协议 (LDAP) 显示名称。 The acceptable values for this parameter are:此参数可接受的值为:

  • A distinguished name一个独特的名字
  • A GUID (objectGUID)一个 GUID (objectGUID)

For piping an object into Remove-ADObject , the following excerpt applies, which is why you can use a Get-AD* command and pipe the result into the Remove-ADObject :要将对象通过管道Remove-ADObjectRemove-ADObject ,以下摘录适用,这就是您可以使用Get-AD*命令并将结果通过管道Remove-ADObjectRemove-ADObject

This parameter can also get this object through the pipeline or you can set this parameter to an object instance.此参数也可以通过管道获取此对象,或者您可以将此参数设置为对象实例。

Derived types, such as the following, are also accepted:也接受派生类型,例如以下类型:

  • Microsoft.ActiveDirectory.Management.ADGroup Microsoft.ActiveDirectory.Management.ADGroup
  • Microsoft.ActiveDirectory.Management.ADUser Microsoft.ActiveDirectory.Management.ADUser
  • Microsoft.ActiveDirectory.Management.ADComputer Microsoft.ActiveDirectory.Management.ADComputer
  • Microsoft.ActiveDirectory.Management.ADServiceAccount Microsoft.ActiveDirectory.Management.ADServiceAccount
  • Microsoft.ActiveDirectory.Management.ADFineGrainedPasswordPolicy Microsoft.ActiveDirectory.Management.ADFineGrainedPasswordPolicy
  • Microsoft.ActiveDirectory.Management.ADDomain Microsoft.ActiveDirectory.Management.ADDomain

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM