Powershell Write-Host 附加到文本文件 - 计算机名称和时间戳

标签 powershell

我是一个 Powershell noobie,我目前正在编写我的第二个脚本,所以请耐心等待。我正在尝试做一个 write-host并将我的写主机消息连同时间戳以及哪台计算机正在完成我的各种脚本块一起输出到文本文件中,并且我在使用哪种语法使事情正常工作方面遇到了问题。

出于测试目的,我尝试了以下操作。一旦我得到语法,这将转到服务器 - 现在它转到我的本地 C:驾驶。

write-host "folders created successfully $env:computername" >> c:\scripts\testlog.txt

我看到的行为是正在创建文本文件,但其中没有内容 - 都是空白的。

最佳答案

使用 Start-Transcript cmdlet - 它可以捕获 Write-Host 输出,如下所示:

Start-Transcript -Path .\testlog.txt
Write-Host "Hello World"
Stop-Transcript

关于Powershell Write-Host 附加到文本文件 - 计算机名称和时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28812239/

相关文章:

powershell - 删除文本文件中包含特定字符的行

python - 用于检查 Python 是否已安装的 Powershell 脚本

r - 尝试将 R.exe 添加到 PATH

powershell - 调试在 Visual Studio Code 中突然停止

powershell - net use */delete/y 不能解决错误 "New-PSDrive : Multiple connections to a server ...."

azure - 尝试创建 Blob 容器时,无法将 'System.Object[]' 转换为参数 'System.String' 所需的类型 'StorageAccountKey'

powershell - 尝试在 powershell 中显示 gif

powershell - 在 PowerShell 脚本中使用 secret 变量的正确方法

powershell - Azure PowerShell - 设置-AzureService : ResourceNotFound: The hosted service does not exist

powershell - 显示编码的电子邮件主题