Experimenting with the Openfiles command via powershell

Zack
Zack used Ask the Experts™
on
Hi EE,

I want to write a PowerShell export the contents of the following query:

openfiles /v /query  /fo CSV

Open in new window


So far I have:

$Session = New-PSSession -ComputerName SERVERNAME -ConfigurationName OpenShareFileSearch

$sb = {
    
    openfiles /v /query  /fo CSV | 
        select -skip 8 |
        ConvertFrom-Csv | 
}

Invoke-Command -Session $Session -ScriptBlock $sb |
     Export-Csv C:\temp\FileSearchResults.csv

Open in new window


I am getting the following error message:

[ict00441] Connecting to remote server failed with the following error message : The WinRM client sent a request to an
HTTP server and got a response saying the requested HTTP URL was not available. This is usually returned by a HTTP serv
er that does not support the WS-Management protocol. For more information, see the about_Remote_Troubleshooting Help to
pic.
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [], PSRemotingTransportExc
   eption
    + FullyQualifiedErrorId : PSSessionOpenFailed
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Supply an argument tha
t is not null or empty and then try the command again.
At C:\temp\filename.ps1:10 char:24
+ Invoke-Command -Session <<<<  $Session -ScriptBlock $sb |
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

Any assistance is welcome.

Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018
Commented:
The problem is that when you define a $SB you do need to create the $session variable inside the sb, I mean each script block ($sb) is like a separate script

So the solution would be:


$sb = {
[Cmdletbinding()]
Param(
    [parameter(mandatory=$true,position=0)]$computername
)
$Session = New-PSSession -ComputerName $computername -ConfigurationName OpenShareFileSearch
    
    openfiles /v /query  /fo CSV | 
        select -skip 8 |
        ConvertFrom-Csv  
}

Invoke-Command  -ScriptBlock $sb -property $computername |
     Export-Csv C:\temp\FileSearchResults.csv

Open in new window

Top Expert 2014

Commented:
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
That error makes it pretty clear that the problem is with your New-PsSession command.  I'm not sure why you would have created a new PS configuration, but try it with just the default.
$Session = New-PSSession -ComputerName SERVERNAME

Open in new window

And if you're not running other commands afterward that need to use the same session, then just use the -computername parameter of Invoke-Command (instead of -session) so that the session is torn down immediately after it's used.
ZackGeneral IT Goto Guy

Author

Commented:
Hi Jose,

Thank you very much for the explanation.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
Glad to help Zac :) I learned that the hard way :)
Top Expert 2014
Commented:
So, tell me in what version of PowerShell does the code that Jose posted work?

There are way too many things that are incomplete and/or wrong here, I'm not even sure how to address them all.

$sb = {
[Cmdletbinding()]
Param(
    [parameter(mandatory=$true,position=0)]$computername
)
$Session = New-PSSession -ComputerName $computername -ConfigurationName OpenShareFileSearch
    
    openfiles /v /query  /fo CSV | 
        select -skip 8 |
        ConvertFrom-Csv  
}

Invoke-Command  -ScriptBlock $sb -property $computername 

Invoke-Command : A parameter cannot be found that matches parameter name 'property'.
At line:15 char:34
+ Invoke-Command  -ScriptBlock $sb -property $computername
+                                  ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

Open in new window

The result is an error because there is no -property parameter for Invoke-Command.

If we assume that the -computername parameter was meant to be used, we get this:
PS C:\Users\user1> Invoke-Command  -ScriptBlock $sb -ComputerName $computername

cmdlet  at command pipeline position 1
Supply values for the following parameters:
computername:

Open in new window

This is because now the scriptblock is requiring that a value be supplied to its -computername parameter.

To pass an argument to a remote scriptblock you would use the -ArgumentList parameter.  If we do that then we get something like this:
PS C:\Users\user1> Invoke-Command  -ScriptBlock $sb -ComputerName $computername -ArgumentList $computername
[server1] Connecting to remote server server1 failed with the following error message : Access is denied. For more
information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
   gTransportException
    + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed
    + PSComputerName        : server1

Open in new window

...followed by results from the openfiles command.  You will see an error like that unless you actually have the OpenShareFileSearch PS configuration created on the remote computer, in which case you would only see the results of the openfiles command.  This is because the
$Session = New-PSSession -ComputerName $computername -ConfigurationName OpenShareFileSearch
command produces no output.  However, in that scenario, the session created by the above New-PsSession command isn't even being used!

PS C:\Users\user1> $sb = {
>>     openfiles /v /query  /fo CSV |
>>         select -skip 8 |
>>         ConvertFrom-Csv
>> }
PS C:\Users\user1> Invoke-Command  -ScriptBlock $sb -ComputerName $computername


Hostname                    : server1
ID                          : 1342226432
Accessed By                 : user2
Type                        : Windows
#Locks                      : 0
Open Mode                   : Read
Open File (Path\executable) : e:\somefolder\blah
PSComputerName              : server1
RunspaceId                  : 758cce34-5823-4adf-8d7f-9c352fc8f225

Open in new window

...produces actual results.  You could use the -Session parameter instead of the -Computername parameter in the above command if you reference an established remote session (like I mentioned in my first post).  This is shown below:
PS C:\Users\user1> $sb = {
>>     openfiles /v /query  /fo CSV |
>>         select -skip 8 |
>>         ConvertFrom-Csv
>> }
PS C:\Users\user1> $Session = New-PSSession -ComputerName server1
PS C:\Users\user1> Invoke-Command  -ScriptBlock $sb -Session $Session


Hostname                    : server1
ID                          : 1342226432
Accessed By                 : user2
Type                        : Windows
#Locks                      : 0
Open Mode                   : Read
Open File (Path\executable) : e:\somefolder\blah
PSComputerName              : server1
RunspaceId                  : 758cce34-5823-4adf-8d7f-9c352fc8f225

Open in new window


Sorry, Jose, but your explanation just doesn't make sense in any way I look at it.
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
It's ok if you want the answer bud, I don't mind.
I used to test the code, but I didn't have the time to do so this week. so fine if the author benefits from it :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial