I have a form I have been using for some time, but the servicedesk team have now asked if, rather than just outputting the results to the screen, I can have the results saved to the clipboard so they can paste the results into tickets or email.
For most of the options I have been able to do this simply by piping the results to clip, however, I have one function (see below) that I cannot seem to get to do this. Can anyone help?
The script this button runs is:
$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
}
This checks all of the DCs and pulls the last logon for the user account. Right now it outputs the results to a window in the form ($results), I want to change this so it outputs to clipboard but I can't figure out how to do this, I'm having a bit of a blonde moment!
Output to clipboard
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.
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.
- techMonkie
- Posts: 4
- Last visit: Mon Nov 14, 2016 7:32 am
- techMonkie
- Posts: 4
- Last visit: Mon Nov 14, 2016 7:32 am
Re: Output to clipboard
Ok, I have tried editing the script using the suggested code above, and now I get no output. Nothing is saved to the clipboard or screen and no errors are reported.
I don't know where to put the code you provided, I am not a powershell expert, I know some but not enough to be anything but a novice so any help is appreciated
Where in this script should I put the line provided - [System.Windows.Forms.Clipboard]::SetText($results.Text) ?
Original script:
$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
}
I don't know where to put the code you provided, I am not a powershell expert, I know some but not enough to be anything but a novice so any help is appreciated
Where in this script should I put the line provided - [System.Windows.Forms.Clipboard]::SetText($results.Text) ?
Original script:
$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
}
Re: Output to clipboard
For future reference, please place code between code blocks to make it easier for us to read your code and help you figure out the issue. To do this simply click the code box just above where you type out your reply:
If you wish to keep the output in the $results control as well as your clipboard, you should be able to add the clipboard line just below the previously mentioned line and it'll also be placed on your clipboard. Your ending event would look similar to this:
That aside, it appears as though you do your output with this line here:
- $results.Text = ($logons | Measure-Object -Maximum).Maximum
- $LastLogonButton_OnClick = {
- $logons = @()
- [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers |
- ForEach-Object{
- $logonInt = (Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'" -server $_.Name -properties lastlogon).lastlogon
- $logons += [datetime]::FromFileTime($logonInt)
- }
- $results.Text = ($logons | Measure-Object -Maximum).Maximum
- [System.Windows.Forms.Clipboard]::SetText($results.Text)
- }
DevinL
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Output to clipboard
I've been using the following function for a few years now and its always worked for me.
HTH
- function Out-Clipboard {
- <#
- .SYNOPSIS
- Sends the given input to the Windows clipboard.
- .EXAMPLE
- dir | Out-Clipboard
- This example sends the view of a directory listing to the clipboard
- .EXAMPLE
- Out-Clipboard "Hello World"
- This example sets the clipboard to the string, "Hello World".
- #>
- [CmdletBinding()] [OutputType()]
- Param(
- [Parameter(ValueFromPipeline)] [PSObject] $InputObject
- )
- BEGIN { $ObjectsToProcess = @() }
- PROCESS { $ObjectsToProcess += $InputObject }
- END {
- ## Launch a new instance of PowerShell in STA mode.
- ## This lets us interact with the Windows clipboard.
- $ObjectsToProcess | PowerShell -NoProfile -STA -Command {
- Add-Type -Assembly PresentationCore
- ## Convert the input objects to a string representation
- $clipText = ($Input | Out-String -Stream) -join [environment]::NewLine
- ## And finally set the clipboard text
- [Windows.Clipboard]::SetText($clipText)
- }
- }
- } # Out-Clipboard
- techMonkie
- Posts: 4
- Last visit: Mon Nov 14, 2016 7:32 am
Re: Output to clipboard
Thank you both, I now have a working script.
The function looks like it will be useful for something else.
Much obliged.
The function looks like it will be useful for something else.
Much obliged.
Re: Output to clipboard
Pipeline input is not necessary. To pipeline simply add it to a pipeline.
You cannot ever use the clipboard in a pipeline. It can only handle on item at a time. Writing a second item removes the first item. Your code just accumulates the input for a single write.
The question was how to transfer the contents of one textbox to the clipboard. While you function is not wrong but it is unnecessary in this particular case as one line is all that is needed.
You cannot ever use the clipboard in a pipeline. It can only handle on item at a time. Writing a second item removes the first item. Your code just accumulates the input for a single write.
The question was how to transfer the contents of one textbox to the clipboard. While you function is not wrong but it is unnecessary in this particular case as one line is all that is needed.