Clear-Host issues

by Typeo at 2013-03-28 06:57:05

Its kind of hard to explain, but basically from time to time I have some text that does not get cleared off by Clear-Host. My script is setup to check whether SNMP is installed and to install it. I have it set to clear-host throughout the script and it seems at times only a portion of the text get cleared and the rest gets mixed up with the next set of text that appears on the screen. I know it doesn’t make sense, I’m not sure how else to explain it. I can try and get some screenshots if that might help?

I have everything setup in separate functions with a menu at the start deciding what the user wants to do, as well with some if statements that allow them to cancel if they choose. I have a similar script that is setup with functions that does not have this issue and the only main difference I can see in how it is formatted is that it is all encased in a script block… Would I need to do that here for some reason?

Anyways, if you want to see the code, it is below.

# SNMP & WMI Installer
# Version 1.0
# Jeryn Neill

# Setting up variables
$ComPath = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities'
$HostPath = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers'
$FWPath = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SharedAccess\Parameters\FirewallPolicy\FirewallRules'

#Importing ServerManager Module
Import-Module ServerManager

Function menu ()
{
Clear-Host
Write-Host 'SNMP & WMI INSTALLER'
Write-Host ''
Write-Host 'Please select one of the following options:'
Write-Host '1 - Full setup (SNMP and WMI)'
Write-Host '2 - SNMP setup only'
Write-Host '3 - WMI setup only'
Write-Host '0 - Exit program'
Write-Host ''

$userselect = Read-Host 'Select an option, press enter to continue'

Clear-Host

Switch ($userselect)
{
1 {Full}
2 {SNMP}
3 {WMI}
0 {Exit}
default {Error}
}
}

Function Full ()
{
#Setting up the initial SNMPCheck Variable
$SNMPCheck = Get-WindowsFeature | Where-Object {$.Name -eq 'SNMP-Service'}

#Checks to see if SNMP Service is installed or not… If not, it will run the IF statement
If (-not $SNMPCheck.Installed)
{
Write-Host 'You are about to install SNMP!'
Write-Host ''
$userselect = Read-Host 'Press enter to continue or type menu to cancel'

if ($userselect -eq 'menu')
{
menu
}

Clear-Host

#Installing windows SNMP Service Feature and its subfeature
Add-WindowsFeature -Name SNMP-Service -IncludeAllSubFeature

}

#Saves new SNMPCheck State into the variable
$SNMPCheck = Get-WindowsFeature | Where-Object {$
.Name -eq 'SNMP-Service'}

#Checks to see if SNMP service is installed or not… If so, it will run the IF statement
If ($SNMPCheck.Installed)
{

Write-Host 'SNMP is installed!'
Write-Host ''
$userselect = Read-Host 'Press enter to finish the setup or type menu to cancel'

if ($userselect -eq 'menu')
{
menu
}

Clear-Host

#Directory Change to make edits to the Registry
CD HKLM:

#Registry change to add SNMP community string
New-ItemProperty -Path $ComPath -Name 'commstring' -Type DWORD -Value 4 -Force | Out-Null

#Registry change to add accpeted hosts (This will overide any
New-ItemProperty -Path $HostPath -Name 2 -Type String -Value ipaddress -Force | Out-Null

#Registry change to edit the inbound filewall rule
New-ItemProperty -Path $FWPath -Name SNMP-In-UDP -Type String -Value 'v2.10|Action=Allow|Active=TRUE|Dir=In|Protocol=17|Profile=Private|Profile=Public|LPort=161|RA4=ipaddress|App=%SystemRoot%\system32\snmp.exe|Svc=SNMP|Name=@snmp.exe,-8|Desc=@snmp.exe,-10|EmbedCtxt=@snmp.exe,-3|' -Force | Out-Null

#Registry change to edit the outbound firewall rule
New-ItemProperty -Path $FWPath -Name SNMP-Out-UDP -Type String -Value 'v2.10|Action=Allow|Active=TRUE|Dir=Out|Protocol=17|Profile=Private|Profile=Public|RPort=161|RA4=ipaddress|App=%SystemRoot%\system32\snmp.exe|Svc=SNMP|Name=@snmp.exe,-9|Desc=@snmp.exe,-11|EmbedCtxt=@snmp.exe,-3|' -Force | Out-Null

#Directory change back to C:<br> CD C:<br>
#Makes sure the SNMP service is started
Start-Service -Name SNMP

#Restarting SNMP service to accept changes
Restart-Service -Name SNMP

#Keeps the powershell window from closing automaticly
Read-Host 'setup has been completed, press enter to continue'
}

#Runs if it runs into issues with installing SNMP service
Else
{
Clear-Host

Read-Host 'Error: SNMP Services could not install correctly! Press Enter to return to to the main menu'

Menu
}

Clear-Host

Write-Host 'WMI - Enable and setup firewall rules!'
Write-Host ''
$userselect = Read-Host 'Press enter to setup WMI, or type menu to cancel'

Clear-Host

if ($userselect -eq 'menu')
{
menu
}

netsh firewall set service remoteadmin enable custom 139.78.48.1/255.255.252.0 all

Read-Host 'Press enter to continue'

menu

}

