Powershell Script

Greetings,
I want to combine two scripts into one script with a choice between them.
The Scripts are working good.
Enclosing what I have built up to now, what have I done wrong?
It does not really make my choice.

Write-Host "Please enter what you need [1-2]:
1. Change Computer Name
2. Restart Computer"

if ($pc -eq "1") { $pc = Get-ADComputer -Filter * -Property CN | Select-Object CN |
Out-GridView -OutputMode Single -Title 'Select one PC to rename'
$newname = Read-Host 'Please enter your new computer name: '
Rename-Computer -ComputerName $pc.CN -NewName $newname -DomainCredential amal-p\admin -Force -PassThru -Restart }

if ($pc1 -eq "2") { $pc1 = Get-ADComputer -Filter * -Property CN | Select-Object CN |
Out-GridView -OutputMode Single -Title 'Select PC To Reset' | Select -ExpandProperty CN
Restart-Computer -ComputerName $pc1 }
2018-09-03_08-24-19.png
Netanel ElhadadIT System 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.

Peter CalderCommented:
You're not waiting for the script to get a value for $pc or $pc1 - the write-host is just printing the options, $pc doesn't exist so it skips to the next line, $pc1 doesn't exist so the script ends.

You need to wait for input and execute once the input is selected.

Here's some inspiration
http://www.tomsitpro.com/articles/powershell-interactive-menu,2-961.html
1
oBdACommented:
You're reading the computer name from the user, but never the selected menu option.
Try something like this;
$menu = [ordered]@{
	'1' = 'Change Computer Name'
	'2' = 'Restart Computer'
}

Do {
	$menu.Keys | ForEach-Object {
		Write-Host "$($_). $($menu[$_])"
	}
	$choice = Read-Host "Please select [$($menu.Keys -join ', ')]"
} Until (($menu.Keys -contains $choice) -or [string]::IsNullOrEmpty($choice))

Switch ($choice) {
	'1' {
		$pc = Get-ADComputer -Filter * -Property CN | Select-Object CN | Out-GridView -OutputMode Single -Title 'Select one PC to rename'
		$newname = Read-Host 'Please enter your new computer name: '
		Rename-Computer -ComputerName $pc.CN -NewName $newname -DomainCredential amal-p\admin -Force -PassThru -Restart
	}
	'2' {
		$pc = Get-ADComputer -Filter * -Property CN | Select-Object CN | Out-GridView -OutputMode Single -Title 'Select PC To Restart'
		Restart-Computer -ComputerName $pc.CN
	}
}

Open in new window

2

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
Netanel ElhadadIT System AdministratorAuthor Commented:
Thanks a lot friends!
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.