Hello fellow sysadmins,
Trying to display an ADUsername tied to a hostname specified in a txt file. I get the following error shown in my CSV when running the script: System.Management.Automation.ParameterBindingValidationException: Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Supply an argument that is not null or empty and then try the command again. —> CODE:
import-module activedirectory $computers = Get-Content -Path “c:computers.txt”
$Results = ForEach ($computer in $computers) { $object = New-Object -TypeName PSObject -Property @{ “Name”=”$($computer.name)” “Username”=”” “Error”=”” } Try { $object.username = (Get-WmiObject -Class “Win32ComputerSystem” -ComputerName “$($computer.name)” -ErrorAction Stop).UserName } Catch { $object.error = $.exception } $object } $Results | Select Name,Username,Error | Export-CSV -Path C:export.csv -NoTypeInformation
submitted by /u/ITGuy420
[link] [comments]
The post Attempting to display ADUsername based on hostname file appeared first on How to Code .NET.