带有 >> 的 powershell 写输出到 file.txt -Append [英] powershell Write-Output with >> to a file.txt -Append
问题描述
我正在创建一个脚本并希望同时使用 Write-Host 和 Write-Output在我工作时,我希望从 AD 中提取的信息备份也附加到 .txt 文件中.这更像是一种备份,以防我错过了一条信息并需要返回并重新创建票证.无论如何,我有一个我的脚本样本,形成我可以告诉它应该工作的内容.如果有更多经验的人可以看看或指出我正确的方向,我将不胜感激.如果我需要添加更多脚本,我可以提供.提前致谢.
I am creating a script and want to both use Write-Host and Write-Output As I work I want a backup of information I pull from AD to also become attached to a .txt file. This is more of a backup in case I miss a piece of information and need to go back and recreate a ticket. Anyways I have a sample of my script, form what I can tell it should be working. If someone with a bit more experience can take a look or point me in the right direction I would appreciate it. If I need to add any more of the script I can provide this. Thanks in Advance.
Import-Module activedirectory
$object = Get-ADUser $sid -Properties * | Select-Object EmailAddress
Write-Host Email: $object.EmailAddress
Write-Output ("Email: $object.EmailAddress") >> C:\psoutput\psoutput.txt -Append
这当然会创建 .txt 文件,但也会添加其他信息,例如:
This will create the .txt file of course but is also add other information such as:
Email: @{GivenName=myfirstname; Surname=mylastname; SamAccountName=myid; DisplayName=lastname, firstname - Contingent Worker; City=; EmailAddress=myemailaddress@mywork.com; EmployeeID=; Enabled=True; OfficePhone=; MobilePhone=(555) 555-5555; LockedOut=False; LockOutTime=0; AccountExpirationDate=05/09/2020 00:00:00; PasswordExpired=False; PasswordLastSet=12/03/2019 12:16:37}.EmailAddress
-Append
我希望得到如下输出...
I am looking to have the output like the following...
name: username
email: user email address
phone: user phone number
etc...
来自 Active Directory 的所有一般信息
All general information from Active Directory
再次感谢您的建议
推荐答案
不要使用写输出.使用 (Get-ADUser $sid -properties mail).mail
.像这样:
Don't use write-output. Use (Get-ADUser $sid -properties mail).mail
.
Like this:
Add-Content -Path "FilePath" -Value "Email: $((Get-ADUser $sid -properties mail).mail)"
这篇关于带有 >> 的 powershell 写输出到 file.txt -Append的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!