Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net User in PowerShell

I am in the middle of moving to the cloud, migrating from SBS 2003 Active Directory, to 2008 R2.

I configured a new user, and noticed that the user was unable to reset their password.

My Server Admin showed me how to use net user.

I noticed that I can obtain information from some accounts and not others. With over 100 accounts to process, I thought I'd try PowerShell.

In this post (Use powershell to look up 'net user' on other domains?) Lorenzo recommends using Get-ADUser (and this applies to polling from another domain). When I run Get-ADUser from my PowerShell prompt, I receive a message stating that the commandlet is not recognized.

I am reading the user IDs from a text file, and sending the output to a log file so that I can send to the server admin for further analysis.

Here is my code so far (please note that I am completely new to PowerShell):

# Get our list of user names from the local staff.txt file
$users = get-content 'C:\Scripts\staff.txt'

# Create log file of output:
$LogTime = Get-Date -Format 'MM-dd-yyyy_hh-mm-ss'
$CompPath = "C:\Scripts\"
$CompLog = $CompPath + "NetUserInfo" + $LogTime + ".txt"
New-Item -path $CompLog -type File

foreach ($user in $users) {

#Testing user:
"Testing user: $user" | out-file $CompLog -Append

# Obtain user information using net user:
net user $user /domain >> $CompLog

# Pause to let system gather information:
Start-Sleep -Second 15

}

As the script runs currently, my log file will have two or three user names followed by the response "The request will be processed at a domain controller for domain (domain)"

If net user, from CMD, would return "System error 5 has occurred, Access is denied." This is not logged in the output file. IF net user, from CMD, would return user information, this is logged to the output file. I am currently receiving output for only a couple users, but when I run the command from CMD, I am able to retrieve information for at least ten.

My first thought was that I needed to wait for the net user command to complete (hence the Start-Sleep command) but that has not had any effect on the output.

Any assistance would be greatly appreciated.

like image 562
JBnAZ Avatar asked Aug 23 '26 19:08

JBnAZ


1 Answers

The reason the error output is not being appended to the log file is because you are only redirecting the STDOUT (standard output stream). To also redirect the STDERR (standard error stream) change

net user $user /domain >> $CompLog

to

net user $user /domain 2>&1 $CompLog

This explains it a bit more: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_Pipes_and_Redirection#Windows_PowerShell_Redirection_Operators

like image 119
user2887442 Avatar answered Aug 26 '26 22:08

user2887442



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!