Author Posts

March 18, 2016 at 12:40 am

I'm trying to get my script to stop if test connection is False.

It keeps trying to query the remote PC even if it fails

function checkhw {

$HostName=$InputBox.text;
$newLine = [System.Environment]::NewLine
foreach ($HostName In $InputBox.text){
$ping = (Test-Connection -ComputerName $HostName -Count 1 -quiet )
try
{
$errorActionPreference = "stop"
$outputBoxping.text = $ping
$outputboxquery.text = write "Querying: $HostName"
$computerSystem = get-wmiobject Win32_ComputerSystem -ComputerName $HostName
$computerBIOS = get-wmiobject Win32_BIOS -ComputerName $HostName
$computerOS = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
$computerOSB = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
$computerBLD = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
$computerCPU = get-wmiobject Win32_Processor -ComputerName $HostName
$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $HostName -Filter "DeviceID='C:'"
$computerLCD = get-wmiobject Win32_DesktopMonitor -ComputerName $HostName
$outputboxquery.text = write "Done"
$Man = $computerSystem.Manufacturer
$Mod = $computerSystem.model -replace "1095BV6", "C30" -replace "3664AK7", "M72e" -replace "30A0S0AT0A", "E32"-replace "32373A0", "M92p" -replace "3237EF9", "M92p" -replace "3167BC8", "M71e" -replace "10A4S00R0D", "M93" -replace "3664AK9", "M72e" -replace "3267B69", "M72e Tiny" -replace "0833AL2", "M70e" -replace "4263BM6", "C20" -replace "30A8S00300", "P700" -replace "11363A0", "C30" -replace "30AGS0CR05", "P300" -replace "43516D7", "S30" -replace "0606C26", "S30" -replace "43517A4", "S30 QA" -replace "VMware Virtual Platform", "VDI" -replace "30A8S00400", "P700 QA" -replace "4157ER5", "S20"
$Ser = $computerBIOS.SerialNumber
$CPUs = $ComputerSystem.NumberOfProcessors
$Cores = $ComputerSystem.NumberOfLogicalProcessors
$HDD = "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
$RAM = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
$HDDF = "{0:N2}" -f ($computerHDD.freespace/1GB) + "GB"
$OS = $computerOS.caption -replace "Microsoft Windows 7 Enterprise", "Win 7" -replace "Microsoft Windows XP Professional", "XP"
$OSB = $computerOSB.ConvertToDateTime(($computerOSB).InstallDate)
$Usr = $computerSystem.UserName
$Check = write " Hostname: " $computerSystem.Name, $newline, "Manufacturer: " $Man, $newline, "Model: " $Mod, $newline, "Serial: " $Ser, $newline, "No. CPU's: " $CPUs,$newline, "No. Cores's: " $Cores, $newline, "Memory: " $RAM, $newline, "HDD Size: " $HDD, $newline, "HDD Free Space: " $HDDF, $newline, "Operating System: " $OS, $newline, "Built Date: " $OSB, $newline, "User Logged in: " $Usr
$outputBoxHW.text=$Check
}

catch
{
out-file $labelPing.text
}}
}

March 18, 2016 at 1:01 am

1.
You should wrap your script in "pre" tags when posting. Also make sure the indentation is correct. Then it will look like this:

