Reputation: 1
I am trying to get the computers distinguished name, set it into a variable $dname
Then I can use the disable computer command disable-adaccount -Identity $dname
$hostname
is set OK as "WIN10TEST1"
When using this command
$dname = (Get-ADcomputer -Identity "$hostname" | select DistinguishedName | ft -hide)
It fails like this:
disable-adaccount -Identity $dname
Disable-ADAccount : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADAccount' required by parameter 'Identity'. Specified method is not supported. At line:1 char:29 + disable-adaccount -Identity $dname + ~~~~~~ + CategoryInfo : InvalidArgument: (:) [Disable-ADAccount], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.DisableADAccount
If I set the variable manually
$dname=CN=WIN10TEST1,OU=Workstations,DC=somedomain,DC=local
disable-adaccount -Identity $dname
It works OK so its a problem with the variable coming in.
When examining the returned data in $dname
it comes back with a cr/lf
before and after the data I need. I think this is tripping it up.
$hostname = "WIN10TEST1"
$dname = (Get-ADcomputer -Identity "$hostname" | select DistinguishedName | ft -hide)
disable-adaccount -Identity $dname
Note: Check $dname
.....it returns with cr/lf
before/after data
I expect to get the distinquishedname
string with no other data
CN=WIN10TEST1,OU=Workstations,DC=somedomain,DC=local
Upvotes: 0
Views: 1174
Reputation: 13473
As @Lee_Dailey mentions, there is no need to use Select, or Format Table on anything because they are meant for display only, and will mess with your data in unintended ways if you save that formatted data to a variable.
With Active Directory PowerShell module you can simply pass the Computer object to Disable-ADAccount, and it knows how to identify and handle it:
$computer = Get-ADcomputer -Identity "$hostname"
Disable-ADAccount -Identity $computer
Upvotes: 1