Invoke Command - File server

Hello,

I am currently performing a powershell script as part of closing locked files.
View the files on a file server then choose the file to close.

I have a little problem with this script, I can’t see the result of the GET when I add the second Invoke.
I tried a Start-Sleep etc but no results.

$fileserver = Read-Host "Merci de renseigner le File_Server  " 


Invoke-Command -ComputerName $fileserver -ScriptBlock {

   $name = Read-Host "Merci de renseigner le nom de l'utilisateur "    
  Get-SmbOpenFile | Where-Object -Property ClientUserName -Contains "$name"
 
 }
 
 Invoke-Command -ComputerName $fileserver -ScriptBlock {

   $sessionid = Read-Host "Merci de renseigner la session ID a fermer "
  Close-SmbOpenFile -FileId $sessionid
 }

Best regards

Why don’t you run it in one Invoke-Command? … like this?

$fileserver = Read-Host "Merci de renseigner le File_Server "

Invoke-Command -ComputerName $fileserver -ScriptBlock {
    $name = Read-Host "Merci de renseigner le nom de l’utilisateur "
    Get-SmbOpenFile | Where-Object -Property ClientUserName -Contains $name | Format-Table

    $sessionid = Read-Host "Merci de renseigner la session ID a fermer "
    Close-SmbOpenFile -FileId $sessionid
}

BTW: when you post code, error messages, sample data or console output format it as code, please.
Here you can read how that works: Guide to Posting Code.
https://forums.powershell.org/t/guide-to-posting-code-redux/15797/3

Thank for your reply. And your explication #template.

Because it’s the same situation.

Ah … I see. There’s an easy trick how to trick the output optimization of Powershell. Simply add a format cmdlet like Format-Table. I updated my code suggestion in my first answer. Try it now. :wink:

Thank you very much . I’m disgusting :cold_sweat: . too much time wasted for that !!!

Did you try to search for it online? There are a lot of posts in forums out there asking the same. :wink:

1 Like

Yes , for example : multiple invoke-command , no display of a get

:smiley:
I tried this:
https://www.google.com/search?client=firefox-b-e&ei=8Eg_YKGfC8aRsAfG563oBQ&q=powershell+runs+cmdlets+not+in+order&oq=powershell+runs+cmdlets+not+in+order&gs_lcp=Cgdnd3Mtd2l6EAM6BQgAEM0COgYIABAWEB46BwghEAoQoAE6CAghEBYQHRAeOgQIIRAKUKTsEFiQ0xFguNURaAFwAHgAgAGXAYgBpw-SAQQwLjE2mAEAoAEBqgEHZ3dzLXdpesABAQ&sclient=gws-wiz&ved=0ahUKEwih18u12pPvAhXGCOwKHcZzC10Q4dUDCAw&uact=5

… and then I found this:

1 Like