1

まず第一に、私はPowershellにまったく慣れていないので、スクリプト全般について、ご理解のほどよろしくお願いいたします。

ユーザーが入力した値(完全修飾ドメイン名)を「servers.txt」というドキュメントに書き込むスクリプトがあります。代わりに、別の場所にある「servers.txt」のいくつかのコピーに追加したいと思います。残念ながら、「servers.txt」ファイルを1つだけ持つことはできません。さまざまな「servers.txt」ファイルにアクセスするさまざまなスクリプトで、一意のデータを含む独立したファイルが必要になる場合がありますが、数か月ごとにそれらを統一する必要があります(またはスケジュールされたイベントの場合、少なくともすべてに特定のエントリが含まれます。

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")
4

1 に答える 1

3

Add-Content を試す:

"TESTING" | Add-Content -Path "C:\temp\test1.txt","C:\temp\test2.txt"
于 2012-05-29T19:10:26.440 に答える