My PowerShell script:
$result = foreach ($user in $list) {
$samname = $user.SamAccountName
$manager = $user.Manager
$name = $user.Name
$managername = Get-ADUser -Filter "Name -eq '$manager'" | Select Name
if ($managername.Name -eq $manager) {
Write-Output "$manager exists in AD."
$dname = Get-ADUser -Filter "Name -eq '$manager'" | Select DistinguishedName
Get-ADUser -Filter "SamAccountName -eq '$samname'" | Set-ADUser -Manager $dname
}
else {
Write-Warning "$manager does not exist in AD."
}
}
$result | Out-File -FilePath C:\testfile.txt -Width 500
$manager = $user.Manager
is a list of names from a file.
If $manager
exists in AD, there will be a write-output "$manager exists in AD."
In order to find $manager
in AD, I created the variable $managername
, and wrote an if ($managername.Name -eq $manager)
.
I am able to produce the write-warning in the console, but not write-output. However, in the txt file, it is the opposite. The write-output if message is listed in the txt file, but not the write-warning else message.
What do I write to produce both if and else messages in the console and also the txt file?
Update: Based on the suggestions given, I tried:
I removed $result, replaced Write-Output and Write-Warning, and tested with both Write-Host, then Write-Information. It did not manage to send any message to the txt file:
foreach ($user in $list) {
$samname = $user.SamAccountName
$manager = $user.Manager
$name = $user.Name
$managername = Get-ADUser -Filter "Name -eq '$manager'" | Select Name
if ($managername.Name -eq $manager) {
$if = Write-Host "$manager exists in AD."
$dname = Get-ADUser -Filter "Name -eq '$manager'" | Select DistinguishedName
Get-ADUser -Filter "SamAccountName -eq '$samname'" | Set-ADUser -Manager $dname
}
else {
$else = Write-Host "$manager does not exist in AD."
}
}
$if, $else | Out-File -FilePath C:\testfile.txt -Width 500
Appending both if and else write-output messages to a variable shows up in the console, but nothing appeared in the txt file.
foreach ($user in $list) {
$samname = $user.SamAccountName
$manager = $user.Manager
$name = $user.Name
$managername = Get-ADUser -Filter "Name -eq '$manager'" | Select Name
if ($managername.Name -eq $manager) {
$if = Write-Output "$manager exists in AD."
$dname = Get-ADUser -Filter "Name -eq '$manager'" | Select DistinguishedName
Get-ADUser -Filter "SamAccountName -eq '$samname'" | Set-ADUser -Manager $dname
}
else {
$else = Write-Output "$manager does not exist in AD."
}
}
$if, $else | Out-File -FilePath C:\testfile.txt -Width 500
Both messages with write-output only will have both if and else messages in the console. How do I output the messages to a txt file though?
foreach ($user in $list) {
$samname = $user.SamAccountName
$manager = $user.Manager
$name = $user.Name
$managername = Get-ADUser -Filter "Name -eq '$manager'" | Select Name
if ($managername.Name -eq $manager) {
Write-Output "$manager exists in AD."
$dname = Get-ADUser -Filter "Name -eq '$manager'" | Select DistinguishedName
Get-ADUser -Filter "SamAccountName -eq '$samname'" | Set-ADUser -Manager $dname
}
else {
Write-Output "$manager does not exist in AD."
}
}