Convert Table to Custom PS Object

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Brandon Lashmet Brandon Lashmet 5 months, 1 week ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #37841
    Profile photo of Brandon Lashmet
    Brandon Lashmet
    Participant

    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?

    #37848
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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
    }
    
    #37851
    Profile photo of tommymaynard
    tommymaynard
    Participant

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

    #37852
    Profile photo of random commandline
    random commandline
    Participant
    $openfiles = openfiles /query /S \\fileserver /FO csv | ConvertFrom-Csv
    $openfiles
    
    #37931
    Profile photo of Brandon Lashmet
    Brandon Lashmet
    Participant

    Thanks guys, using the /FO csv switch worked!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.