SO I wrote a script that gets some information from AD about a user and their associated computer(s). I have read online about a few of the problems people face with the Get-ADComputer cmdlet and some possible bugs with it. At first I thought that was what I had run into here but now I'm not so sure.
My problem is that my code works fine, but if I add an unrelated snippet of code to get some info from a variable, it breaks something else. Take a look.
Entire code: http://paste.ofcode.org/Hgbiukp2XYqKnv2sdUGBKb
The bit of code in question:
## Prompt host for input
$username = Read-Host -Prompt "Enter the Username"
## Get list of computers
$ComputerList = Get-ADComputer -Filter {ManagedBy -eq $username} -Properties ManagedBy | Select-Object -ExpandProperty Name
## Compute and format results
Foreach ($Computer in $ComputerList)
{
$OnlineStatus = Test-Connection -ComputerName $Computer -BufferSize 16 -Count 1 -Quiet
If ($OnlineStatus -like "True") {$OnlineStatus = "$True"} else {$OnlineStatus = "$False"}
Get-ADComputer -Identity $Computer -Properties ManagedBy,DNSHostName,LastLogonTimestamp |
Select-Object DNSHostName,@{Name="Active";Expression={$OnlineStatus}},@{Name="LastLogonTimestamp";Expression={[datetime]::FromFileTime($_.LastLogonTimestamp)}}
}
So this part works perfect. Now if you add a snippet that gets some info about the username, you can see that it stops displaying the output about the AD computer.
## Prompt host for input
$username = Read-Host -Prompt "Enter the Username"
## Get Username info
Get-ADUser -Identity $username –Properties “DisplayName”, “msDS-UserPasswordExpiryTimeComputed”, "LockedOut" |
Select-Object -Property @{Name="Name";Expression={$_.DisplayName}},@{Name=“PWD Expiration Timestamp”;Expression={[datetime]::FromFileTime($_.“msDS-UserPasswordExpiryTimeComputed”)}},LockedOut
## Get list of computers
$ComputerList = Get-ADComputer -Filter {ManagedBy -eq $username} -Properties ManagedBy | Select-Object -ExpandProperty Name
## Compute and format results
Foreach ($Computer in $ComputerList)
{
$OnlineStatus = Test-Connection -ComputerName $Computer -BufferSize 16 -Count 1 -Quiet
If ($OnlineStatus -like "True") {$OnlineStatus = "$True"} else {$OnlineStatus = "$False"}
Get-ADComputer -Identity $Computer -Properties ManagedBy,DNSHostName,LastLogonTimestamp |
Select-Object DNSHostName,@{Name="Active";Expression={$OnlineStatus}},@{Name="LastLogonTimestamp";Expression={[datetime]::FromFileTime($_.LastLogonTimestamp)}}
}
Can someone tell me why this is? And how to fix it?
Normally I'm able to figure it out just by playing with it but this one really has me stumped. I think the issue must be with the formatting of the $ComputerList
variable but every item in the list is a String, which is what Get-ADComputer
requires. So I just don't know.
Thank you all in advance.