POWERSHELL GET-ADUSER在变量中显示错误?

问题描述

Import-Module ActiveDirectory  

$OutputEncoding = [Console]::OutputEncoding

#************************* Initiate Variable Empty ********************************
$var1="User,expiration date,departament,city,Manager `r`n" 

#*********** Variable $Accounts will query SearchAccount with the Filters below  *****************                        
$accounts = Search-ADAccount -SearchBase "OU=OUUsersguest,OU=OUMex23,DC=ferreronet,DC=com" -AccountExpiring -TimeSpan (New-TimeSpan -Days 15)

#*****Function Query will Initiate "Foreach" in every results and Filter as the Attributes below.****

$Tables = ForEach($account in $accounts) {
    $mgr = (Get-ADUser $account -Properties * ) | Select-Object @{n =" ";e={get-aduser $_.manager | select -ExpandProperty name }}
    $department = (Get-Aduser $account -Properties *).department 
    $city = (Get-Aduser $account -Properties *).city
    $var1 = "{0}{1},{2},{3},{4},{5}`r`n" -f $var1,$account.name,$account.AccountExpirationDate,$department,$city,$mgr
    }
     Write-Output $var1 

我得到这个结果: 约翰·史密斯(John Smith),8/26/2020 12:00:00,纽约,销售,@ {=雷蒙德·格雷} 我的疑问是为什么我得到@ {= managername}我只想要显示名称而不是其他字符

解决方法

您不应尝试创建这样的逗号定界结果,而应使用import math import matplotlib.pyplot as plt from random import randint class XYZ: @property def X(self): return self._X @X.setter def X(self,X): self._X = X @property def Y(self): return self._Y @Y.setter def Y(self,Y): self._X = Y def __init__(self,x,y): self._X = x self._Y = y def __add__(self,other): x = self.X + other.X y = self.Y + other.Y return XYZ(x,y) def __sub__(self,other): x = self.X - other.X y = self.Y - other.Y return XYZ(x,y) def ccw(self,other): return (self.X*other.Y - self.Y*other.X) > 0 ### Rand Points points = [] for i in range(100): points.append(XYZ(randint(0,100),randint(0,100))) ### Static Points # points.append(XYZ(0,0)) # points.append(XYZ(0,1)) # points.append(XYZ(15,6)) # points.append(XYZ(2,2)) # points.append(XYZ(0,8)) # points.append(XYZ(12,16)) # points.append(XYZ(12,2)) # points.append(XYZ(8,1)) for i,point in enumerate(points): plt.scatter([point.X],[point.Y]) points.sort(key=lambda x: x.X) points.sort(key=lambda x: x.Y) p0 = points.pop(0) points.sort(key=lambda x: math.atan2((x-p0).Y,(x-p0).X)) stack = [p0] stack.append(points.pop(0)) stack.append(points.pop(0)) while len(points) > 0: vector1 = stack[-1] - stack[-2] vector2 = points[0] - stack[-1] if not vector1.ccw(vector2): stack.pop(-1) else: stack.append(points.pop(0)) for i,point in enumerate(stack): plt.plot([point.X,stack[i-1].X],[point.Y,stack[i-1].Y]) plt.show() 创建一个可用文件供您在Excel中打开。
为此,请让循环输出具有所需属性的 objects (而不是字符串),并在变量中捕获这些对象。

此外,由于只需要少数用户属性,因此使用Export-Csv是浪费的,尤其是如果您在同一个帐户中循环执行三次,每次仅获得一个属性。

尝试

-Properties *

我在 Import-Module ActiveDirectory $searchBase = "OU=OUUsersguest,OU=OUMex23,DC=ferreronet,DC=com" $accounts = Search-ADAccount -UsersOnly -SearchBase $searchBase -AccountExpiring -TimeSpan (New-TimeSpan -Days 15) $result = foreach($account in $accounts) { # get the properties you need from the user account $usr = $account | Get-ADUser -Properties Manager,Department,City,AccountExpirationDate $mgr = if ($usr.Manager) { (Get-ADUser -Identity $usr.Manager).Name } else { $null } # output an object with desired properties [PsCustomObject]@{ User = $usr.Name ExpirationDate = $usr.AccountExpirationDate Department = $usr.Department City = $usr.City Manager = $mgr } } # output on screen $result | Format-Table -AutoSize # output to CSV file $result | Export-Csv -Path 'D:\Test\ExpiringUsers.csv' -UseCulture -Encoding UTF8 -NoTypeInformation cmdlet中添加了开关-UseCulture,因此文件将使用Excel期望的定界符