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

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

问题描述

我对许多服务器运行PowerShell脚本,并将输出记录到文本文件。



我想捕获脚本所在的服务器目前正在运行。到目前为止,我有:

  $ file =\\server\share\file.txt
$ computername = $ env:computername

$ computername | Add-Content -Path $ file

最后一行在输出文件中添加了问号。哎呀。

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

解决方案

经过一些试验和错误之后,我发现

$ $ $ $ $ $ $ $ computername = $ env:computername

可以得到一个计算机名称,但是将 $ computername 发送给通过添加内容的文件不起作用。



我也试过 $ computername.Value



我使用

  $ computername = get-content env:computername 

$ b

我可以使用

  $ computername | Out-File $ file 


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 = "\\server\share\file.txt"
$computername = $env:computername

$computername | Add-Content -Path $file

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

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

解决方案

After some trial and error, I found that

$computername = $env:computername

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

I also tried $computername.Value.

Instead, if I use

$computername = get-content env:computername

I can send it to a text file using

$computername | Out-File $file

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

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