Simple PS script but dirty output

2019-08-26 12:07发布

问题:

I'm having an issue with a script I'm writing. The code works but it's not spitting out the info how I'd like to see it. Basically, it spits it out with a few carriage returns between the final two commands. I also tried to dump the Get-ADUser command into an array and then add the data from the $Pass into said array but that failed miserably and I could never get that to work; every which way I tried to write and execute it, would come up with errors. Am I on the right track or is there a better way to do this? If this is the way it should be done, how can I get it cleaned up the way I'd like it?

$User = Read-Host "Enter the UserName you wish to look up."

$UserInfo = Get-ADUser $User -Properties * | Select-Object -Property DisplayName, GivenName, Initials, Surname, SamAccountName, @{Name = 'Student ID'; Expression = {$_.EmployeeID}}

$Pass = '{0}{1}{2}#{3}' -f @(
            $UserInfo.GivenName[0].ToString().ToUpper(),
            $UserInfo.Initials[0].ToString().ToLower(),
            $UserInfo.Surname[0].ToString().ToLower(),
            $UserInfo.'Student ID')

$UserInfo | fl
Write-host "Password       : $Pass"

回答1:

You can add the $Pass as a property just like you do with the Student Id property.

$User = Read-Host "Enter the UserName you wish to look up."

$UserInfo = Get-ADUser $User -Properties * | Select-Object -Property DisplayName, GivenName, Initials, Surname, SamAccountName, @{Name = 'Student ID'; Expression = {$_.EmployeeID}}

$Pass = '{0}{1}{2}#{3}' -f @(
            $UserInfo.GivenName[0].ToString().ToUpper(),
            $UserInfo.Initials[0].ToString().ToLower(),
            $UserInfo.Surname[0].ToString().ToLower(),
            $UserInfo.'Student ID')

$UserInfo | Select *,@{l='Password';e={$Pass}} | fl

For that matter you could do it all inline...

Read-Host "Enter the UserName you wish to look up." | Get-ADUser -Properties * | Select-Object -Property DisplayName, GivenName, Initials, Surname, SamAccountName, @{Name = 'Student ID'; Expression = {$_.EmployeeID}},@{l='Password';e={'{0}{1}{2}#{3}' -f $_.GivenName[0].ToString().ToUpper(), $_.Initials[0].ToString().ToLower(), $_.Surname[0].ToString().ToLower(), $_.'Student ID')}} | fl

But that gets to be a really long line and hard to read.

To explain the line $UserInfo | Select *,@{l='Password';e={$Pass}} | fl, it's fairly simple. I pass the ADUser object to Select-Object, where I select all of the properties that it has by using the * wildcard, and also add a calculated property. I do use some shorter aliases, but in essence it is a hashtable where you define the name/label of the property being defined, and an expression that defines the value of it. Fully it could be:

@{
    label = 'Password'
    expression = {$Pass}
}

Since it is looking for 'label' (or 'name') as a parameter for an argument, and 'expression' for another we can shorten those to simply 'l' and 'e' as the first letter of each, which shortens the entire thing down to what you see in my code.



回答2:

I changed the bit of code that uses the format operator to compensate for the lack of a middle name/initial as noted in one of my comments. I hope it helps someone, should they ever need it.

# This attempts to format the password according to our convention.
IF([String]::IsNullOrEmpty($UserInfo.Initials)) {
    $Pass = '{0}{1}#{2}' -f @(
            $UserInfo.GivenName[0].ToString().ToUpper(),
            $UserInfo.Surname[0].ToString().ToLower(),
            $UserInfo.'Student ID')
    } Else {
    $Pass = '{0}{1}{2}#{3}' -f @(
            $UserInfo.GivenName[0].ToString().ToUpper(),
            $UserInfo.Initials[0].ToString().ToLower(),
            $UserInfo.Surname[0].ToString().ToLower(),
            $UserInfo.'Student ID')
    }