none
Help with calling a function

    Question

  • I have a function that works.   But it only works when I run it in PowerShell ISE.   

    $pc = Read-Host "Enter a computer name" 

    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
    }


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

    When I save this script and place it on a network share then call it using Invoke-Command I can't get it to work.  I've tried a few things and it either returns nothing or I get an error saying it needs a paramiter for /s.   

    How do I call this using Invoke-Command and have it run?  I've tried the Prompt both in the parent script and in the called Function script and it does not seem to work.


    mqh7

    Friday, April 21, 2017 3:18 PM

Answers

  • To run on a remote computer just do this:

    (openfiles.exe /S remotepc /fo csv /query|?{$_[0] -eq '"'})|convertFrom-Csv


    \_(ツ)_/

    • Marked as answer by mqh7 Friday, April 21, 2017 6:10 PM
    Friday, April 21, 2017 5:43 PM
    Moderator

All replies

  • is this the exact verbatim copy pasta from your code?

    what does $pc go to?

    why $ComputerName and $computerName?

    Friday, April 21, 2017 4:10 PM
  • Wrong function:

    (openfiles.exe /fo csv /query|?{$_[0] -eq '"'})|convertFrom-Csv


    \_(ツ)_/

    Friday, April 21, 2017 4:27 PM
    Moderator
  • it is self explanatory what each variable does.   This is how functions work.   copy/paste this code and run it in ISE and it works.    I need to know how to make it work when I call it with Invoke-Command.  

    mqh7

    Friday, April 21, 2017 4:29 PM
  • invoke-command {(openfiles.exe /fo csv /query|?{$_[0] -eq '"'})|convertFrom-Csv}

    \_(ツ)_/

    Friday, April 21, 2017 4:36 PM
    Moderator
  • ok, using your script how do I run it on a remote computer?   Your code does not support a -ComputerName switch for Invoke-Command.    I want to be able to have any user run this, enter a remote computer name and then find out all the open files on that remote machine and who has them open.

    thank you.  


    mqh7

    Friday, April 21, 2017 5:39 PM
  • To run on a remote computer just do this:

    (openfiles.exe /S remotepc /fo csv /query|?{$_[0] -eq '"'})|convertFrom-Csv


    \_(ツ)_/

    • Marked as answer by mqh7 Friday, April 21, 2017 6:10 PM
    Friday, April 21, 2017 5:43 PM
    Moderator
  • No, that did not work.   I get this error:    Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
    At line:10 char:1
    + invoke-command (openfiles.exe /s "RemoteServer" /fo csv /query|?{$_[ ...

    OpenFiles.exe is not a PowerShell command.  is there an equivalent command in PowerShell?  


    mqh7

    Friday, April 21, 2017 5:57 PM
  • sorry, sorry..... I did not have the brackets {}    Now it works :-)

    mqh7

    Friday, April 21, 2017 5:58 PM
  • You do not need to use Invoke-Command and should not use it here.  Just execute this line in PowerShell:

    (openfiles.exe /S remotepc /fo csv /query|?{$_[0] -eq '"'})|convertFrom-Csv

    Invoke is used to execute commands remotely  "Openfiles" does its own remoting.


    \_(ツ)_/

    Friday, April 21, 2017 6:12 PM
    Moderator