output issues

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.
User avatar
mqh777
Posts: 230
Joined: Tue Feb 25, 2014 1:01 pm

output issues

Post by mqh777 » Mon Oct 29, 2018 1:25 pm

We have a PowerShell Studio form that collects lots of data from machines. We are trying to gather TPM information from machines. So I added a button that has this code behind it.

Code: Select all

$buttonTPMStatus_Click={
$statusbar1.text = 'Getting TPM status, please wait...'
$richtextbox_output.Clear()
$TPM = Invoke-Command -ComputerName $TextPC -ScriptBlock {
Get-Tpm | Select-Object PSComputerName, RunspaceId, TpmPresent, TpmReady, ManufacturerId, ManufacturerIdTxt, ManufacturerVersion, ManufacturerVersionFull20, ManagedAuthLevel, OwnerAuth, OwnerClearDisabled, AutoProvisioning, LockedOut, LockoutHealTime, LockoutCount, LockoutMax } | Out-String
$richtextbox_output.AppendText($TPM)
$statusbar1.text = 'All done!'
}
When I run this it only returns the following in the RichTextBox
TpmPresent : True
TpmReady : False
ManufacturerId : 1464156928
ManufacturerIdTxt : WEC

Why? When I run the same code in PowerShell ISE it returns this.

Code: Select all

TpmPresent                : True
TpmReady                  : False
ManufacturerId            : 1464156928
ManufacturerIdTxt         : WEC 
ManufacturerVersion       : 5.81 
ManufacturerVersionFull20 : Not Supported for TPM 1.2
ManagedAuthLevel          : Full
OwnerClearDisabled        : True
AutoProvisioning          : Enabled
LockedOut                 : False
LockoutHealTime           : Not Supported for TPM 1.2
LockoutCount              : Not Supported for TPM 1.2
LockoutMax                : Not Supported for TPM 1.2
PSComputerName            : bo-fao4r46ch2-d
RunspaceId                : a0970be0-1e88-46a1-8169-4525f5a2f02f

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: output issues

Post by jvierra » Mon Oct 29, 2018 2:05 pm

Works fine for me. Perhaps you need a bigger textbox.

I would do it this way:

Code: Select all

$props = @(
    'PSComputerName', 
    'RunspaceId', 
    'TpmPresent', 
    'TpmReady', 
    'ManufacturerId', 
    'ManufacturerIdTxt', 
    'ManufacturerVersion', 
    'ManufacturerVersionFull20', 
    'ManagedAuthLevel', 
    'OwnerAuth', 
    'OwnerClearDisabled', 
    'AutoProvisioning', 
    'LockedOut', 
    'LockoutHealTime', 
    'LockoutCount', 
    'LockoutMax'
)
$buttonGetTPM_Click={
    $statusbar1.text = 'Getting TPM status, please wait...'
    $richtextbox_output.Clear()
    $tpm_lines = Invoke-Command -ComputerName $computer.Text -ScriptBlock {Get-Tpm } | 
            Select-Object $props | Out-String
    $richtextbox_output.Lines = $tpm_lines
    $statusbar1.text = 'All done!'
}

User avatar
mqh777
Posts: 230
Joined: Tue Feb 25, 2014 1:01 pm

Re: output issues

Post by mqh777 » Mon Oct 29, 2018 2:11 pm

using your code my output looks like this:

Code: Select all

PSComputerName            : mypc
RunspaceId                : f0b08ec5-02d8-4a4c-8b71-d3ead76f3840
TpmPresent                : True
TpmReady                  : False
ManufacturerId            : 1464156928
ManufacturerIdTxt         : WEC
My richtextbox output window is big enough. Plus it will scroll. I have other buttons that dump hundreds of lines into it and they all display.

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: output issues

Post by jvierra » Mon Oct 29, 2018 2:21 pm

Post the exact code you used. My code does not do that when I run it.

User avatar
mqh777
Posts: 230
Joined: Tue Feb 25, 2014 1:01 pm

Re: output issues

Post by mqh777 » Mon Oct 29, 2018 2:30 pm

I have put the array both inside and outside of the ButtonClick Event. I get the same results.

Code: Select all

$props = @(
	'PSComputerName',
	'RunspaceId',
	'TpmPresent',
	'TpmReady',
	'ManufacturerId',
	'ManufacturerIdTxt',
	'ManufacturerVersion',
	'ManufacturerVersionFull20',
	'ManagedAuthLevel',
	'OwnerAuth',
	'OwnerClearDisabled',
	'AutoProvisioning',
	'LockedOut',
	'LockoutHealTime',
	'LockoutCount',
	'LockoutMax'
)

