首先,我对 Powershell 和脚本非常陌生,一般来说,非常感谢您的耐心等待。
我有一个脚本,将用户输入的值(完全限定域名)写入名为“servers.txt”的文档。相反,我想附加到位于不同位置的多个“servers.txt”副本。不幸的是,只有一个“servers.txt”文件不是一种选择,访问各种“servers.txt”文件的各种脚本有时需要一个具有唯一数据的独立文件,但每隔几个月我必须使它们统一(或至少都包含某个条目)用于预定事件。
我检查了 out-file 的 get-help 并没有看到对多个输出的引用。我用谷歌搜索了一段时间,但似乎没有多少人想做我正在尝试的事情(或者我在问这个问题时做得很糟糕)。
这是脚本:
# 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")