PowerShell Script Help needed

I have a PowerShell script that requires two inputs

I have it working sort of  just need some fine tuning from an expert.

I am passing the Event Id error number and then selecting which event log system or application

When the script presents the results I am returned to the beginning of the script
My problem is it does not go back to the beginning it goes to the second question

How can I incorporate the Read-Host and the MainMenu section?

I need to be able to enter the Event ID Error again

Here is my script
Clear-Host
$in = Read-host “Enter the Event Error”
function mainMenu()
{
	Clear-Host;
	Write-Host "====================";
	Write-Host "= Select Event Log =";
	Write-Host "====================";
	Write-Host "1. Press '1' for System Log";
	Write-Host "2. Press '2' for Application Log";
}
function returnMenu($option)
{
	Clear-Host;
	Get-WinEvent -FilterHashtable @{ LogName = "$ev"; id = $in} -maxevents 1 | FL *
	Write-Host "Press any key to return to the main menu.";
	$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
}
do
{
	mainMenu;
	$input = Read-Host "Enter a number for an option or type `"quit`" to finish."
	switch ($input)
	{
		"1"
		{
			$ev = "System";
			returnMenu $input;
		}
		"2"
		{
			$ev = "Application";
			returnMenu $input;
		}
		"quit"
		{
			# nothing
		}
		default
		{
			Clear-Host;
			Write-Host "Invalid input. Please enter a valid option. Press any key to continue.";
			$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
		}
	}
} until ($input -eq "quit");
Clear-Host;

Open in new window



I have gathered different code from searches I have found to get this complete

I hope I am not to far off

Feel free to modify this


Thanks in advance

Tom
LVL 23
Thomas GrassiSystems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
function Pause ($Message="Press any key to continue...")
{
#check for Console or ISE
if ($host.name -eq 'ConsoleHost')
{
    Write-Host -NoNewLine $Message
    $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    Write-Host "" 
}
else {
    #[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") >$null
    [System.Windows.Forms.MessageBox]::Show("Press Enter or Click OK to Continue.")
    }
}

function mainMenu()
{
#	Clear-Host;
#    $in = Read-host “Enter the Event Error”
	Write-Host "====================";
	Write-Host "= Select Event Log =";
	Write-Host "====================";
	Write-Host "1. Press '1' for System Log";
	Write-Host "2. Press '2' for Application Log";
    Write-Host "3. Press 'm' to enter a new Event";
}
function returnMenu($option)
{
#	Clear-Host;
    Get-WinEvent -LogName $ev | Where-Object {$_.Id -eq $id} | fl 
	pause
}
Function main
{
Clear-Host
$in = Read-host “Enter the Event Error or q to quit”
if ($in -eq 'q'){
return
}
else {
do
{
	mainMenu
	$input = Read-Host "Enter a number for an option or m for main menu, q to finish"
	switch ($input)
	{
		"1"
		{
			$ev = "System";
			returnMenu $input;
		}
		"2"
		{
			$ev = "Application";
			returnMenu $input;
		}
		'm'
        {
        main			
		}
		default
		{
		$input = 'quit'
		}
	}
} until ($input -eq "quit")
}
}
main

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thomas GrassiSystems AdministratorAuthor Commented:
Dave

Thanks

I had to change this line to get it to work

Get-WinEvent -LogName $ev | Where-Object {$_.Id -eq $id} | fl

to

Get-WinEvent -FilterHashtable @{ LogName = "$ev"; id = $in} -maxevents 1 | FL *


Now it works as I need.

Thanks again
0
Thomas GrassiSystems AdministratorAuthor Commented:
Thanks for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.