$buttonTPMStatus_Click = {
		$statusbar1.text = 'Getting TPM status, please wait...'
		$richtextbox_output.Clear()
		$tpm_lines = Invoke-Command -ComputerName $PCNameBox.Text -ScriptBlock { Get-Tpm } |
		Select-Object $props | Out-String
		$richtextbox_output.Lines = $tpm_lines
		$statusbar1.text = 'All done!'

}

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: output issues

Post by jvierra » Mon Oct 29, 2018 2:34 pm

Works fine for me. Set all RTB properties back to default and try again. Be sure "Multiline" is set to $true.

User avatar
mqh777
Posts: 230
Joined: Tue Feb 25, 2014 1:01 pm

Re: output issues

Post by mqh777 » Tue Oct 30, 2018 6:56 am

Product: PowerShell Studio 2018 (64 Bit)
Build: v5.5.155
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.16299.0

I created a brand new form that has 1 button and 1 richtextbox. All RTB properties are set to default.

I have this code.

Code: Select all

$button1_Click={
	if (test-path c:\temp\tpm.txt) { remove-item c:\temp\tpm.txt }
	$statusbar1.text = 'Getting TPM status, please wait...'
	$richtextbox1.Clear()
	Invoke-Command -ComputerName "MyComputer" -ScriptBlock { Get-Tpm } | Out-File c:\temp\TPM.txt
	$GetTPMData = Get-Content c:\temp\tpm.txt
	$GetTPMData | Out-String
	$richtextbox1.AppendText($GetTPMData)
	$statusbar1.text = 'All done!'
}
The TPM.txt file gets created and looks like this: (note: there are several blank lines at the top)

Code: Select all



PSComputerName            : MyComputer
RunspaceId                : eea87612-e378-4e3e-ba53-3aa7b6ddfc76
TpmPresent                : True
TpmReady                  : False
ManufacturerId            : 1464156928
ManufacturerIdTxt         : WEC 
ManufacturerVersion       : 5.81 
ManufacturerVersionFull20 : Not Supported for TPM 1.2
ManagedAuthLevel          : Full
OwnerAuth                 : 
OwnerClearDisabled        : False
AutoProvisioning          : Enabled
LockedOut                 : False
LockoutHealTime           : Not Supported for TPM 1.2
LockoutCount              : Not Supported for TPM 1.2
LockoutMax                : Not Supported for TPM 1.2
SelfTest                  : {255, 1, 2, 5...}
Yet what is displayed in the RTB is this:

Code: Select all

  PSComputerName            : MyComputer RunspaceId                : 4c267e6d-27b7-4b7c-b7e9-ed5358ac1331 TpmPresent                : True TpmReady                  : False ManufacturerId            : 1464156928 ManufacturerIdTxt         : WEC

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: output issues

Post by jvierra » Tue Oct 30, 2018 8:06 am

Yes - the TPM object cannot be displayed in a form correctly. I can reproduce the results.

The issue is that the middle of the results contains a large string of null characters which breaks the output. Open the file in a binary editor to see what I mean.

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: output issues

Post by jvierra » Tue Oct 30, 2018 8:13 am

Here is a work-around:

Code: Select all

$buttonGetTPM_Click={
    $statusbar1.text = 'Getting TPM status, please wait...'
    $tpm_lines = Invoke-Command -ComputerName alpha -ScriptBlock {  Get-Tpm }     
    $richtextbox_output.Lines = $tpm_lines[0] | Out-String | %{$_ -replace '\0'}
    $statusbar1.text = 'All done!'
}

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: output issues

Post by jvierra » Tue Oct 30, 2018 4:34 pm

Also this works:

Code: Select all

$props = @(
    'PSComputerName', 
    'RunspaceId', 
    'TpmPresent', 
    'TpmReady', 
    'ManufacturerId', 
    'ManufacturerIdTxt', 
    #'ManufacturerVersion', 
    #'ManufacturerVersionFull20', 
    'ManagedAuthLevel', 
    'OwnerAuth', 
    'OwnerClearDisabled', 
    'AutoProvisioning', 
    'LockedOut', 
    'LockoutHealTime', 
    'LockoutCount', 
    'LockoutMax'
)
$buttonGetTPM_Click={
    $statusbar1.text = 'Getting TPM status, please wait...'
    $tpm_lines = Invoke-Command -ComputerName MyPC-ScriptBlock { Get-Tpm }      
    $richtextbox_output.Lines = $tpm_lines | Select $props |Out-String 
    $statusbar1.text = 'All done!'
}

Locked