Why does this not display the results in the output window

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
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
daviesg6
Posts: 49
Joined: Thu Nov 21, 2013 12:36 pm

Re: Why does this not display the results in the output wind

Post by daviesg6 » Fri Nov 21, 2014 3:29 pm

I think you have misunderstood what I am trying to do. I do not want out-string to set a value, I am trying to get the value created by the code you provided to get the last logon time displayed in the text box. I know out-string cannot set a value it is just designed to output a value provided by other code to a destination. The advice you have given is valid, but does not work if I want it to use a value entered in one box, run a query on it and output the value generated by this to another location.

Value A is entered in location 1.
The query is run on value A, in this case it looks for the last logon time, call this value B. Value B is then piped out and Value B is sent to location 2.

using the code you provided does not allow this to work, or at least I can't get it to work. I am also still unable to agree with you that this method does not work because I have it working on many other GUIs, and other buttons within this one. If you just copy the full script and run it you will see, I gave the details of which lines contain the relevant code so you do not have to pick through all of it, especially as mot of it is just the window dressing that generates the GUI, the first 3 sections are what you need to look at to get this, and the actual script in question is just 10 lines in the third section.

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

Re: Why does this not display the results in the output wind

Post by jvierra » Fri Nov 21, 2014 9:15 pm

I cannot understand what you are asking. I recommend spending some time learning more of the fundamentals of PowerShell. THere are many good books and videos that can help you understand how this scripting system works.

Windows forms can be very difficult for non-technical people to comprehend. Forms use many advanced programming techniques like "state" management and "events". "Events" have a hierarchy and the methods used to interconnect events to controls can be very confusing to anyone who is a programmer but even more confusing to non-programmers.

I have worked with and taught experienced programmers how to program WIndows forms for close to 20 years. I know how hard it is for many programmers to understand event driven systems and state machines. When someone who is only trained indesktop support tries to enter the realm it can get pretty compolicated.

Unfortunately this forum and most forums are not equipped to incrementally teach basic programming or basic Windows forms techniques. I can point you at basic resources that can help you undersgtand basic programming like simple assignment.

Let me know it this would be helpful.

User avatar
daviesg6
Posts: 49
Joined: Thu Nov 21, 2013 12:36 pm

Re: Why does this not display the results in the output wind

Post by daviesg6 » Mon Nov 24, 2014 1:41 pm

