将变量输出(“echo")到文本文件 [英] Output ("echo") a variable to a text file

查看:124
本文介绍了将变量输出(“echo")到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在针对多台服务器运行 PowerShell 脚本,并将输出记录到文本文件中.

I'm running a PowerShell script against many servers, and it is logging output to a text file.

我想捕获当前运行脚本的服务器.到目前为止,我有:

I'd like to capture the server the script is currently running on. So far I have:

$file = "\serversharefile.txt"
$computername = $env:computername

$computername | Add-Content -Path $file

最后一行在输出文件中添加问号.糟糕.

This last line adds question marks in the output file. Oops.

如何在 PowerShell 中将变量输出到文本文件?

How do I output a variable to a text file in PowerShell?

推荐答案

经过反复试验,我发现

$computername = $env:computername

用于获取计算机名称,但通过 $computername 发送到文件rel="noreferrer">添加内容 不起作用.

works to get a computer name, but sending $computername to a file via Add-Content doesn't work.

我也试过$computername.Value.

相反,如果我使用

$computername = get-content env:computername

我可以使用

$computername | Out-File $file

这篇关于将变量输出(“echo")到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