Experimenting with the Openfiles command via powershell

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.
ZackGeneral IT Goto GuyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroEE Solution Guide/Topic Advisor and CEO Faru Bonon ITCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
footechCommented:
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 GuyAuthor Commented:
Hi Jose,

Thank you very much for the explanation.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Jose Gabriel Ortega CastroEE Solution Guide/Topic Advisor and CEO Faru Bonon ITCommented:
Glad to help Zac :) I learned that the hard way :)
footechCommented:
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 CastroEE Solution Guide/Topic Advisor and CEO Faru Bonon ITCommented:
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 :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.