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 6 years and 3 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
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2
Post
by apowershelluser » Wed Jan 03, 2018 11:21 pm
Hello,
So back story on my problem. We have two models of PC that are named the same in the registry. So instead of just typing this to get the model.
( Get-ItemProperty -Path 'HKLM:\HARDWARE\DESCRIPTION\System\Bios' ) .SystemProductName
I'm forced to dig into the ProcessorNameString and using a switch statement. Note: This is in the -CompletedScript of the Job
$GetModel = switch ( $results )
{
'Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz' { "HP 840" }
'Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz' { "HP 840P" }
}
if ( $results -eq 'Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz' ) {
$GetModel = ( Get-ItemProperty -Path 'HKLM:\HARDWARE\DESCRIPTION\System\Bios' ) .SystemProductName
}
$label1 .text = $GetModel
This works perfectly in the ISE, but when I build my form, it appears the $GetModel is not being updated so my label actually returns both switches.
This is the full button click event
$buttonStartJob_Click = {
$buttonStartJob .Enabled = $false
#Create a New Job using the Job Tracker
Add- JobTracker -Name 'JobGetESDRegistry' `
- JobScript {
#--------------------------------------------------
#TODO: Set a script block
#Important: Do not access form controls from this script block.
Param ( $computer , $GetModel ) #Pass any arguments using the ArgumentList parameter
Invoke- Command -ComputerName $computer - ScriptBlock { ( Get-ItemProperty -Path 'HKLM:\HARDWARE\DESCRIPTION\System\CentralProcessor\0' ) .ProcessorNameString }
#--------------------------------------------------
} `
- CompletedScript {
Param ( $Job )
$results = Receive- Job - Job $Job
$GetModel = switch ( $results )
{
'Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz' { "HP 810" }
'Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz' { "HP 810P” }
}
if ($results -eq 'Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz')
{
$GetModel = invoke-command -computername $computer -scriptblock { (Get-ItemProperty -Path 'HKLM:\HARDWARE\DESCRIPTION\System\Bios').SystemProductName }
}
$label1.Text = $GetModel
#Enable the Button
$buttonStartJob.ImageIndex = -1
$buttonStartJob.Enabled = $true
}`
-UpdateScript {
Param($Job)
#$results = Receive-Job -Job $Job -Keep
#Animate the Button
$label1.Text = " Looking..."
if($null -ne $buttonStartJob.ImageList)
{
if($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
{
$buttonStartJob.ImageIndex += 1
}
else
{
$buttonStartJob.ImageIndex = 0
}
}
}`
-ArgumentList $textbox1.Text
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Thu Jan 04, 2018 1:02 am
There is no point in using job to retrieve one registry value. Use WMI to get the processor type remotely. Do not use a job.
$result = (Get-WmiObject win32_processor -Computer $computer).Name | Select -first 1
On systems with multiple processors you will get more than on record. This code returns on one record.
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2
Post
by apowershelluser » Thu Jan 04, 2018 5:54 am
The real form will be running multiple jobs, so a job it must be.
I’m very aware but that’s not what I’m trying to do J
Two PCs have the same processor name, I need to differentiate between the two, hence my if statement
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Thu Jan 04, 2018 8:10 am
Your question is very hard to follow. You cannot tell the PCs apart from the processor name. You start with the PC name. Why do you need it again.
Add the pcname to the output. Invoke-Command returns "PsComputerName" which tells you which PC the command was run on.
Even in a Job you do not need to use "Invoke" command with WMI.
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2
Post
by apowershelluser » Thu Jan 04, 2018 8:39 am
All the PCs in our environment have different processors names but two
these two PCs are also named in the registry with the same Model - don't ask me why, and no one has given me an answer yet.
I need to give the technicians the correct information so they refresh a computer with the proper replacement model
the invoke comes in because these are remote PCs
I'm not looking for the name of the PC, I'm looking for the model.
I will see if taking it out of the job resolves this, but that won't help me in the future with working with jobs and not asking questions here
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Thu Jan 04, 2018 8:44 am
WMI works remotely without needing to use Invoke.
There is no way to identify a computer from the processor name.
What you seem to be trying to do makes no technical sense.
Here is how to get the processor name and the PC name remotely.
Get-WmiObject Win32_Processor -ComputerName $computer| select PsComputerName, Name
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2
Post
by apowershelluser » Thu Jan 04, 2018 9:16 am
Good call out on the gwmi
I would have figured you read the script and noticed my switch parameter
Using it allows me to create a model name based of the processor
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Thu Jan 04, 2018 9:26 am
Run the following at a command prompt to see how it works. It will show you how to run all calls in parallel. This is very fast and can be combined with job tracker to run as a background task.
Code: Select all
workflow GetProcessors {
param ([string[]]$computers)
foreach –parallel ($computer in $computers) {
Get-WmiObject –Class Win32_Processor –PSComputerName $computer
}
}
# run directly
GetProcessors -computers ws702, SBS01 | Select-Object SystemName, Name
# or run as a job
$job = GetProcessors -computers ws702, SBS01 -AsJob
$job | Receive-Job -Wait -AutoRemoveJob | Select-Object SystemName, Name
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2
Post
by apowershelluser » Thu Jan 04, 2018 10:24 am
I appreciate the code but you're still hung up on finding the information. Here's a broken down PSF file
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Thu Jan 04, 2018 11:16 am
I don't see what the issue is. What is not happening? Have you run this in the debugger?
I cannot run this on my system because I do not have those processors. It is also hard for me to understand what you are asking or what is not happening.
Try this to see your results:
Code: Select all
$button1_Click={
$processor = (Get-WmiObject Win32_Processor).Name
Write-Host Processor $processor
$model = switch ($processor) {
'Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz' { "HP 810" }
'Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz' { "HP 9470" }
}
Write-Host $model
if ($processor -like "Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz"){
$product = (Get-ItemProperty -Path 'HKLM:\HARDWARE\DESCRIPTION\System\Bios').SystemProductName
Write-Host Product $product
}
# not sure what you think will end up here
$label1.Text = $GetModel
}
This topic is 6 years and 3 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