PowerShell / PowerShell/PowerShell

"Unable to cast PathInfo to String" when assigning to shell shortcut property

Abierto
#16,926 7 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Issue-Bug KeepOpen WG-Engine
Lenguaje dominante
C#
Estrellas
55.5k
Forks
8.5k
Merge medio
1 d 2 h
PR fusionados (30 d)
88

Descripción

Prerequisites
Steps to reproduce

The following function creates a new shortcut (.lnk) using a Shell COM object, and then attempts to set the working directory property (without saving the shortcut).

function fn([switch]$Resolve) {
    $WorkingDirectory = if ($Resolve) {Resolve-Path "."} else {"."}
    $Shell = (New-Object -ComObject "WScript.Shell")
    # the shortcut is not actually created
    $Shortcut = $Shell.CreateShortcut("C:\shortcut.lnk")
    $Shortcut.WorkingDirectory = $WorkingDirectory
}

If you add this function into a fresh pwsh session, and then run the following 2 invocations, everything works correctly:

> fn -Resolve
> fn

However, if you run the commands in the opposite order (again in a fresh pwsh session), a type error is thrown:

> fn
> fn -Resolve
OperationStopped:
Line |
   6 |      $Shortcut.WorkingDirectory = $WorkingDirectory
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to cast object of type 'System.Management.Automation.PathInfo' to type 'System.String'.

My hypothesis: PowerShell somehow remembers the first type that is assigned into a property of a COM object, and then refuses any other type (even if the second type can be normally implicitly casted to the first type). However, this only works in one direction (when the first type is a String, which matches the expected type of the property), but not in the other (when the first type is something that can be implicitly casted to the correct type (String in this case)). No idea why, quite curious. :)

Expected behavior

Both invocations should succeed, independent of the order.

Actual behavior

A type error is thrown if the first assignment assigns a String, and the second assignment assigns a different type, even if has an implicit cast to String.

Error details
Exception             :
    Type       : System.InvalidCastException
    TargetSite :
        Name          : ChkCast_Helper
        DeclaringType : System.Runtime.CompilerServices.CastHelpers, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Message    : Unable to cast object of type 'System.Management.Automation.PathInfo' to type 'System.String'.
    Data       : System.Collections.ListDictionaryInternal
    Source     : System.Private.CoreLib
    HResult    : -2147467262
    StackTrace :
   at CallSite.Target(Closure , CallSite , ComObject , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo          : OperationStopped: (:) [], InvalidCastException
FullyQualifiedErrorId : System.InvalidCastException
InvocationInfo        :
    ScriptLineNumber : 6
    OffsetInLine     : 5
    HistoryId        : -1
    Line             :     $Shortcut.WorkingDirectory = $WorkingDirectory

    PositionMessage  : At line:6 char:5
                       +     $Shortcut.WorkingDirectory = $WorkingDirectory
                       +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at fn, <No file>: line 6
                        at <ScriptBlock>, <No file>: line 1
Environment data
Name                           Value
----                           -----
PSVersion                      7.3.0-preview.1
PSEdition                      Core
GitCommitId                    7.3.0-preview.1
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
Visuals

image

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce la asignación del acceso directo COM en una sesión nueva de pwsh usando la función y ambos órdenes de invocación mostrados en el issue. Rastrea la ruta de asignación de propiedades COM de PowerShell responsable del cast y, a continuación, verifica que asignar un string y un PathInfo funciona en cualquiera de los dos órdenes sin la InvalidCastException indicada.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
csharp, powershell
Área
cli
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.