function checkhw {
	$HostName=$InputBox.text;
	$newLine = [System.Environment]::NewLine
	foreach ($HostName In $InputBox.text) {
		$ping = (Test-Connection -ComputerName $HostName -Count 1 -quiet)
		try
		{
			$errorActionPreference = "stop"
			$outputBoxping.text = $ping
			$outputboxquery.text = write "Querying: $HostName"
			$computerSystem = get-wmiobject Win32_ComputerSystem -ComputerName $HostName
			$computerBIOS = get-wmiobject Win32_BIOS -ComputerName $HostName
			$computerOS = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
			$computerOSB = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
			$computerBLD = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
			$computerCPU = get-wmiobject Win32_Processor -ComputerName $HostName
			$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $HostName -Filter "DeviceID='C:'"
			$computerLCD = get-wmiobject Win32_DesktopMonitor -ComputerName $HostName
			$outputboxquery.text = write "Done"
			$Man = $computerSystem.Manufacturer
			$Mod = $computerSystem.model -replace "1095BV6", "C30" -replace "3664AK7", "M72e" -replace "30A0S0AT0A", "E32"-replace "32373A0", "M92p" -replace "3237EF9", "M92p" -replace "3167BC8", "M71e" -replace "10A4S00R0D", "M93" -replace "3664AK9", "M72e" -replace "3267B69", "M72e Tiny" -replace "0833AL2", "M70e" -replace "4263BM6", "C20" -replace "30A8S00300", "P700" -replace "11363A0", "C30" -replace "30AGS0CR05", "P300" -replace "43516D7", "S30" -replace "0606C26", "S30" -replace "43517A4", "S30 QA" -replace "VMware Virtual Platform", "VDI" -replace "30A8S00400", "P700 QA" -replace "4157ER5", "S20"
			$Ser = $computerBIOS.SerialNumber
			$CPUs = $ComputerSystem.NumberOfProcessors
			$Cores = $ComputerSystem.NumberOfLogicalProcessors
			$HDD = "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
			$RAM = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
			$HDDF = "{0:N2}" -f ($computerHDD.freespace/1GB) + "GB"
			$OS = $computerOS.caption -replace "Microsoft Windows 7 Enterprise", "Win 7" -replace "Microsoft Windows XP Professional", "XP"
			$OSB = $computerOSB.ConvertToDateTime(($computerOSB).InstallDate)
			$Usr = $computerSystem.UserName
			$Check = write " Hostname: " $computerSystem.Name, $newline, "Manufacturer: " $Man, $newline, "Model: " $Mod, $newline, "Serial: " $Ser, $newline, "No. CPU's: " $CPUs,$newline, "No. Cores's: " $Cores, $newline, "Memory: " $RAM, $newline, "HDD Size: " $HDD, $newline, "HDD Free Space: " $HDDF, $newline, "Operating System: " $OS, $newline, "Built Date: " $OSB, $newline, "User Logged in: " $Usr
			$outputBoxHW.text=$Check
		} catch {
			out-file $labelPing.text
		}
	}
}

2.
This script does not run properly. You're using undeclared variables. I would recommend that you set the following:

Set-StrictMode -Version "latest"

This will give you errors when trying to use undeclared variables. Read about Set-StrictMode here: https://technet.microsoft.com/en-us/library/hh849692.aspx

3.
It's not that easy to understand what you need help with since the script doesn't run properly, but if you want to skip the foreach when the Test-Connection fails you could just add this:

$ping = (Test-Connection -ComputerName $HostName -Count 1 -quiet)
if (-not $ping) {
    continue
}

Read about the "continue" statement here: https://technet.microsoft.com/en-us/library/hh847821.aspx

March 18, 2016 at 1:32 am

Thanks Paal

The continue works a treat.

And i'm reading up on the strictmode 🙂

I have an output box the displays the True or False of test connection.
It doesn't display the False anymore when it fails

Any Ideas?

$outputBoxping = New-Object System.Windows.Forms.TextBox
$outputBoxping.Location = New-Object System.Drawing.Size(70,380)
$outputBoxping.Size = New-Object System.Drawing.Size(50,20)
$Form.Controls.Add($outputBoxping)

March 18, 2016 at 1:51 am

Could you post a full example that runs without errors (also when using strict mode)?

March 18, 2016 at 1:54 am

OK, my post has nothing to do with stopping your script. But I need to address that mess for replacing model numbers. If you need to have the data in the script, it would be much better addressed as a hash table to do the translation. It cleans up the script considerably making it easier to maintain, and it removes all the unnecessary replaces on a string that has already been replaced. Consider this ...

$Mod = "10A4S00R0D"  #sample for test, replace with $Mod = $computerSystem.model

# the table should be defined before the $host loop, not in it.
$modTable = @{
    "1095BV6" = "C30"
    "3664AK7" = "M72e"
    "30A0S0AT0A" = "E32"
    "32373A0" = "M92p"
    "3237EF9" = "M92p"
    "3167BC8" = "M71e"
    "10A4S00R0D" = "M93"
    "3664AK9" = "M72e"
    "3267B69" = "M72e Tiny"
    "0833AL2" = "M70e"
    "4263BM6" = "C20"
    "30A8S00300" = "P700"
    "11363A0" = "C30"
    "30AGS0CR05" = "P300"
    "43516D7" = "S30"
    "0606C26" = "S30"
    "43517A4" = "S30 QA"
    "VMware Virtual Platform" = "VDI"
    "30A8S00400" = "P700 QA"
    "4157ER5" = "S20"
}
if ($modTable.keys -contains $Mod)
{
    $Mod = $modTable[$Mod]
}
$Mod

"But wait, there's more!" But that's still not the best solution. The best would be be to extract the data altogether from your script. Move your data to a CSV file like this ...

Old,New
"1095BV6","C30"
"3664AK7","M72e"
"30A0S0AT0A","E32"

... and then read it in to create a lookup dictionary;

