2016-08-31 15 views
1

URLを監視するスクリプトを作成しようとしています。私は、私が欲しい情報を得ることができた、とこのPowerShellで変数出力の名前を変更する

$logfile = "C:\LogFileTest.log" 
Function WriteHeader-Websites { 
    [cmdletbinding()] 
    param (
     [string]$URL 
    ) 
    if ($logfile -eq "nologfile"){ 
     write-host 
     write-host "$URL" 
     write-host 
    } 
    else { 
     add-content -path $logfile -value "" 
     add-content -path $logfile -value "$URL" 
     add-content -path $logfile -value "" 
    } 
} 

Function Test-Websites { 
$URLs = Get-Content -Path C:\WebsiteChecks.txt 
foreach ($URL in $URLs) { 
$ArrayLine =$Line.split(",") 
    $ 
    $Line.name = 
    try { 
     $request = [System.Net.WebRequest]::Create($URL) 
     $response = $request.GetResponse() 
     WriteHeader-Websites "$URL is available!" 
    } catch { 
     WriteHeader-Websites ("$URL failed! The error message was '{0}'." -f $_) 
    } finally { 
      if ($response) { 
       $response.Close() 
       Remove-Variable response 
      } 
     } 
    } 
} 

Test-Websites 

私のテキストファイルは次のようになりますように私のスクリプトに見えます:

http://www.google.com 
http://www.bing.com 
http://www.bbc.co.uk 

、スクリプトからの出力は次のようになります。

http://www.google.com is available! 

ウェブサイトの短い名前をテキストファイルに追加して、代わりにその名前を使用したいと考えています。

テキストファイルの例は次のようになります。

http://www.google.com,Google Website 
http://www.bing.com,Bing Website 

と私はリターンがどのように見えるしたいと思います:

Google Website is available! 
Bing Website is available! 

または

Google Website failed! Error message is... 

しかし、私はありません持っていますそれをどうやって行うのか、Googleがそれを見つけ出すにはどうすればいいのだろうか。助言がありますか?

おかげ

答えて

0

ヘッダを持つCSVファイルを作成することですこれを行う最も簡単な方法は、あなたが自分のヘッダーを使用して列を参照することができます。

Import-Csv -Path C:\WebsiteChecks.txt | % { 
    $_.description 
    $_.url 
} 

更新 ここで私はそれ自身の中に説明を入れImport-Csv通知を使用して書き換えられ、あなたのコードは次のとおりです。あなたはこのような何かを行うことができるように

url,description 
http://www.google.com,Google Website 
http://www.bing.com,Bing Website 

次に、ファイルをインポートするImport-Csvを使用変数$ _はcatch文の例外によって更新されます。

$logfile = "C:\LogFileTest.log" 

Function WriteHeader-Websites { 
    [cmdletbinding()] 
    param (
     [string]$URL 
    ) 
    if ($logfile -eq "nologfile"){ 
     write-host 
     write-host "$URL" 
     write-host 
    } 
    else { 
     add-content -path $logfile -value "" 
     add-content -path $logfile -value "$URL" 
     add-content -path $logfile -value "" 
    } 
} 

Function Test-Websites { 

    Import-Csv -Path C:\WebsiteChecks.txt | % { 

    $url = $_.url 
    $description = $_.description 

    try { 
     $request = [System.Net.WebRequest]::Create($url) 
     $response = $request.GetResponse() 
     WriteHeader-Websites "$description is available!" 
    } catch { 
     WriteHeader-Websites ("$description failed! The error message was '{0}'." -f $_) 
    } finally { 
      if ($response) { 
       $response.Close() 
       Remove-Variable response 
      } 
     } 
    } 
} 

Test-Websites 
+0

こんにちはDavid。あなたのご意見ありがとうございます。私はこれにかなり新しいので、実際にスクリプトを編集してその作業を行う方法がわからないのですか?謝罪。 – jakeythehobo

+1

実際にCSVファイルにヘッダーを含める必要はありません。 'Import-Csv'の' -Header'引数でカスタムのものを使うことができます。 – Joey

+0

@ジョーのおかげで私はそれを知らなかった –

関連する問題