작동하지 않는 기능에 변수를 공급 및 문자열을 "안녕하세요"를 포함하는 파일을, 그 다음 결과에 대한 HTML 파일을 인쇄 :의 Windows PowerShell - 반환하고 ... 나는 임 희망 누군가가 도움이 할 수있는 문제가 내가 재귀 적으로 사용자 메뉴가 다음 코드는 텍스트 파일을 검색 한</p> <p>을
Foreach ($Target in $Targets){ #ip address from the text file supplied
Function gettextfiles {
Write-Output "Collating Detail for $Target"
$Results = Get-ChildItem -Path $Target -Recurse -Include *.txt
Write-Output "output from recursively searching for text files $Results"
$MyReport = Get-CustomHTML "$Target Audit"
$MyReport += Get-CustomHeader0 "$Target Details"
$MyReport += Get-CustomHeader "2" "Text files found"
foreach ($file in $Results) {
$MyReport += Get-HTMLDetail "Path to the file" ($file)
}
$MyReport += Get-CustomHeaderClose
return $MyReport
}
Function gethello {
$Results = Get-ChildItem -Path $Target -Recurse | Select-String -pattern hello | group path | select -ExpandProperty name
Write-Output "output from recursively looking for the string hello $Results"
$MyReport += Get-CustomHeader "2" "Hello Strings Found"
foreach ($file in $Results) {
$MyReport += Get-HTMLDetail "Path to the file" ($file)
}
$MyReport += Get-CustomHeaderClose
return $MyReport
}
####################################################################
# To create the html document from the data gathered in the above functions
Function printeverything([string]$MyReport) {
$Date = Get-Date
$Filename = "C:\Desktop" + "_" + $date.Hour + $date.Minute + "_" + $Date.Day + "-" + $Date.Month + "-" + $Date.Year + $Date.Second + ".htm"
$MyReport | out-file -encoding ASCII -filepath $Filename
Write "HTML file saved as $Filename"
}
###################################################################
User input menu, call the functions the user chooses then when they press 3 creates the html file
do {
[int]$xMenuChoiceA = 0
while ($xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 4){
Write-host "1. Get Text Files"
Write-host "2. Get Files With The String Hello"
[Int]$xMenuChoiceA = read-host "Please enter an option 1 to 4..." }
Switch($xMenuChoiceA){
1{gettextfiles}
2{gethello}
3{printeverything "$MyReport"}
default{<#run a default action or call a function here #>}
}
} while ($xMenuChoiceA -ne 4)
} #ending bracket of Targets foreach
문제 나는 데 :
내가 성공적으로 찾을 수있는 스크립트를 실행할 수있는 사용자 메뉴를 사용하여 텍스트 f iles 문자열을 포함하는 파일을 찾으려면 html 파일에 대해 html을 생성하는 함수를 사용하여 찾은 결과가 $MyReport
에 추가됩니다. < - 나는 그것이 나를 위해 HTML 파일을 생성 할 수 있도록 다음 $MyReport
변수로 printeverything
함수를 호출 할 때이 모든 workds 완벽하게
그러나, 그것은 작동하지 않습니다.
다음은 HTML 파일을 생성하는 코드 내가 테스트 한대로 perefectly 작동 나는 문제가 $MyReport
변수가 올바르게 printeverything
함수에 전달되고 있지하지만 난하지 운동 내가 잘못
나는이가 많이 주시면 감사하겠습니다으로 당신의 도움이 PowerShell을 위해 새로운 오전으로 , 감사 당신의 do-while
찾는
나는 당신이 제공 한 링크를 시도해보아야합니다. 당신은이 솔루션이 이상적이지 않다고 말했습니까? 그렇다면 이것을 수행하는 이상적인 방법은 무엇입니까? 귀하의 도움을 주셔서 감사합니다 –
@ perl-user - 이상적인 방법은 함수에서 적절한 범위와 값을 반환하고 로컬 범위의 변수에 반환 값을 할당하고 다른 함수 등에 전달하여 작업하는 것입니다. – manojlds
예 I 동의하고, 내가 성취하려고하는 것을 말한다. 나는 그런 방식으로 여러 번 해보려고했지만 아직 운이 없다. 문법적으로 내가 잘못했는지는 모르지만 일을 할 수는 없다. (필자는 Perl에서는 쉽게 그런 일을 할 수 있지만 powershell에서는 문제가있다.) 이게 어떻게 올바르게 수행 될지 예를 보여줄 수 있습니까, 도움 주셔서 감사합니다. –