2016-08-31 2 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

안녕하세요. 귀하의 의견을 보내 주셔서 감사합니다. 나는이 모든 것에 대해 상당히 새로운 것이므로 실제로 스크립트를 편집하여 그 작업을 수행하는 방법을 모르겠습니다. 사과. – jakeythehobo

+1

실제로 CSV 파일에 헤더를 포함 할 필요는 없습니다. 커스텀 인수를'-Header' 인수로'Import-Csv'에 사용할 수 있습니다. – Joey

+0

@ 조이 덕분에 나는 결코 알지 못했습니다 –

관련 문제