output information

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 7 years and 4 days 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
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

output information

Post by mqh77777 »

Product, version and build: 5.1.13
32 or 64 bit version of product: 64
Operating system: windows 7
32 or 64 bit OS: 32 & 64
PowerShell Version: 4

I have a field that gets the PC name. The variable is called $PC. I then want to find out all file owners on $PC. This code works perfect from PowerShell ISE but I cannot get it to display anything in my RichTextBox. I've also tried piping it to Out-Gridview and that also shows nothing.
Any guidance would be nice. thanks.

$buttonFindFileOwner_Click={
function Get-OpenFile
{
Param (
[string]$ComputerName
)

$openfiles = openfiles.exe /query /s $computerName /fo csv /V
$openfiles | ForEach-Object {
$line = $_
if ($line -match '","') {$line}

} | ConvertFrom-Csv
}

$Result = Get-OpenFile -ComputerName $PC | select-object 'Accessed By', '#Locks', 'Open Mode', 'Open File (Path\executable)'

$FileOwner = $Result | Out-String
$richtextbox_Output.AppendText($FileOwner)
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: output information

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: output information

Post by jvierra »

Your function is not in scope when you are calling it:
  1. function Get-OpenFile {
  2.     Param (
  3.         [string]$ComputerName
  4.     )
  5.    
  6.     $openfiles = openfiles.exe /query /s $computerName /fo csv /V
  7.     $openfiles | ForEach-Object {
  8.         $line = $_
  9.         if ($line -match '","') { $line }
  10.        
  11.     } | ConvertFrom-Csv
  12. }
  13.  
  14.  
  15. $buttonFindFileOwner_Click = {
  16.     $Result = Get-OpenFile -ComputerName $PC |
  17.         select-object 'Accessed By', '#Locks', 'Open Mode', 'Open File (Path\executable)'
  18.     $richtextbox_Output.AppendText($Result)
  19. }
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: output information

Post by mqh77777 »

Hello, I am not sure what you mean by "my function is not in scope" I tried your code and it does not produce anything in my RichTextBox window. Also, I don't see how your steps work. if I move my function outside of the ButtonClick it did not work. Also, I only want this Function to run if the button is clicked.
thank you.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: output information

Post by jvierra »

A function inside of a button click does not run and it is not in scope for any other event. It is local only.

see: HELP about_scopes
This topic is 7 years and 4 days 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