1

I'm trying to close certain open files on a remote 2008 R2 fileserver with the following lines:

$FileList = Invoke-Command -Scriptblock { openfiles.exe /query /S fileserver01 /fo csv | where {$_.contains("sampletext")} }
foreach ($f in $FileList) {
    $fid = $f.split(',')[0]
    $fid = $fid -replace '"'
    Invoke-Command -ComputerName fileserver01 -ScriptBlock { net file $fid /close }
}

I can get this to work if I type it in, but when I throw it into a script, it doesn't close the files.

I've verified that the $FileList variable gets filled ($fid indeed gets the file id), so I don't think there's a execution policy that's blocking me. I'm not sure what it could be.

Tunaki
  • 132,869
  • 46
  • 340
  • 423

2 Answers2

1

I think its because your $fid variable exists only on your local session.

If you are on powershell 3 you can use "using", example:

Invoke-Command -ComputerName fileserver01 -ScriptBlock { net file $using:fid /close }

In powershell 2 you can use this:

How do I pass named parameters with Invoke-Command?

Community
  • 1
  • 1
wallybh
  • 934
  • 1
  • 11
  • 28
  • Do you know of any documentation for the $using command? I couldn't get it to work. I also tried throwing the command in a string and using the string, but that didn't help either. For example: $netfile = "net file $fid /close" – michaewlewis May 29 '15 at 22:20
  • if does not work. Probably you are on a powershell v2 our v1. Then you have to use de second option. – wallybh May 29 '15 at 22:32
  • I upgraded to version 4.0 from v2 this morning because I thought I might be running into a version problem.... :/ – michaewlewis May 29 '15 at 22:40
  • strange. Have you tried replace $fid to $using:fid in your last line? – wallybh May 29 '15 at 22:41
  • yup... sure have..... Here's my whole script.... maybe you can find something wrong with it that I've overlooked. http://pastebin.com/uw3mUfSy – michaewlewis May 29 '15 at 22:53
  • Does anyone know of some documentation for the "using" command/statement? I'm trying to find something, but since "using" is such a common word with other "uses", I'm having trouble getting anything "useful". – michaewlewis Jun 01 '15 at 15:58
  • @michaewlewis: `Get-Help about_Scopes` and `Get-Help about_Remote_Variables`. – mklement0 Mar 17 '16 at 19:38
1

The problem is that the $fid variable in the script block is a different variable than the $fid variable in your foreach loop. You need to pass it in as an argument, like so:

foreach ($f in $FileList) {
    $fid = $f.split(',')[0]
    $fid = $fid -replace '"'
    Invoke-Command -ComputerName fileserver01 -ArgumentList $fid -ScriptBlock {
        param ($fid)
        net file $fid /close
    }
}

Whenever I do something like this, I usually choose a different variable name inside the script block, to avoid confusion. The arguments in the -ArgumentList parameter get matched one-by-one to the variables in the param section, so in this example, $fid gets mapped to $fileId:

foreach ($f in $FileList) {
    $fid = $f.split(',')[0]
    $fid = $fid -replace '"'
    Invoke-Command -ComputerName fileserver01 -ArgumentList $fid -ScriptBlock {
        param ($fileId)
        net file $fileId /close
    }
}
JamesQMurphy
  • 4,214
  • 1
  • 36
  • 41