Redirect error to file

十年热恋 提交于 2020-01-06 15:29:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!