Powershell Write-Host 附加到文本文件 - 计算机名称和时间戳 [英] Powershell Write-Host append to text file - computer name and time stamp
问题描述
我是一个 Powershell 新手,我目前正在编写我的第二个脚本,所以请耐心等待.我正在尝试执行 write-host
并输出我的 write-host 消息以及时间戳,以及哪台计算机正在将我的各种脚本块完成到一个文本文件中,但我在使用哪种语法时遇到了问题让事情发挥作用.
I am a Powershell noobie and I am currently writing my second script so bear with me. I am trying to do a write-host
and output my write-host message along with a time stamp and what computer is completing my various script blocks to a text file and I am having issues with which syntax to make things work.
为了测试目的,我尝试了以下方法.一旦我得到语法,这将转到服务器 - 现在它转到我的本地 C:
驱动器.
I tried the following for testing purposes. This will be going to a server once I get the Syntax down - for now it goes to my local C:
drive.
write-host "folders created successfully $env:computername" >> c:\scripts\testlog.txt
我看到的行为是正在创建文本文件,但其中没有内容 - 全是空白.
The behavior I'm seeing is the text file is being created but there's no contents in it - all blank.
推荐答案
使用 Start-Transcript cmdlet - 它可以捕获 Write-Host 输出,如下所示:
Use the Start-Transcript cmdlet - it can capture Write-Host output as follows:
Start-Transcript -Path .\testlog.txt
Write-Host "Hello World"
Stop-Transcript
这篇关于Powershell Write-Host 附加到文本文件 - 计算机名称和时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!