radio buttons within PowerShell Studio

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.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

radio buttons within PowerShell Studio

Post by mqh77777 »

I have a function that will change the timezone on any computer. This function works fine when run from within PowerShell ISE but it does not change anything when run from within PowerShell Studio 2018 v5.5.150.

I have tried the following:

Code: Select all

       $NewTZ = ""
	if ($radiobuttonPacific.Checked -eq $true){$NewTZ = "Pacific Standard Time"}
	elseif ($radiobuttonMountain.Checked -eq $true) {$NewTZ = "US Mountain Standard Time" }
	elseif ($radiobuttonCentral.Checked -eq $true) { $NewTZ = "Central Standard Time" }
	elseif ($radiobuttonEastern.Checked -eq $true ) { $NewTZ = "Eastern Standard Time" }

	if ($radiobuttonPacific.Checked = $true){$NewTZ = "Pacific Standard Time"}
	elseif ($radiobuttonMountain.Checked = $true) {$NewTZ = "US Mountain Standard Time" }
	elseif ($radiobuttonCentral.Checked = $true) { $NewTZ = "Central Standard Time" }
	elseif ($radiobuttonEastern.Checked = $true ) { $NewTZ = "Eastern Standard Time" }

	if ($radiobuttonPacific.Checked){$NewTZ = "Pacific Standard Time"}
	elseif ($radiobuttonMountain.Checked) {$NewTZ = "US Mountain Standard Time" }
	elseif ($radiobuttonCentral.Checked) { $NewTZ = "Central Standard Time" }
	elseif ($radiobuttonEastern.Checked) { $NewTZ = "Eastern Standard Time" }
And here is the Function that I found online:

Code: Select all

