I have a powershell script that uses a menu and a selection. So loops until selection is 0. For one selection it tests entered port with "Test-NetConnection". But for some reason after this is ran, whenever another selection is made it seems "Test-NetConnection" is always ran, the banner shows up in powershell (even though it is very quick, but it shouldn't be executed.
I removed some selections. And I also have a few custom functions. But not sure why this is happening. Any help would be greatly appreciated!
Powershell...
while ($selection -ne 0) {
Write-Output ""
Write-Output "__MENU__"
Write-Output "0. Exit"
Write-Output "1. Set Target Machines"
Write-Output "2. Set Save Directory"
Write-Output "4. Save MSINFO32"
Write-Output "5. Save All Installed Software"
Write-Output "9. Save GPO Info (gpresult)
Write-Output "10. Test WMI"
Write-Output "11. Test TCP Port"
Write-Output "14. Test Syslog"
Write-Output ""
Write-Output "Target Machines: $($targetComputers -Join ',')"
Write-Output "Save Directory: $savePath`n"
$selection = Read-Host -Prompt "Selection"
Write-Output ""
switch ($selection) {
0 { break }
1 { ... }
2 { ... }
11 {
$testPort = Read-Host -Prompt "TCP Port to Test (Leave blank for default: 80)"
Write-Output ""
if (($testPort -eq $null) -Or ($testPort -eq "")) { $testPort = 80 }
foreach ($computer in $targetComputers) {
try {
$netcon = Test-NetConnection -ComputerName $computer -Port $testPort -ErrorAction "Stop"
if($netcon.TcpTestSucceeded) { Write-Output "TCP Port $testPort test was successful on $computer." }
else { Write-Warning "TCP Port $testPort test was unsuccessful on $computer." }
}
catch { Write-Warning "TCP Port $testPort test was unsuccessful on $computer." }
}
} #11
14 {
$sysServer = Read-Host -Prompt "Syslog Server (Leave blank for default: ${env:computername})"
$sysProto = Read-Host -Prompt "Syslog Server Protocol (Leave blank for default: TCP)"
$sysPort = Read-Host -Prompt "Syslog Server Port (Leave blank for default: 514)"
Write-Output ""
if (($sysServer -eq $null) -Or ($sysServer -eq "")) { $sysServer = $env:computername }
if (($sysProto -eq $null) -Or ($sysProto -eq "")) { $sysProto = "TCP" }
if (($sysPort -eq $null) -Or ($sysPort -eq "")) { $sysPort = 514 }
sendSyslog -Server $sysServer -Protocol $sysProto -Port $sysPort
} #14
default { break }
} #switch
} #while
Your menu option "9. Save GPO Info (gpresult) is missing a closing quote.
Make this easier for yourself and use a Here-String for the menu:
$menu = #"
__MENU__
0. Exit
1. Set Target Machines
2. Set Save Directory
4. Save MSINFO32
5. Save All Installed Software
9. Save GPO Info (gpresult)
10. Test WMI
11. Test TCP Port
14. Test Syslog
"#
Then instead of looping like while ($selection -ne 0), run an endless loop you can exit as soon as the user types a '0'.
# enter an endless loop here.
# exit that loop right after the Read-Host when the user type '0'
while ($true) {
Write-Host $menu
# some in-between code.. #
$selection = Read-Host -Prompt "Selection"
if ($selection -eq '0') { break } # or exit if you want to stop the entire script here
# rest of your code.. #
}
P.S. anything coming back from Read-Host is a string, not a number.
Related
I am trying to add a user to the Remote Desktop group using Powershell.
It seems it's not breaking the loop and doesn't execute further statements.
Can you please help me where the issue is:
$remote = ""
While($remote -ne "Y" ){
$remote = read-host "Do you need to add anyone to the Remote Desktop group (y/n)?"
Switch ($remote)
{
Y {
$remoteuser = ""
while ( ($remoteuser -eq "") -or ($UserExists -eq $false) )
{
$remoteuser = Read-Host "Enter the username that needs to be in the group"
Write-Host "User inputted $remoteuser"
sleep -Seconds 2
try {
Get-ADUser -Identity $remoteuser -Server <server-FQDN>
$UserExists = $true
Write-Host "$remoteuser found!"
sleep 5
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityResolutionException] {
Write-Host "User does not exist."
$UserExists = $false
}
catch {
Write-Host "Username is blank"
$UserExists = $false
}
}
}
N {Write-Host "No user accounts will be added to the Remote Desktop Users group. Restart your PC."}
default {Write-Host "Only Y/N are Valid responses"}
}
}
<further statements>
On my Windows 7 Embedded machine I want to change the IP address via Powershell script as an user.
For that I added my user to the "Network Configuration Operators" group and wrote the following script.
param(
[string]$Type
)
Write-Host "Networkchanger"
$adapter = Get-WmiObject win32_NetworkAdapterConfiguration -filter "Index = 11"
if($Type -eq "black"){
Write-Host "Using black"
$IP = "192.168.1.172"
$Netmask = "255.255.255.0"
$Gateway = "192.168.1.1"
$DNS = "192.168.1.254"
$adapter.EnableStatic($IP, $NetMask)
Sleep -Seconds 4
$adapter.SetGateways($Gateway)
$adapter.SetDNSServerSearchOrder($DNS)
} else {
Write-Host "Using rf"
$adapter.SetDNSServerSearchOrder()
$adapter.EnableDHCP()
}
The script runs fine as admin, but not as an user. Did I forget to add some rights to the script or user?
Edit:
When I click "Run as admin" and use black it works for the first time. After changing it to rf (which works), the black net just changes the Gateway and DNS, but not the IP and Netmask, which confuses me.
You are correct that admin rights are required for setting a static IP address. I do it on our Windows Embedded Standard 7 images. Essentially, I created a shortcut on the desktop with Run As Administrator for launching PowerShell with the particular script. Also note that no such elevated permission is required to enable DHCP, but it doesn't hurt.
There is a slightly simpler way to set the IP address from PowerShell, using the netsh command. The good thing about this approach is that you can see the specific error from a command prompt also. Try switching back and forth from an elevated command prompt and a non-elevated one.
netsh interface ip set address "${InterfaceName}" static addr=${IPAddr} mask=${Mask} gateway=${Gateway}
netsh interface ip set address "${InterfaceName}" dhcp
I've solved the problem by heavily modifing my Powershell script:
$id=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal=New-Object System.Security.Principal.WindowsPrincipal($id)
if(!$principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
$powershell=[System.Diagnostics.Process]::GetCurrentProcess()
$psi=New-Object System.Diagnostics.ProcessStartInfo $powershell.Path
$script=$MyInvocation.MyCommand.Path
$prm=$script+$Type
foreach($a in $args) {
$prm+=' '+$a
}
$psi.Arguments=$prm
$psi.Verb="runas"
[System.Diagnostics.Process]::Start($psi) | Out-Null
return;
}
Write-Host "Networkchanger"
Write-Host ""
Write-Host "[0] cancel"
Write-Host "[1] net1: 10.0.0.172"
Write-Host "[2] net2: 192.168.178.(172,235,237,248,251)"
$adapter = Get-WmiObject win32_NetworkAdapterConfiguration -filter "Index = 0"
$loop = 1;
while($loop -eq 1){
$Type = Read-Host -Prompt '0, 1 OR 2'
switch($Type){
0 {
Write-Host "Cancel Process"
Sleep -Seconds 3
exit
}
1 {
Write-Host "Using sb"
$IP = "10.0.0.172"
$Netmask = "255.255.255.0"
$Gateway = "10.0.0.1"
$DNS = "10.0.0.254"
$adapter.EnableStatic($IP, $NetMask)
Sleep -Seconds 4
$adapter.SetGateways($Gateway)
$adapter.SetDNSServerSearchOrder($DNS)
$loop = 0
}
2 {
Write-Host "Using rf"
$macaddress = $adapter | select -expand macaddress
Write-Host $macaddress
$IP = ""
if ($macaddress -eq "xx:xx:xx:xx:xx:xx"){
$IP = "192.168.178.172"
} elseif ($macaddress -eq "xx:xx:xx:xx:xx:xx") {
$IP = "192.168.178.235"
} elseif ($macaddress -eq "xx:xx:xx:xx:xx:xx") {
$IP = "192.168.178.237"
} elseif ($macaddress -eq "xx:xx:xx:xx:xx:xx") {
$IP = "192.168.178.248"
} elseif ($macaddress -eq "xx:xx:xx:xx:xx:xx") {
$IP = "192.168.178.251"
} else {
Write-Host "Mac address not in list"
Sleep -Seconds 5
exit
}
$Netmask = "255.255.255.0"
$Gateway = "192.168.178.1"
$DNS = "192.168.178.2","192.168.178.3"
$adapter.EnableStatic($IP, $NetMask)
Sleep -Seconds 4
$adapter.SetGateways($Gateway)
$adapter.SetDNSServerSearchOrder($DNS)
$loop = 0
}
}
}
Write-Host "Current IP: "
ipconfig
Start-Sleep -seconds 5
I want to jump from one section in the script to another section in the same PowerShell script. My script is only going from top to bottom right now, but I have several tasks. e.g. I want to be able to choose from a task list in the beginning of the script and go to task number 2 skip task 1.
I hope this makes any sense for you. Take a look at my script:
Write-Host -ForegroundColor Yellow "welcome to my powershell script..."
""
""
""
Start-Sleep -Seconds 1
Write-Host -ForegroundColor Yellow "Choose a task:"
""
""
Write-Host -ForegroundColor Yellow "1. Clean up"
Write-Host -ForegroundColor Yellow "2. Uninstall Pre-Installed Apps"
Write-Host -ForegroundColor Yellow "3. Something should be written here"
""
""
""
While ($Valg -ne "1" -or $Valg -ne "2" -or $Valg -ne "3") {
$Valg = Read-Host "Choose a number from the task list"
If ($Valg –eq "1") { Break }
If ($Valg –eq "2") { Break }
If ($Valg –eq "3") { Break }
if ($Valg -ne "1" -or $Valg -ne "2" -or $Valg -ne "3") {
""
Write-Host -ForegroundColor Red "Ups. Try again..."
}
}
#### 1. First task should come here (clean up)
#some code here for the "cleaning up" task
#### 2. Second task here
#some code here for the "Uninstall Pre-Installed Apps" task
#### 3. Third task there
#Some code for the third task here
#### And so on...
Here is a generic solution which uses an array of PSCustomObject that represents the task (message and the function to invoke). It doesn't need a switch, you can simply add new Tasks to the array (and implement the desired function) without modifying the remaining script:
# define a task list with the messages to display and the functions to invoke:
$taskList = #(
[PSCustomObject]#{Message = 'Clean up'; Task = { Cleanup }}
[PSCustomObject]#{Message = 'Uninstall Pre-Installed Apps'; Task = { Uninstall }}
[PSCustomObject]#{Message = 'Something should be written here'; Task = { Print }}
)
# define the functions:
function Cleanup()
{
Write-Host "Cleanup"
}
function Uninstall()
{
Write-Host "Uninstall"
}
function Print()
{
Write-Host "Print"
}
# let the user pick a task:
Write-Host -ForegroundColor Yellow "Choose a task:"
$taskList | foreach -Begin { $i = 1;} -Process {
Write-Host -ForegroundColor Yellow ('{0}. {1}' -f ($i++), $_.Message)
}
do
{
$value = Read-Host 'Choose a number from the task list'
}
while($value -match '\D+' -or $value -le 0 -or $value -gt $taskList.Count)
# invoke the task:
& $taskList[$value-1].Task
I am updating answer to complete script based on inputs by "bluuf" and "majkinator".
Use the Switch-Case construct along with Functions like below. This is complete working solution.
#region Function Definitions. These come first before calling them
function FirstTask
(
[string] $inputVariable
)
{
"write any scipt for First task here without quotes. Input is: " + $inputVariable
}
function SecondTask
{
"write any scipt for Second task here without quotes"
}
function ThirdTask
{
"write any scipt for Third task here without quotes"
}
#endregion
#region Showing Options
Write-Host -ForegroundColor Yellow "welcome to my powershell script..."
""
""
""
Start-Sleep -Seconds 1
Write-Host -ForegroundColor Yellow "Choose a task:"
""
""
Write-Host -ForegroundColor Yellow "1. Clean up"
Write-Host -ForegroundColor Yellow "2. Uninstall Pre-Installed Apps"
Write-Host -ForegroundColor Yellow "3. Something should be written here"
Write-Host -ForegroundColor Yellow "0. Exit"
""
""
""
#endregion
#region Getting input
While ($true) {
$Valg = Read-Host "Choose a number from the task list"
If ($Valg –eq "0")
{
"Thanks for using my utility";
Break;
}
If (($Valg -ne "1") -and ($Valg -ne "2") -and ($Valg -ne "3") -and ($Valg -ne "0")) {
""
Write-Host -ForegroundColor Red "Ups. Try again..."
}
#region Main Code
switch ($Valg)
{
1{
FirstTask("sending input");
}
2 {
SecondTask;
}
3 {
ThirdTask;
}
default { "Please select a correct option."}
}
#endregion
}
#endregion
I have 2 sections of PowerShell code that exit the script on failure. After thinking about it I realized it would make more sense to pause on a failure and allow the admin time to remediate... then when the any key is paused, retry the same line in the CSV. Here's how it looks now:
#check tools status first
Write-Host ""
Write-Host "Checking VMware Tools Status before proceeding." -foreground green
Write-Host ""
foreach ($item in $vmlist) {
$vmname = $item.vmname
$ToolsStatus = (Get-VM $vmname).extensiondata.Guest.ToolsStatus -eq "toolsNotRunning"
if ($ToolsStatus -eq $true) {
Write-Host ""
Write-Host "Tools is not installed or running on $vmname. Remediate on guest and restart the script" -foreground Yellow
Write-Host "Script will continue to exit until tools is running on $vmname" -foreground yellow
Write-Host ""
exit
} else {
Write-Host ""
Write-Host "Tools running on all VMs, script will continue" -foreground green
Write-Host ""
}
}
I know how to put the pause in $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'), but I have no idea to do the loop so it retries.
I'm using similar code elsewhere to validate that VMs are powered up so this will work in both sections.
EDIT: Here's the re-worked script. Would this work?
foreach ($item in $vmlist) {
$vmname = $item.vmname
do {
$ToolsStatus = (Get-VM $vmname).extensiondata.Guest.ToolsStatus -eq "toolsNotRunning"
if ($ToolsStatus) {
Write-Host ""
Write-Host "Tools is not installed or running on $vmname." -Foreground yellow
Write-Host "Remediate VMware tools on $vmname and"
Write-host "Press any key to retry..."
$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
}
} until ($ToolsStatus -eq "PoweredOn")
Write-Host "Tools running on $vmname, script will continue" -Foreground green
}
Add a nested do..while loop like this:
foreach ($item in $vmlist) {
$vmname = $item.vmname
do {
$ToolsMissing = (Get-VM $vmname).extensiondata.Guest.ToolsStatus -eq "toolsNotRunning"
if ($ToolsMissing) {
Write-Host "Tools is not installed or ..." -Foreground yellow
$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
}
} while ($ToolsMissing)
}
Write-Host "Tools running on all VMs, script will continue" -Foreground green
I am very new to coding so I know very little.
I am writing my first code which is a ping. It can ping any workstation ID that you put in it. I would like that when it pings, the reply would be green and request timed out would be red.
I just want to test it out and learn from it. So far I have this:
$Search = Read-Host "Enter Workstation ID"
$Reply = ping -t $Search
if ($Reply -like "Request Timed Out.") {
Write-Host $Reply -ForegroundColor Red
}
But that is not working.
If you look at the usage message from ping.exe, you'll see that the -t switch makes ping.exe continue pinging the host until interrupted by Ctrl+C or Ctrl+Break.
That's why it seems like it's "doing nothing".
I would probably go for the Test-Connection cmdlet, rather than ping.exe:
$Hostname = Read-Host "please enter the hostname..."
if(Test-Connection -ComputerName $Hostname -Quiet)
{
Write-Host "Ping succeeded!" -ForegroundColor Green
}
else
{
Write-Host "Ping failed!" -ForegroundColor Red
}
It doesn't have a -t parameter, but you can provide a ridiculously high value to the -Count parameter, and make due with that (with a second in between each request, [int]::MaxValue gives you 2^31 seconds, or 68 years worth of pinging):
Test-Connection $Hostname -Count ([int]::MaxValue)
If you sincerely want the -t switch, you can't rely on assignment to a variable, since PowerShell will wait for ping.exe to return (which, intentionally, never happens).
You can however pipe the standard output from ping.exe, and PowerShell's asynchronous runtime will keep them coming for as long as you like:
function Keep-Pinging
{
param([string]$Hostname)
ping.exe -t $Hostname |ForEach-Object {
$Color = if($_ -like "Request timed out*") {
"Red"
} elseif($_ -like "Reply from*") {
"Green"
} else {
"Gray"
}
Write-Host $_ -ForegroundColor $Color
}
}