0

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

4

2 回答 2

0

你有一种非常复杂的方式来获得你想要的东西。为了尽量减少Get-ADUser调用次数,只需使用一个变量。您已经在开始时分配了一个变量,但无论出于何种原因,您都“丢弃”了该对象。

在您的代码Select-Object SamAccountName -InputObject $Name中似乎没有意义。你从不展示你分配的$Name内容以及你在它之前所做的事情,这看起来很奇怪。因此,我在下面的代码中删除了它。

$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

$niceDisplay = $user |
                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';} | Format-list | Out-String

$name = $User.SamAccountName
于 2019-02-19T07:02:54.200 回答
0

最好的做法是将任何一段代码变成一个可重用的函数,然后将多个函数组合成模块。由于您是从 powershell 开始的,因此您可以从一个简单的函数开始,如下所示:

Function Get-ADUserInfo {
   param([string]$NameID)

   Get-ADUser -Filter "SamAccountName -eq '$NameID'" -Properties Enabled,LockedOut,Mail,Created,passwordlastset,Description,PasswordExpired,LastLogonDate,EmployeeID,DisplayName,Manager,"msRTCSIP-UserEnabled","msDS-UserPasswordExpiryTimeComputed","extensionAttribute7",telephonenumber,targetaddress,distinguishedName |
                Select-Object @{Expression={$_.Name};Label='User Name'},
                SamAccountName

    }

# call the function with different nameid values like so
$Name = Get-ADUserInfo -NameID someuser1
$Name = Get-ADUserInfo -NameID someuser2

经理信息

Get-ADuser -Identity $Name.Manager

顾名思义,Inputobject 用于将对象作为输入传递给函数。该属性samaccountname已经存在于输出中,因此除了按照上面的代码所示指定它之外,无需执行任何其他操作。

还有为什么选择将“名称”显示为“用户名”?在excel中格式化标题会更容易吗?

以下是一些可能有用的链接:

功能

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-5.1

于 2019-02-19T04:52:57.067 回答