what I am asking is WHY this code
PowerShell Code
Double-click the code block to select all.
$LastLogonButton_OnClick= 
{
$results.Text=$logons=@()
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers |
    ForEach{
        $logonInt=(Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'" -server $_.Name -properties lastlogon).lastlogon
        $logons+=[datetime]::FromFileTime($logonInt)
    }

$newestLogon=($logons| Measure-Object -Max).Maximum 
$newestLogon | Out-String $results.Focus()
}
does not display the results in the $results text box (BEFORE YOU ANSWER SAYING THIS DOES NOT WORK PLEASE READ ON)
but this code
PowerShell Code
Double-click the code block to select all.
$IDSearchButton_OnClick= 
{
$results.Text=Get-ADUser -Filter "sAMAccountName -eq '$($EntryBox.text)'" -Properties DisplayName, sAMAccountName, mail, extensionattribute5, msExchHomeServerName, PasswordLastSet, PasswordExpired, PasswordNeverExpires, buMemberOf, msExchOmaAdminWirelessEnable, whenCreated | select givenName, surname, DisplayName, sAMAccountName, mail, extensionattribute5, msExchHomeServerName, PasswordLastSet, PasswordExpired, PasswordNeverExpires, buMemberOf, msExchOmaAdminWirelessEnable, whenCreated | Out-String
    $results.Focus()
}
DOES display the results in the $results text box.
Compare the code, apart from the actual query being run on AD it is the same instructions to powershell regarding where to post the results.
You keep telling me using Out-string will not do this BUT IT CLEARLY DOES IF YOU READ THE SECOND OF THESE CODE BLOCKS, you asked for an example that works, I have given it numerous times but you keep ignoring this and telling me to learn to code. I have created numerous primal forms based GUIs, all using out-string to display the results in the text box designated, all work apart from this 1 query so I know it works.

The one that does not work checks for the userID entered in the input text box, reads the last logon time recorded on each DC and is supposed to display the results in the $results text box. It does not display the results BUT it does clear the data already displayed in the $results text box so it is obviously doing something with the target text box, just not displaying the data there. So what I am asking is why this works for other queries and not this one, how do I get the results displayed in the text box it has cleared, which all other queries also do before displaying their results

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

Re: Why does this not display the results in the output wind

Post by jvierra » Mon Nov 24, 2014 1:59 pm

The answer is obvious if you learn how PowerShell and Forms work. YOu are trying to understand something but lack the funcamental knowledge required to see what it is that I am tryin to show. you.

I am sorry but I can offer no moreexamples other than what has been posted. If you are not able to understand mu explanation then I suggest finding someoeone elese such as a friend or co-worker to work with you to sort this out.

Simply put you have n o assignment statement of the form:
$textbox1.Text=<some non-null and non-empty value>.

I know no way to make it any simpler or more explicit.

User avatar
daviesg6
Posts: 49
Joined: Thu Nov 21, 2013 12:36 pm

Re: Why does this not display the results in the output wind

Post by daviesg6 » Tue Nov 25, 2014 9:26 am

you keep saying there has to be a value to display in the textbox, and I keep saying there is a value, but ok, I am going to make do with outputting to a file, which requires a value to populate the text file and works which is the issue I don't understand, and your insistence I need to specify something to display does not work because if this is the case and I am not giving this then why, if I tell it to out-file does it populate the file with data? I have the fundamental understanding of this, if I did not I would not have been able to create the forms already in use, using exactly the same base code as I am using here which you tell me does not work. Sorry, I have evidence it does work. and every time I mention this, and provide the code, you do not answer why it works but ask me for the pff file instead, which will not answer the question or provide the code that does not work.

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

Re: Why does this not display the results in the output wind

Post by jvierra » Tue Nov 25, 2014 9:50 am

If it goes to the file I don't understand why you won't just assign it to the textbox like I keep showing you.

Please. Just try this once without changing it with all of the SetFocus() stuff which does absolutely noting.

PowerShell Code
Double-click the code block to select all.
$LastLogonButton_OnClick= {
    [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers |  
        ForEach{
            $logonInt=(Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'" -server $_.Name -properties lastlogon).lastlogon        
             $logons+=[datetime]::FromFileTime($logonInt) 
      } 
      $results.Text=($logons| Measure-Object -Max).Maximum
}

User avatar
daviesg6
Posts: 49
Joined: Thu Nov 21, 2013 12:36 pm

Re: Why does this not display the results in the output wind

Post by daviesg6 » Tue Nov 25, 2014 10:11 am

Sorry, I thought I had posted why I was not using that, I had tried it previously and found I got numerous errors, it displayed a result but not the correct one. I was sure I had posted that while that displayed it errored and gave the wrong result but using the code I was trying normally gave the correct result seen if sent to a file) but did not display it.

Using the code provided I get these errors

Cannot convert argument "1", with value: "11/14/2014 4:53:34 PM", for "op_Addition" to type "System.TimeSpan": "Cannot convert value "11/14/2014 4:53:34 PM"
to type "System.TimeSpan". Error: "Invalid cast from 'System.DateTime' to 'System.TimeSpan'.""
At C:\Users\gdavies\Desktop\ServiceDesk.ps1:70 char:14
+ $logons+=[datetime]::FromFileTime($logonInt)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Cannot convert argument "1", with value: "11/25/2014 10:45:52 AM", for "op_Addition" to type "System.TimeSpan": "Cannot convert value "11/25/2014 10:45:52
AM" to type "System.TimeSpan". Error: "Invalid cast from 'System.DateTime' to 'System.TimeSpan'.""
At C:\Users\gdavies\Desktop\ServiceDesk.ps1:70 char:14
+ $logons+=[datetime]::FromFileTime($logonInt)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Cannot convert argument "1", with value: "11/25/2014 9:13:16 AM", for "op_Addition" to type "System.TimeSpan": "Cannot convert value "11/25/2014 9:13:16 AM"
to type "System.TimeSpan". Error: "Invalid cast from 'System.DateTime' to 'System.TimeSpan'.""
At C:\Users\gdavies\Desktop\ServiceDesk.ps1:70 char:14
+ $logons+=[datetime]::FromFileTime($logonInt)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Cannot convert argument "1", with value: "11/24/2014 1:26:53 PM", for "op_Addition" to type "System.TimeSpan": "Cannot convert value "11/24/2014 1:26:53 PM"
to type "System.TimeSpan". Error: "Invalid cast from 'System.DateTime' to 'System.TimeSpan'.""
At C:\Users\gdavies\Desktop\ServiceDesk.ps1:70 char:14
+ $logons+=[datetime]::FromFileTime($logonInt)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

and the result it gives is out by more than a week.

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

Re: Why does this not display the results in the output wind

Post by jvierra » Tue Nov 25, 2014 12:09 pm

Here start with this: It works on all systems.
PowerShell Code
Double-click the code block to select all.
function GetLastLogon{
    Param(
        $ComputerName=$env:COMPUTERNAME,
        $SamAccountName=$env:USERNAME
    )
    [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers |  
        ForEach{
            $logonInt=(Get-Aduser -Filter "sAMAccountName -eq '$SamAccountName'" -server $ComputerName -properties lastlogon).lastlogon
            Try{
                $logons+=[datetime]::FromFileTime($logonInt)
		    }
            Catch{
                [void][System.Windows.Forms.MessageBox]::Show('LastLogon value invalid','AD Error')
		    }   
        }
    ($logons|Measure-Object -Maximum).Maximum
}

$result.Text=GetLastLogon

User avatar
daviesg6
Posts: 49
Joined: Thu Nov 21, 2013 12:36 pm

Re: Why does this not display the results in the output wind

Post by daviesg6 » Tue Nov 25, 2014 2:22 pm

thanks, but this, as a standalone script, gives errors and reports the last logon value is invalid, even if it did work as it is, it does not allow me to search for a specific user and therefore would not be of use in the scenario I am looking to create. In addition, creating functions to perform a relatively simple search is a little over the top when I can use my original code to output to the native powershell window, to a file or to a printer and get the correct result.

After playing around with a hybrid of my code and some you provided I have come up with this
PowerShell Code
Double-click the code block to select all.
$LastLogonButton_OnClick= {
$logons=@()
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers |
    ForEach{
        $logonInt=(Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'" -server $_.Name -properties lastlogon).lastlogon
        $logons+=[datetime]::FromFileTime($logonInt)
    }


      $results.Text=($logons| Measure-Object -Max).Maximum
}
and find it actually gives me the correct result in the window I want to see it displayed.

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

Re: Why does this not display the results in the output wind

Post by jvierra » Tue Nov 25, 2014 4:32 pm

Well you don't have to use the scrip I posted so I guess I can't be of much help. Sorry but I am going to have to give up.

Good luck sorting this out.

Locked