从Powershell中的写入主机输出中删除某些字符(Get-ADuser)



我正在比较两个域之间的用户,以确保在一个域中禁用的用户在另一个域被禁用,使用以下两个步骤:

域1:

Get-ADUser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain1,DC=com" -Filter * -Properties * | Select-Object Name | Export-Csv -encoding "utf8" Users.csv

域2:

$input = import-csv -path "Users.csv" 
ForEach ($User in $input) {
$result = get-aduser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain2,DC=com" -Filter "name -eq '$($User.Name)'" | Select-Object Enabled
If ($result -eq $null) { Write-host -ForegroundColor Yellow $User "Name not found. Please do a manual check" 
}
elseif ($result -like '*False*') 
{ 
Write-host -ForegroundColor Red "**" $User "** must be disabled!" 
}
else {get-aduser -SearchBase "ou=Users,ou=SCS,ou=All,dc=osit,dc=ad" -Filter "name -eq '$($User.Name)'" -Properties * | Select-Object Name, Enabled}
}

这是有效的,但给了我以下输出:

Name                          Enabled
----                          -------
Firstname1 Lastname1             True
@{Name=Firstname2 Lastname2} - Name not found. Please do a manual check

如何删除"@{Name="one_answers"}"?我尝试过在$result中添加-ExtendProperity,但没有成功。我可能做错了。。

$User是一个自定义对象(类型为[pscustomobject],由Import-Csv输出(,而@{Name=Firstname2 Lastname2}是其字符串化表示[1],因为Write-Host字符串化其参数以供显示。

访问.Name属性,只获取名称:

Write-host -ForegroundColor Yellow $User.Name "- Name not found. Please do a manual check" 

更习惯的做法是,使用单个可扩展字符串("..."中的字符串插值(:

Write-host -ForegroundColor Yellow  "$($User.Name) - Name not found. Please do a manual check" 

如果你想包括完整的对象表示,就像你直接打印到控制台时一样,你需要Out-String,但请注意,你最终会得到多行输出:

Write-host -ForegroundColor Yellow  "$($User | Out-String) - Name not found. Please do a manual check" 

[1]您可以按照以下方式验证:$user = [pscustomobject] @{ Name = 'Firstname1 LastName1' }; "$user"。输出为字符串@{Name=Firstname1 LastName1}

最新更新