Powershell Studio Script not returning data

Ask your 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.
This topic is 5 years and 9 months 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
Genx13
Posts: 12
Last visit: Wed Jun 16, 2021 5:29 am

Powershell Studio Script not returning data

Post by Genx13 »

Im trying to generate a CSV file from a remote PSession if I run the commands from PowerShell everything works fine If I run them from the Powershell Studio exported .ps1 script it generates a blank csv file. I can not get the .ps1 file to return any results. All variables also show correctly when I write-host or enter a value directly into the script. There are no errors generated around getting or writing data either. I have a feeling I'm just missing a Powershell Studio vs Powershell Syntax somewhere.

The Variables:
  1. $Session = New-PSSession -ComputerName SERVERNAME -ConfigurationName OpenShareFileSearch -Authentication Kerberos
  1. $filename=filenametxtb.Text
This is the command:
  1. Invoke-Command -Session $Session -ScriptBlock { $FileList = openfiles /v /query  /fo CSV /nh | findstr "$($using:FileName)"; $Header = "Hostname", "ID", "Accessed By", "Type", "#Locks", "Open Mode", "Open File (Path\executable)"; ConvertFrom-Csv -InputObject $FileList -Delimiter "," -Header $Header | Select "Accessed By", "Hostname", "Open File (Path\executable)" | Export-Csv "D:\Department Shares\IT Dept\6-Misc\FileShareCSVs\FileSearchResults.csv" -NoTypeInformation } -ArgumentList $FileName
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Studio Script not returning data

Post by jvierra »

Here is an example of how to use OpenFiles to get a specific file.

Code: Select all

$sb = {
    param($FileName)
    
    openfiles /v /query  /fo CSV | 
        select -skip 8 |
        ConvertFrom-Csv | 
        Where{$_.'Open File (Path\executable)' -match $FileName}
}

Invoke-Command -Session $Session -ScriptBlock $sb -ArgumentList $FileName |
     Export-Csv D:\Department Shares\IT Dept\6-Misc\FileShareCSVs\FileSearchResults.csv
User avatar
Genx13
Posts: 12
Last visit: Wed Jun 16, 2021 5:29 am

Re: Powershell Studio Script not returning data

Post by Genx13 »

Thanks jvierra that does work except the where statement isnt filtering correctly and I can't seem to get the correct operator.

What I want it to return is the file path with anything after it
  1. Where{$_.'Open File (Path\executable)' -match $FileName}
With this it is filtering something but if the value of $Filename is E:\Data\Some Folder Im getting all sorts of results like:
D:\data\corp\contact
F:\HomeDrive\someguy

Using -like and -contains return similar results
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Studio Script not returning data

Post by jvierra »

It returns the complete matching CSV rows.

You can also do this:

Where{$_.'Open File (Path\executable)' -match $FileName} | Select -Expand 'Open File (Path\executable)'
User avatar
Genx13
Posts: 12
Last visit: Wed Jun 16, 2021 5:29 am

Re: Powershell Studio Script not returning data

Post by Genx13 »

I was able to use part of the suggested solution but I could never get the results to return what I was actually looking for. Below is what I'm currently using and is filtering based off of the supplied variable. The $csv gets imported into a datagrid that displays the results. Thanks for all the help.

Code: Select all

$OpenFiles =
		{
	          openfiles /v /query  /fo CSV | 
	          select -skip 8 |
	          ConvertFrom-Csv  
	        }
	
Invoke-Command -Session $Session -ScriptBlock $OpenFiles | Select-Object "Accessed By", "Hostname", "Open File (Path\executable)" | Export-Csv $FileResultcsv
Remove-PSSession $Session
Get-Content $FileResultcsv | Select-String -SimpleMatch "$($FileName)" | Out-File $FilteredFileResultcsv -Enc ascii
$csv = Import-Csv -Path $FilteredFileResultcsv -Header "Accessed By", "Hostname", "Open File (Path\executable)" | Sort-Object "Open File (Path\executable)"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Studio Script not returning data

Post by jvierra »

For this you do not need a session. There is no need to export and import the CSV twice.

Code: Select all

$OpenFiles = {
    openfiles /v /query  /fo CSV | 
        select -skip 8 |
        ConvertFrom-Csv  
}
	
$csv = Invoke-Command -ComputerName $computer -ScriptBlock $OpenFiles | 
    Select-Object 'Accessed By', 'Hostname', @{n='Filename';e={$_.'Open File (Path\executable)'}}
$filtered = $csv | where{$_.Filename -match $filename}
$datagridview1.DataSource = [System.Collections.ArrayList]$filtered
This topic is 5 years and 9 months 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