How to Clear Hostname Variable

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
Nicholas.Gerritz
Posts: 2
Last visit: Fri Jul 23, 2021 1:15 am

How to Clear Hostname Variable

Post by Nicholas.Gerritz »

Hi Support,

I have a Powershell GUI Script/App which does a SQLCMD query to a (hostname) DB table lets call it MyDB\INST1 and then pipes the result to an out-string, which is then set to a variable. It works perfectly well.

I have a problem however when I switch to a different environment (from within my GUI Script/App), and then run that same SQLCMD query on an ientical DB (that has the same hostname but different IP address and subnet) but the SQLCMD query runs on the first environment's DB host.

Is there a way for me to get my variables to forget what they were last run against, so that when I run them a second time, they are forced to do a fresh lookup, and therefore target the new hostname/ip pair, which will resolve in the hostsfile and target the correct IP address.

For some reason, no matter if I wrap the step in a function, or even just script the query straight into a button, it works the first time and then fails to work "correctly" the second time, and when I have switched to a different environment that has the same hostnames but different host IP addresse, the GUI Script/App refuses to target the new host/ipaddress. I have to use Hostnames to target the DB, since they are common in name, but i cant easily manage the IP address as a target, since there are hundreds and they often change in IP but not in name, so managing hostname as the target is FAR more practical to do.

BTW : My host file is updated each time with the new environments host/ip pairs and runs an "ipconfig /flushdns" step when the environment target is switched. Its the hostname/ip that seems to persist in the Powershell GUI somehow?

Any ideas?
Thanks in advance

jvierra
Posts: 14866
Last visit: Wed Jul 21, 2021 2:13 am
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: How to Clear Hostname Variable

Post by jvierra »

Sorry but it is impossible to know what you are asking. Without some idea of what variable you are asking about there is no way to suggest an answer.

WHat are teh "host/ip pairs" There is no such technical variable or thing. Without some clear idea of what you have and how you are proceeding then we cannot guess at anyting.

Nicholas.Gerritz
Posts: 2
Last visit: Fri Jul 23, 2021 1:15 am

Re: How to Clear Hostname Variable

Post by Nicholas.Gerritz »

Hi @jvierra thanks for the reply,

So I Have a variable that is called $textbox_return which I later then print to a textbox object $txtbox1 using
$txtbox1.text = $textbox_return

I set $textbox_return = Invoke-SQLCMD -query "select * from table where columnID = 27" -ServerInstance DBHost1\INST1 -Database MyDB -Username x -password y | Out-String

$txtbox1.text = $textbox_return

In this case DBHost1 is a hostname of my DB Server and my pc looks in my hostfile for DBHost1 IP to reach the server to run the query - which works just fine.

Then I have a button called $btn_ChangeEnvironment that does a Hostfile Change on my PC on a click event which works fine too,
this is verifiable using a ping to check confirm

$pingResult = ping DBHost1 -n 4 | out-string
$txtBox1.text = $pingResult (and this confirms the IP has changed after the hostfile updates from the $btn_ChangeEnvironment_Click event)

After the Environment is changed (ie: the host file is changed where DBHost1 now has a different IP address mapping in my hostfile)
I then run my original SQLCMD Query :

$textbox_return = Invoke-SQLCMD -query "select * from table where columnID = 27" -ServerInstance DBHost1\INST1 -Database MyDB -Username x -password y | Out-String

$txtbox1.text = $textbox_return

and print out the return string which shows that the query looked again at the 1st env DBHost1 and not the new env DBHost1, despite DBHost1 in my hostfile now being different interms of the IP address mapped to it in my hosts file.

I cant seem to get the Powershell script GUI APP to forget the context of DBHost1 when it is run more than once, and where DBHost1 is mapped to a new IP relitive to the changed Hostfile mappings effected by the step $btn_ChangeEnvironment_Click

Does that make any more sense?

Just to clarify with an example, when the GUI Script runs my host file is like this :

Environment 1 :
DBHost1 192.168.50.2
APPHost1 192.168.50.3
WebHost1 192.168.50.4

$pingResult returns "192.168.50.2"

After executing $btn_ChangeEnvironment_Click

Environment 2:
DBHost1 172.16.0.2
APPHost1 172.16.0.3
WebHost1 172.16.0.4

$pingResult returns "172.16.0.2"

jvierra
Posts: 14866
Last visit: Wed Jul 21, 2021 2:13 am
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: How to Clear Hostname Variable

Post by jvierra »

WHy do you have to change the hostname? WHy are you using SQLCMD in a GUI? Changing a hostname after starting an app will not change it foro the app unless you exit the app and restart it.

Overal I see that you have no programming experience and you are just guessing at how to do this. Normally we would just use a Net class to query a dataaabse. Also a database should only be queried by its own instance name. Doing otherwise will usually cause conflicts with the database instance.

Once a connection is made using a name then changing the IP will not remove the cached connection which will just be reused the next time you use the same name.

Of course all of this is also dependent on the installation and the configuration of you machine, network and database.

Post Reply