Question Details

No question body available.

Tags

powershell

Answers (2)

Accepted Answer Available
Accepted Answer
March 5, 2026 Score: 2 Rep: 406 Quality: High Completeness: 80%

Can't test it at the moment, but I strongly suspect that $account.psbase.invoke("setpassword", $newPassword) returns $null, since you're not assigning or voiding it. $account is a DirectoryEntry object, which can behave strangely. When that $null hits Export-Csv, the cmdlet doesn't know what to do with it.
That aside, your output objects have inconsistent properties, and you're referencing an undefined variable $Status.
There's also no need to store the results; use the ForEach-Object cmdlet and pipe the results directly to Export-Csv.
Try something like this:

$inFile = "g:\computers.txt"
$outFile = "g:\password.csv"

Function Get-RandomPassword { Param ( [Parameter(Mandatory)] [int]$Length, [int]$AmountOfNonAlphanumeric = 1 ) Add-Type -AssemblyName 'System.Web' [System.Web.Security.Membership]::GeneratePassword($Length, $AmountOfNonAlphanumeric) }

Get-Content -Path $inFile -ErrorAction Stop | ForEach-Object { $computer = $ Write-Host "Processing $($computer)" $result = [PSCustomObject]@{ ComputerName = $computer Status = 'OFFLINE' Error = $null NewCredentials = $null } If ((Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue -ErrorVariable testError)) { Try { $newPassword = Get-RandomPassword -Length 12 $account = [ADSI]("WinNT://$($computer)/Administrator") [void]$account.psbase.Invoke('setpassword', $newPassword)

$result.Status = 'OK' $result.NewCredentials = $newPassword } Catch { $result.Status = 'ERROR' $result.Error = $.Exception.Message } } Else { # If Count in Test-Connection will always remain at 1, a simple "$result.Error = $testError[0].Exception.Message" will do. # This version will also handle multiple pings: $result.Error = ($testError | ForEach-Object {$_.Exception.Message} | Select-Object -Unique) -join '; ' } $result } | Export-Csv -Path $outFile -NoTypeInformation

Edit: COM object will now be released.
Edit2: $account is not a COM object after all (but DirectoryEntries are not a lot better) ...

March 5, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 50%

The error happens because $passwordResetReport is $null.

If the foreach loop does not output any objects, the assignment

$passwordResetReport = foreach ($Computer in $Computers) { ... }

results in $passwordResetReport being $null. When $null is piped to Export-Csv, PowerShell throws:

Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.