首先,我对Powershell和脚本都很陌生,总的来说,你的耐心非常感谢。
我有一个脚本,将用户输入的值(完全限定的域名)写入名为“servers.txt”的文档。相反,我想附加位于不同位置的“servers.txt”的几个副本。不幸的是,只有一个“servers.txt”文件不是一个选项,访问各种“servers.txt”文件的各种脚本有时需要一个具有唯一数据的独立文件,但每隔几个月我就必须使它们统一(或者对于预定的事件,至少包含所有条目。
我检查了out-help for out-file并且没有看到多个输出的引用。我用谷歌搜索了一段时间,但似乎很多人不想做我正在尝试的事情(或者我正在做一个讨论问题的糟糕工作)。
这是脚本:
# This variable exists to ensure the loop continues forever.
$yes="y"
#Information for the user
ECHO "This script adds your input to every servers.txt file. You may press "VIEW" at any time to see what currently exists in a servers.txt file"
#Loop. The script requests the FQDN or the command "exit" or "view". Exit ends the script, VIEW shows the current content of servers.txt. If an FQDN is tested for positively, it will append the user's value to servers.txt
do {
$a = read-host "Please Input Server FQDN here, or type EXIT to end the loop"
#test for command to quit (exit)
if ($a -eq "exit")
{
exit
}
#test for command to view contents of servers.txt
if ($a -eq "view")
{
get-content servers.txt
}
#if not exit or view, check if this is a proper FQDN
else
{
IF ($a -match "^[a-zA-Z0-9]+([a-zA-Z0-9\-\.]+)?\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|internal)$")
#If test for FQDN is positive append value to servers.txt
{
echo $a | out-file "servers.txt" -append
}
#If test fails, return error
ELSE
{echo "Entry is Invalid"}
}
}
#continue looping forever because YES always is equal to Y
while ($yes -eq "y")
答案 0 :(得分:3)
尝试添加内容:
"TESTING" | Add-Content -Path "C:\temp\test1.txt","C:\temp\test2.txt"