$buttonSetTimeZone_Click = {
	$textPC = $PCNameBox.Text
	$richtextbox_Output.Clear()
	$statusbar1.text = 'Changing timezone, please wait...'
	$NewTZ = ""
	if ($radiobuttonPacific.Checked -eq $true){$NewTZ = "Pacific Standard Time"}
	elseif ($radiobuttonMountain.Checked -eq $true) {$NewTZ = "US Mountain Standard Time" }
	elseif ($radiobuttonCentral.Checked -eq $true) { $NewTZ = "Central Standard Time" }
	elseif ($radiobuttonEastern.Checked = $true ) { $NewTZ = "Eastern Standard Time" }
	
	
	Function Set-RemotetimeZone
	{
		[cmdletbinding()]
		param
		(
			
			
			[Parameter(Mandatory = $true, valuefrompipeline = $true)]
			[validatescript({ test-connection -ComputerName $_ -Quiet -Count 1 })]
			[String[]]$Computername,
			[validateset("Dateline Standard Time", "UTC-11", "Aleutian Standard Time", "Hawaiian Standard Time", "Marquesas Standard Time", "Alaskan Standard Time", "UTC-09"
						  ,"Pacific Standard Time (Mexico)", "UTC-08", "Pacific Standard Time", "US Mountain Standard Time", "Mountain Standard Time (Mexico)", "Mountain St
andard Time", "Central America Standard Time", "Central Standard Time", "Easter Island Standard Time", "Central Standard Time (Mexico)", "Canada C
entral Standard Time", "SA Pacific Standard Time", "Eastern Standard Time (Mexico)", "Eastern Standard Time", "Haiti Standard Time", "Cuba Standar
d Time", "US Eastern Standard Time", "Paraguay Standard Time", "Atlantic Standard Time", "Venezuela Standard Time", "Central Brazilian Standard Ti
me", "SA Western Standard Time", "Pacific SA Standard Time", "Turks And Caicos Standard Time", "Newfoundland Standard Time", "Tocantins Standard T
ime", "E. South America Standard Time", "SA Eastern Standard Time", "Argentina Standard Time", "Greenland Standard Time", "Montevideo Standard Tim
e", "Saint Pierre Standard Time", "Bahia Standard Time", "UTC-02", "Azores Standard Time", "Cape Verde Standard Time", "UTC", "Morocco Standard Time
", "GMT Standard Time", "Greenwich Standard Time", "W. Europe Standard Time", "Central Europe Standard Time", "Romance Standard Time", "Central Eur
opean Standard Time", "W. Central Africa Standard Time", "Namibia Standard Time", "Jordan Standard Time", "GTB Standard Time", "Middle East Standa
rd Time", "Egypt Standard Time", "E. Europe Standard Time", "Syria Standard Time", "West Bank Standard Time", "South Africa Standard Time", "FLE St
andard Time", "Israel Standard Time", "Kaliningrad Standard Time", "Libya Standard Time", "Arabic Standard Time", "Turkey Standard Time", "Arab Sta
ndard Time", "Belarus Standard Time", "Russian Standard Time", "E. Africa Standard Time", "Iran Standard Time", "Arabian Standard Time", "Astrakhan
 Standard Time", "Azerbaijan Standard Time", "Russia Time Zone 3", "Mauritius Standard Time", "Georgian Standard Time", "Caucasus Standard Time", "
Afghanistan Standard Time", "West Asia Standard Time", "Ekaterinburg Standard Time", "Pakistan Standard Time", "India Standard Time", "Sri Lanka S
tandard Time", "Nepal Standard Time", "Central Asia Standard Time", "Bangladesh Standard Time", "Omsk Standard Time", "Myanmar Standard Time", "SE 
Asia Standard Time", "Altai Standard Time", "W. Mongolia Standard Time", "North Asia Standard Time", "N. Central Asia Standard Time", "Tomsk Stand
ard Time", "China Standard Time", "North Asia East Standard Time", "Singapore Standard Time", "W. Australia Standard Time", "Taipei Standard Time"
						  ,"Ulaanbaatar Standard Time", "North Korea Standard Time", "Aus Central W. Standard Time", "Transbaikal Standard Time", "Tokyo Standard Time", "Ko
rea Standard Time", "Yakutsk Standard Time", "Cen. Australia Standard Time", "AUS Central Standard Time", "E. Australia Standard Time", "AUS Easte
rn Standard Time", "West Pacific Standard Time", "Tasmania Standard Time", "Vladivostok Standard Time", "Lord Howe Standard Time", "Bougainville S
tandard Time", "Russia Time Zone 10", "Magadan Standard Time", "Norfolk Standard Time", "Sakhalin Standard Time", "Central Pacific Standard Time",
						 "Russia Time Zone 11", "New Zealand Standard Time", "UTC+12", "Fiji Standard Time", "Chatham Islands Standard Time", "Tonga Standard Time", "Samoa 
Standard Time", "Line Islands Standard Time")]
			[Parameter(Mandatory = $true)]
			$Zone
			
		)
		begin { }
		process
		{
			
			foreach ($computer in $computername)
			{
				try
				{
					$currenttimezone = Get-wmiobject -ClassName Win32_SystemTimeZone -computername $computer -ErrorAction Stop | select-object setting
					if ($currenttimezone -like "*$zone*")
					{
						#Write-verbose "On $computer time zone is already set to $zone" -Verbose
					}
					else
					{
						Invoke-Command -ComputerName $computer -ScriptBlock { tzutil.exe /s $args[0] } -ErrorAction Stop -ArgumentList $Zone
						#Write-verbose "On $computer time zone is now set to $zone" -Verbose
					}
				}
				catch
				{
					#Write-error "Failed to set $computer time zone to $zone, Kindly check if Remoting and Access is enabled on computer" -Verbose
				}
			}
		}
		
		end { }
	}
	
	
	
	Set-RemotetimeZone -computername "$textPC" -zone  "$NewTZ"
	$statusbar1.text = 'All done!'
	$richtextbox_Output.AppendText($NewTZ)
	
	
}

The OUTPUT to the richtextbox always displays the correct radio button but the timezone never changes. and on the line where I call my function I've tried both with and without quotes around my variables. What am I doing wrong here?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: radio buttons within PowerShell Studio

Post by jvierra »

I see no reason for it not working in PS 2018. Are you sure the function is being called.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: radio buttons within PowerShell Studio

Post by jvierra »

Why do you have three copies of the same block of code?

The following is reproduced three time:

Code: Select all

	if ($radiobuttonPacific.Checked -eq $true){$NewTZ = "Pacific Standard Time"}
	elseif ($radiobuttonMountain.Checked -eq $true) {$NewTZ = "US Mountain Standard Time" }
	elseif ($radiobuttonCentral.Checked -eq $true) { $NewTZ = "Central Standard Time" }
	elseif ($radiobuttonEastern.Checked -eq $true ) { $NewTZ = "Eastern Standard Time" }
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: radio buttons within PowerShell Studio

Post by mqh77777 »

Why do you have three copies of the same block of code?
I was only trying to illustrate the different ways I've coded this and none work. That is why I had three copies of the same block.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: radio buttons within PowerShell Studio

Post by jvierra »

A quick look says that the code will not ever run correctly in ISE or in a form. The "ValidateSet" is broken and no errors are being reported when the code fails.

You cannot set the TZ to the local computer without running at an elevated prompt
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: radio buttons within PowerShell Studio

Post by jvierra »

This shows how to do this. I also fixed the function to work better in a form.
Attachments
Update-TimeZone.psf
(57.17 KiB) Downloaded 136 times
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: radio buttons within PowerShell Studio

Post by mqh77777 »

thank you and I did get mine to work. I was running it all on my own PC where I'm a local admin. But it only worked when I launched my compiled .exe as "administrator"

But I have another question on radio buttons. How do you clear them? I have a button on my form that when pressed clears all text fields. How do you clear all radio buttons?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: radio buttons within PowerShell Studio

Post by jvierra »

Here is ho I clear the form:
Attachments
Update-TimeZone.psf
(57.32 KiB) Downloaded 169 times
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked