0

I have a little script to filter out error log for application and system for remote PC in past 7 days.

Write-Host "Creating Error Log of Remote PC ......."
[System.Threading.Thread]::CurrentThread.CurrentCulture = New-Object "System.Globalization.CultureInfo" "en-US"
$date = get-date
$range = $date.adddays(-7)
$pcname = Read-Host -Prompt "Please Enter PC name"

Get-WinEvent -ComputerName $pcname -FilterHashtable @{LogName="system","application"; Level=1,2; starttime=$range} | 
Select-Object @{label="PC"; expression={"$pcname"}},LogName,levelDisplayName,ProviderName,ID,TimeCreated,message | 
Sort-Object logname | 
Format-Table -AutoSize | 
Out-File C:\Eventlog.txt -width 214748

C:\Eventlog.txt

When it runs,I will get a prompt to enter PC name, The thing is that when the script finished, powershell prompt will return to

PS c:\windows\system32\windowspowershell\v1.0>

If i want to check the logs on another PC i have to close the current PS window and run my script again.

How can I do a loop that it will show my [Enter PC name] prompt again after the first run finished?

3 Answers 3

6

You could enclose existing code in a while loop:

while($true){
   Write-Host "Creating Error Log of Remote PC ......."
   ...
   # rest of the code
   ...
   C:\Eventlog.txt
}

This will repeat your code ad infinitum or until you press ctrl+c.

Sign up to request clarification or add additional context in comments.

Comments

1

Enclose in a do..while loop:

$pcname = Read-Host "Enter PC Name"

do {
    Write-Host "Creating Error Log of Remote PC ......."
    [System.Threading.Thread]::CurrentThread.CurrentCulture = New-Object "System.Globalization.CultureInfo" "en-US"
    $date = get-date
    $range = $date.adddays(-7)
    $pcname = Read-Host -Prompt "Please Enter PC name"

    Get-WinEvent -ComputerName $pcname -FilterHashtable @{LogName="system","application"; Level=1,2; starttime=$range} | 
    Select-Object @{label="PC"; expression={"$pcname"}},LogName,levelDisplayName,ProviderName,ID,TimeCreated,message | 
    Sort-Object logname | 
    Format-Table -AutoSize | 
    Out-File C:\Eventlog.txt -width 214748

    C:\Eventlog.txt
    $pcname = Read-Host "Enter PC Name" 

} while($pcname -match '.+')

The while condition checks something was entered, therefore entering a blank string (just pressing Enter at input) will exit the script

Comments

0

an altenative way

# do your stuff

read-host 'press a key to continue'
$file=$myInvocation |select -expand invocationName
start-process -noNewWindow "powershell.exe" "-file $file"

Note that I would not recommand to use this as it will lauch a new process ans consume resource on each iteration.

IMO a better way would be to create a simple menu to ask the user if it wants to run the function another time.

1 Comment

This is a new idea. will try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.