I've been scrambling around looking for examples of how -inputobject is used, and I cannot seem to find any. I've never touched powershell before, but when I recently started this job, and was told that this is a script we used, I couldn't help but start messing around with it! Pipes are fascinating, but I can't seem to get past this latest issue I have.
I have this huge list of data that comes out when looking up users in AD, and I was wondering if I could also snag the SamAccountName from the same code block!
$User = Get-ADUser -Filter "EmployeeID -eq '$NameID' -or SamAccountName -eq '$NameID' -or DisplayName -eq '$NameID' -or UserPrincipalName -eq '$NameID'" -Properties
Enabled,LockedOut,Mail,Created,passwordlastset,Description,PasswordExpired,LastLogonDate,EmployeeID,DisplayName,"msRTCSIP-UserEnabled",
"msDS-UserPasswordExpiryTimeComputed","extensionAttribute7",telephonenumber,targetaddress,distinguishedName |
Select-Object @{Expression={$_.Name};Label='User Name';},
@{Expression={$_.UserPrincipalName};Label='Logon Name';},
@{Expression={$_.DisplayName};Label='Display Name';},
@{Expression={$_.Created};Label='Date Created';},
@{Expression={$_.SamAccountName};Label='SamAccountName';} -InputObject $Name,
Description,
Enabled,
@{Expression={$_.LockedOut};Label='Locked';},
@{Expression={$_.Mail}; Label='Email Address';},
@{Expression={$_.passwordlastset};Label='PW Last Reset';},
@{Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")};Label='Password Expiry';},
@{Expression={$_.LastLogonDate};Label='Last Logon';},
@{Expression={$_.PasswordExpired};Label='Password Expired';},
@{Expression={$_.extensionAttribute7};Label='Contract Expires On';},
@{Expression={$_.EmployeeID};Label='Employee ID';},
@{Expression={$_."msRTCSIP-UserEnabled"};Label='Skype Enabled';},
@{Expression={$_.telephonenumber};Label='Phone Number';},
@{Expression={$_.targetaddress};Label='Email Forwarded To';},
@{Expression={$_.distinguishedName};Label='Distinguished Name';} | Select-Object SamAccountName -InputObject $Name | Format-list | Out-String
The above is what I use to get most of the interesting information to display nicely in the script, but going forward, I have to call it again with my limited knowledge to simply input a user's SamAccountName into a $Name var(To gather their managers and the like.) It looks something like this:
$Name = (getad-user -Filter "EmployeeID -eq '$NameID' -or SamAccountName -eq '$NameID' -or DisplayName -eq '$NameID' -or UserPrincipalName -eq '$NameID'").SamAccountName
I was just wondering if I could compress it all down into one Get-ADUser, and what the best practice that would be!
Thanks in advance all