Function SNMP ()
{
Clear-Host

#Setting up the initial SNMPCheck Variable
$SNMPCheck = Get-WindowsFeature | Where-Object {$.Name -eq 'SNMP-Service'}

#Checks to see if SNMP Service is installed or not… If not, it will run the IF statement
If (-not $SNMPCheck.Installed)
{
Write-Host 'You are about to install SNMP!'
Write-Host ''
$userselect = Read-Host 'Press enter to continue or type menu to cancel'

Clear-Host

if ($userselect -eq 'menu')
{
menu
}

#Installing windows SNMP Service Feature and its subfeature
Add-WindowsFeature -Name SNMP-Service -IncludeAllSubFeature

}

#Saves new SNMPCheck State into the variable
$SNMPCheck = Get-WindowsFeature | Where-Object {$
.Name -eq 'SNMP-Service'}

#Checks to see if SNMP service is installed or not… If so, it will run the IF statement
If ($SNMPCheck.Installed)
{

Write-Host 'SNMP is installed!'
Write-Host ''
$userselect = Read-Host 'Press enter to finish the setup or type menu to cancel'

Clear-Host

if ($userselect -eq 'menu')
{
menu
}

#Directory Change to make edits to the Registry
CD HKLM:

#Registry change to add SNMP community string
New-ItemProperty -Path $ComPath -Name 'Ren&$timpyAugust1991' -Type DWORD -Value 4 -Force | Out-Null

#Registry change to add accpeted hosts (This will overide any
New-ItemProperty -Path $HostPath -Name 2 -Type String -Value 139.78.48.36 -Force | Out-Null

#Registry change to edit the inbound filewall rule
New-ItemProperty -Path $FWPath -Name SNMP-In-UDP -Type String -Value 'v2.10|Action=Allow|Active=TRUE|Dir=In|Protocol=17|Profile=Private|Profile=Public|LPort=161|RA4=139.78.48.36|App=%SystemRoot%\system32\snmp.exe|Svc=SNMP|Name=@snmp.exe,-8|Desc=@snmp.exe,-10|EmbedCtxt=@snmp.exe,-3|' -Force | Out-Null

#Registry change to edit the outbound firewall rule
New-ItemProperty -Path $FWPath -Name SNMP-Out-UDP -Type String -Value 'v2.10|Action=Allow|Active=TRUE|Dir=Out|Protocol=17|Profile=Private|Profile=Public|RPort=161|RA4=139.78.48.36|App=%SystemRoot%\system32\snmp.exe|Svc=SNMP|Name=@snmp.exe,-9|Desc=@snmp.exe,-11|EmbedCtxt=@snmp.exe,-3|' -Force | Out-Null

#Directory change back to C:<br> CD C:<br>
#Makes sure the SNMP service is started
Start-Service -Name SNMP

#Restarting SNMP service to accept changes
Restart-Service -Name SNMP

Clear-Host

#Keeps the powershell window from closing automaticly
Read-Host 'setup has been completed, press enter to continue'

}

#Runs if it runs into issues with installing SNMP service
Else
{
Clear-Host

Read-Host 'Error: SNMP Services could not install correctly! Press Enter to return to to the main menu'

Menu
}
}

Function WMI ()
{
Clear-Host

Write-Host 'WMI - Enable and setup firewall rules!'
Write-Host ''
$userselect = Read-Host 'Press enter to continue, or type menu to cancel'

Clear-Host

if ($userselect -eq 'menu')
{
menu
}

netsh firewall set service remoteadmin enable custom 139.78.48.1/255.255.252.0 all

Read-Host 'Press enter to continue'

menu
}

Function Exit ()
{
exit
}

Function Error ()
{
Read-Host 'Invalid entry, press enter to continue'

Menu
}

Menu


Thanks for your help,
Jeryn
by DonJ at 2013-03-28 07:54:55
How, lotta code there.

I can tell you that I’ve seen display anomalies like that before. In v3, for example, the progress bar shown by the Get-Help command (when it’s searching for help) obscures the underlying output. Similar snafu. Nothing you can do about it, though.
by Typeo at 2013-03-28 08:33:24
Thank you very much for the response. I have been testing this script on my 2008 server that I had updated to V3… I moved it to my other test server with V2 on it, and so far it doesn’t seem to be having the issues anymore. I will continue to test it and see if they come back, however, so far it seems to be working fine.

Even if it does come back, seems like there is not much I can do about it, so I will go ahead and put this as solved.

Thank you very much for the help,
Jeryn
by Typeo at 2013-03-28 08:45:46
Sadly, it did happen again, and since it could make several of the screens very confusing for somebody who doesn’t run the script very often wanna give a bit more information to see if anything can be done. A screen to show what is going on is below.

a user uploaded image

The text in the red is the only thing that should be on this screen and half the times it is… However, every now and then it keeps portions on the older screens and it drags them through the rest of the program. You can see that is the green box. What is in the green box does not go away no matter how many clear-hosts it hits.

I am going to unsolve this thread for a bit just to see if anybody might have a suggestion I can give a try.
by DonJ at 2013-03-28 08:52:51
You’re kinda using the shell in a way it isn’t ideally designed-for - my suggestion would be to avoid using Clear-Host so much in the script ;). I get that you’re trying to make a nice user-friendly UI, but that’s not a strong point for the product. That console host is a very old application, and it’s just got limitations that the PowerShell team can’t address (they don’t actually own that code). But good luck!
by Typeo at 2013-03-28 09:07:53
I will keep it in mind and try to make some changes on this and future scripts.

Thank you for the information / advise,
Jeryn
by DexterPOSH at 2013-03-29 16:19:50
I was wondering if after using Clear-Host in your script, you could use start-sleep to pause the execution for a bit and give it time.
Then maybe it will help. I haven’t tested it but seems like it could work.