相关疑难解决方法(0)

将变量输出("echo")到文本文件

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

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

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

$computername | Add-Content -Path $file
Run Code Online (Sandbox Code Playgroud)

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

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

powershell file-io

35
推荐指数
3
解决办法
16万
查看次数

如何在powershell中为herestring/heredoc设置编码?

我正在尝试更新 Windows 服务器上的主机文件,并尝试使用 powershell 中的 heredoc 来完成。

我不明白为什么我的结果在每个主机条目中的每个字符之间都有额外的空格。

我正在从 Linux 移植一些脚本。

PS C:\Users\Administrator> cat C:\Users\Administrator\AppData\Local\Temp\etchosts.ps1
@"
127.0.0.1 src.example.com
127.0.0.2 builds.example.com
127.0.0.3 ti.example.com
127.0.0.4 jira.example.com
"@ >>C:\Windows\System32\drivers\etc\hosts



PS C:\Users\Administrator> powershell C:\Users\Administrator\AppData\Local\Temp\etchosts.ps1
PS C:\Users\Administrator> cat C:\Windows\System32\drivers\etc\hosts
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address …
Run Code Online (Sandbox Code Playgroud)

powershell encoding heredoc herestring

3
推荐指数
1
解决办法
1317
查看次数

标签 统计

powershell ×2

encoding ×1

file-io ×1

heredoc ×1

herestring ×1