$Mod = "3664AK7"  # sample input
# Create dictionary (before $host loop)
$content = Import-CSV -Path .\modtable.csv
$modTable = $content | Group-Object -AsHashTable -AsString -Property old
if ($modTable.keys -contains $Mod)
{
    $Mod = $modTable[$Mod].New
}
$Mod

That way, if you need to add/change/delete models, you don't have to edit a working production script.

March 18, 2016 at 2:57 am

Thanks for all your help guys.

But how do i paste it using this pre thing...

Sorry i am a newbie

March 18, 2016 at 3:11 am

You paste in you code like you would normally, but you surround it with the pre and /pre tags.

March 18, 2016 at 3:15 am

This is my code for checking desktop hardware. I'm trying not to use external sources like csv as i want to roll it up into an exe for others to use.

Set-StrictMode -Version "latest"
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")



$Form = New-Object System.Windows.Forms.Form
$Form.BackColor = "White"
$Form.StartPosition = "CenterScreen"
$User = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$Form.text = "Test"
$Form.Size = New-Object System.Drawing.Size(800,510)


############################################## Start Check Hardware functions

function checkhw {

$HostName=$InputBox.text; 
$newLine = [System.Environment]::NewLine 
foreach ($HostName In $InputBox.text){
$ping = (Test-Connection -ComputerName $HostName -Count 1 -quiet)
if (-not $ping) {
    continue  
    }
            $outputBoxping.text = $ping  
            $errorActionPreference = "stop"
            $outputboxquery.text = write "Querying: $HostName"
            $computerSystem = get-wmiobject Win32_ComputerSystem -ComputerName $HostName
            $computerBIOS = get-wmiobject Win32_BIOS -ComputerName $HostName
            $computerOS = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
            $computerOSB = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
            $computerBLD = get-wmiobject Win32_OperatingSystem -ComputerName $HostName
            $computerCPU = get-wmiobject Win32_Processor -ComputerName $HostName
            $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $HostName -Filter "DeviceID='C:'"
            $computerLCD = get-wmiobject Win32_DesktopMonitor -ComputerName $HostName
            $outputboxquery.text = write "Done"
            $Man = $computerSystem.Manufacturer
            $Mod = $computerSystem.model -replace "1095BV6", "C30" -replace "3664AK7", "M72e" -replace "30A0S0AT0A", "E32"-replace "32373A0", "M92p" -replace "3237EF9", "M92p" -replace "3167BC8", "M71e" -replace "10A4S00R0D", "M93" -replace "3664AK9", "M72e" -replace "3267B69", "M72e Tiny" -replace "0833AL2", "M70e" -replace "4263BM6", "C20" -replace "30A8S00300", "P700" -replace "11363A0", "C30" -replace "30AGS0CR05", "P300" -replace "43516D7", "S30" -replace "0606C26", "S30" -replace "43517A4", "S30 QA" -replace "VMware Virtual Platform", "VDI" -replace "30A8S00400", "P700 QA" -replace "4157ER5", "S20"
            $Ser = $computerBIOS.SerialNumber
            $CPUs = $ComputerSystem.NumberOfProcessors
            $Cores = $ComputerSystem.NumberOfLogicalProcessors
            $HDD = "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
            $RAM = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
            $HDDF = "{0:N2}" -f ($computerHDD.freespace/1GB) + "GB" 
            $OS = $computerOS.caption -replace "Microsoft Windows 7 Enterprise", "Win 7" -replace "Microsoft Windows XP Professional", "XP"
            $OSB = $computerOSB.ConvertToDateTime(($computerOSB).InstallDate)
            $Usr = $computerSystem.UserName
            $Check = write " Hostname: " $computerSystem.Name, $newline, "Manufacturer: " $Man, $newline, "Model: " $Mod, $newline, "Serial: " $Ser, $newline, "No. CPU's: " $CPUs,$newline, "No. Cores's: " $Cores, $newline, "Memory: " $RAM, $newline, "HDD Size: " $HDD, $newline, "HDD Free Space: " $HDDF, $newline, "Operating System: " $OS, $newline,  "Built Date: " $OSB, $newline, "User Logged in: " $Usr
            $outputBoxHW.text=$Check
            }
 
            { 
               out-file $labelPing.text         
            }
             }
				           
############################################## End Check Hardware functions

############################################## Start Heading Label

$labelHead = New-Object Windows.Forms.Label
$labelHead.ForeColor = "darkgray"
$labelHead.Location = New-Object Drawing.Point 320,40
$labelHead.Size = New-Object Drawing.Point 300,60
$labelHead.text = "Desktop Information Tool"
$Form.Controls.Add($labelHead)

############################################## end heading Label

