问题
I'm writing a PowerShell script and one of its parts should add a new user to Active Directory.
I'm using the New-ADUser
cmdlet and I want to redirect any errors (if it will produce any) to a file.
So I write
New-ADUser -smth -smth 2>> ./log.txt
But it creates only a blank log.txt
file when I simulate error.
What did I do wrong?
回答1:
Errors that are thrown by the cmdlet itself can be redirected like you tried:
New-Item -Type File C:\Windows\WinSxS\foo.txt 2>> error.log
Errors thrown by the environment (parser errors for instance) can only be redirected if you run the cmdlet in a nested context, e.g. a scriptblock:
& { New-Item -Foo } 2>> error.log
来源:https://stackoverflow.com/questions/34162216/redirect-error-to-file