Author Posts

April 15, 2016 at 6:20 am

The command

 openfiles /query /s FILESERVER 

outputs a table as follows:

ID Accessed By Type Open File (Path\executable)
======== ==================== ========== =====================================
36910428 USERNAME Windows D:\..\File.txt
30199644 USERNAME Windows D:\..\File329.xlsx
37581731 USERNAME Windows D:\..\model_4484.doc
12752424 USERNAME Windows D:\..\TestFolder
28187627 USERNAME Windows D:\..\TestFolder2
232448 USERNAME Windows D:\..\Temp55

Storing this table as an array (i.e., $OpenFiles = @(openfiles /query /s FILESERVER)) lets me access the rows like this:

$OpenFiles[0] –> (blank line)
$OpenFiles[1] –> ID Accessed By Type Open File (Path\executable)
$OpenFiles[2] –> ======== ==================== ==========
$OpenFiles[3] –> 36910428 USERNAME Windows D:\..\File.txt
...
...
etc.

However, since this is not an object with properties (e.g., ID, Accessed By, etc., are not properties), I can't do:

$OpenFiles[3].AccessedBy –> USERNAME

How do I convert this table to a custom ps object so I can access values that way?

April 15, 2016 at 8:18 am

here is an example from http://stackoverflow.com/questions/30145539/openfiles-query-to-see-open-files

function Get-OpenFiles {
    cls
    openfiles /query /s $args[0] /fo csv /V | Out-File -Force C:\temp\openfiles.csv
    $search = $args[1]
    Import-CSV C:\temp\openfiles.csv | Select "Accessed By", "Open Mode", "Open File (Path\executable)" | Where-Object {$_."Open File (Path\executable)" -match $search} | format-table -auto
    Remove-Item C:\temp\openfiles.csv
}

April 15, 2016 at 9:18 am

Edit: My example was garbage, ugh. Use the /fo csv switch.

April 15, 2016 at 9:43 am

$openfiles = openfiles /query /S \\fileserver /FO csv | ConvertFrom-Csv
$openfiles

April 18, 2016 at 5:55 am

Thanks guys, using the /FO csv switch worked!