############################################## Start HostName Label

$labelHN = New-Object Windows.Forms.Label
$labelHN.Location = New-Object Drawing.Point 10,25
$labelHN.Size = New-Object Drawing.Point 120,20 
$labelHN.text = "Enter Hostname"
$Form.Controls.Add($labelHN)

############################################## end Hostname Label


############################################## Start Ping Label

$labelPing = New-Object Windows.Forms.Label
$labelPing.Location = New-Object Drawing.Point 10,382
$labelPing.Size = New-Object Drawing.Point 50,17 
$labelPing.text = "Pinging?"
$Form.Controls.Add($labelPing)

############################################## end Ping functions


############################################## Start Hardware Info Label

$labelquery = New-Object Windows.Forms.Label
$labelquery.Location = New-Object Drawing.Point 10,100
$labelquery.Size = New-Object Drawing.Point 170,17 
$labelquery.text = "Hardware Information"
$Form.Controls.Add($labelquery)

############################################## end Hardware Info Label


############################################## Start Version Label

$labelver = New-Object Windows.Forms.Label
$labelver.Location = New-Object Drawing.Point 725,450
$labelver.Size = New-Object Drawing.Point 30,30 
$labelver.text = "v1.0"
$Form.Controls.Add($labelver)

############################################## end Version Labe

############################################## Start text fields

$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(10,45)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$Form.Controls.Add($InputBox)

$outputBoxHW = New-Object System.Windows.Forms.TextBox
$outputBoxHW.Location = New-Object System.Drawing.Size(10,120)
$outputBoxHW.Size = New-Object System.Drawing.Size(325,245)
$outputBoxHW.MultiLine = $True
$Form.Controls.Add($outputBoxHW)

############################################## end text fields

############################################## Start Output Build Check Box

$outputBoxBC = New-Object System.Windows.Forms.TextBox
$outputBoxBC.Location = New-Object System.Drawing.Size(365,120)
$outputBoxBC.Size = New-Object System.Drawing.Size(325,245)
$outputBoxBC.MultiLine = $True
$Form.Controls.Add($outputBoxBC)

############################################## end Output Build Check Box

############################################## Start Ping Output box

$outputBoxping = New-Object System.Windows.Forms.TextBox
$outputBoxping.Location = New-Object System.Drawing.Size(70,380)
$outputBoxping.Size = New-Object System.Drawing.Size(50,20)
$Form.Controls.Add($outputBoxping)

############################################## end Ping Output box

############################################## Start Hardware buttons

$ButtonHW = New-Object System.Windows.Forms.Button
$ButtonHW.Location = New-Object System.Drawing.Size(180,30)
$ButtonHW.Size = New-Object System.Drawing.Size(75,50)
$ButtonHW.Text = "Check Hardware"
$ButtonHW.Add_Click({$outputBoxHW.Text=""})
$ButtonHW.Add_Click({$outputBoxping.Text=""})
$ButtonHW.Add_Click({checkhw})
$Form.Controls.Add($ButtonHW)

############################################## end Hardware buttons


############################################## Start Close buttons

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(700,413)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Close"
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
$Form.Controls.Add($cancelButton)

############################################## end Close buttons


$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

March 18, 2016 at 3:30 am

If you don't want external data that's fine. But the multi-replace is very inefficient and would be better served by the hash table in your function.

March 18, 2016 at 3:34 am

And I am confused at to your use of $labelping in your function checkhw as it is defined after checkhw and will not be available to it.

March 18, 2016 at 3:34 am

Yea i'll do that Bob.
Not sure how though 🙂
It's only required because Lenovo use a code instead of an easy name... All HP desktops display fine.

March 18, 2016 at 3:55 am

Thanks Bob

I got the Hash table working and removed the $labelping.

Only thing i'm stuck on now is it displaying test-connection false when the script stops

March 18, 2016 at 4:18 am

Did you "lift" this code from somewhere else? There are a lot of inefficiencies in it, and I'm just looking a the one checkhw function. For instance you make three separate calls to the WMI Win32_OperatingSystem calls when all you need is one and then split out the parameters (as was done with Win32_ComputerSystem).

March 18, 2016 at 4:37 am

Yes bob

I googled everything about this and changing it to suit my needs.. There is more to the code but has company info in it so not posting 🙂

I appriciate all the assistance you have given me.

March 18, 2016 at 6:30 am

Don't put the test connection inside the function.

function checkhw {
	
	param($computername)
	
	Write-Host $computername
	
}

foreach ($computername in $computernames) {
	
	if (Test-Connection $computername -Count 1 -Quiet) { checkhw $computername }
	
}