misc-powershell icon indicating copy to clipboard operation
misc-powershell copied to clipboard

Get-FileLockProcess: Add support for PS 7.0

Open holc-tkomp opened this issue 2 years ago • 2 comments

Support PowerShell 7.0 (and above?) in Get-FileLockProcess.

Issue

Trying to run current Get-FileLockProcess on PS 7.0.4 throws the following:

PowerShell 7.0.4 output
Add-Type:
Line |
 200 |              Add-Type -ReferencedAssemblies $ReferencedAssemblies -Typ …
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | (87,36): error CS1069: The type name 'Process' could not be found in the namespace 'System.Diagnostics'. This type has been forwarded to assembly 'System.Diagnostics.Process, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
                static public List<Process> WhoIsLocking(string path)
                                   ^

Add-Type:
Line |
 200 |              Add-Type -ReferencedAssemblies $ReferencedAssemblies -Typ …
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | (91,26): error CS1069: The type name 'Process' could not be found in the namespace 'System.Diagnostics'. This type has been forwarded to assembly 'System.Diagnostics.Process, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
                    List<Process> processes = new List<Process>();
                         ^

Add-Type:
Line |
 200 |              Add-Type -ReferencedAssemblies $ReferencedAssemblies -Typ …
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | (91,56): error CS1069: The type name 'Process' could not be found in the namespace 'System.Diagnostics'. This type has been forwarded to assembly 'System.Diagnostics.Process, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
                    List<Process> processes = new List<Process>();
                                                       ^

Add-Type:
Line |
 200 |              Add-Type -ReferencedAssemblies $ReferencedAssemblies -Typ …
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | (124,54): error CS1069: The type name 'Process' could not be found in the namespace 'System.Diagnostics'. This type has been forwarded to assembly 'System.Diagnostics.Process, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
                                processes = new List<Process>((int)pnProcInfo);
                                                     ^

Add-Type:
Line |
 200 |              Add-Type -ReferencedAssemblies $ReferencedAssemblies -Typ …
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | (132,55): error CS0103: The name 'Process' does not exist in the current context
                                        processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId));
                                                      ^

Add-Type:
Line |
 200 |              Add-Type -ReferencedAssemblies $ReferencedAssemblies -Typ …
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot add type. Compilation errors occurred.
InvalidOperation:
Line |
 206 |          $Result = [MyCore.Utils.FileLockUtil]::WhoIsLocking($FilePath …
     |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to find type [MyCore.Utils.FileLockUtil].

holc-tkomp avatar May 23 '22 07:05 holc-tkomp

I'm using the latest update: https://aka.ms/PowerShell-Release?tag=v7.2.4

Sorry for not getting back to you earlier but it comes back with this error:

Add-Type: W:\SS_CODING\PowerShell\Functions\Get-FileLockProcess.ps1:218
Line |
 218 |        Add-Type -ReferencedAssemblies $ReferencedAssemblies -TypeDefin …
     |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | (1,19): error CS0234: The type or namespace name 'CSharp' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)   using Microsoft.CSharp;
     | ^

Add-Type: W:\SS_CODING\PowerShell\Functions\Get-FileLockProcess.ps1:218
Line |
 218 |        Add-Type -ReferencedAssemblies $ReferencedAssemblies -TypeDefin …
     |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot add type. Compilation errors occurred.

InvalidOperation: W:\SS_CODING\PowerShell\Functions\Get-FileLockProcess.ps1:224
Line |
 224 |      $Result = [MyCore.Utils.FileLockUtil]::WhoIsLocking($FilePath)
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to find type [MyCore.Utils.FileLockUtil].

stephen147 avatar Jun 01 '22 18:06 stephen147

I just installed 7.2.4 and it does work for me. I remember I got similar, or even the exact same error, when I tried to call this method from within MSBuild (building a .csproj in Visual Studio, which executed PS script), but didn't bother to investigate what's the issue.

Host info
PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.4
PSEdition                      Core
GitCommitId                    7.2.4
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS> Get-Host

Name             : ConsoleHost
Version          : 7.2.4
InstanceId       : feb6ba0d-984f-4a01-9acd-435537cfee19
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : pl-PL
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

holc-tkomp avatar Jun 15 '22 11:06 holc-tkomp