Visitar URL original
`Start-Transcript` throws when `$Transcript` is a `PSObject` wrapped `string` · Issue #24740 · PowerShell/PowerShell · GitHub
Skip to content

Start-Transcript throws when $Transcript is a PSObject wrapped string #24740

Description

Prerequisites

Steps to reproduce

First bug report I've filed for PowerShell ever, so go easy on me :-). Here's the PowerShell doc's Github issue I had raised that is now closed with the suggestion of filing a bug report.

I believe I've stumbled upon a bug with Start-Transcript when using one of the Start-Transcript examples when using preference variables in PowerShell 7.4.6 (on Windows and Linux) when using '~' in the path.

Using Start-Transcript after using the example #3 in the docs, I get an error:

Start-Transcript: Unable to cast object of type 'Systme.Management.Automation.PSObject' to type 'System.String'.

However, if I use Start-Transcript $Trasncript, the command works and a transcript is created without issue. I do not get this error using the example with a UNC path or full local path. It seems to only happen when using '~' in both Windows and Linux.

Expected behavior

$sharepath  = '~/PowerShell_Transcripts/'
$username   = $env:USERNAME
$hostname   = hostname
$version    = $PSVersionTable.PSVersion.ToString()
$datetime   = Get-Date -f 'yyyyMMddHHmmss'
$filename   = "Transcript-${username}-${hostname}-${version}-${datetime}.txt"
$Transcript = Join-Path -Path $sharepath -ChildPath $filename
Start-Transcript # I should see "Transcript Started, output file $filename here"

Actual behavior

$sharepath  = '~/PowerShell_Transcripts/'
$username   = $env:USERNAME
$hostname   = hostname
$version    = $PSVersionTable.PSVersion.ToString()
$datetime   = Get-Date -f 'yyyyMMddHHmmss'
$filename   = "Transcript-${username}-${hostname}-${version}-${datetime}.txt"
$Transcript = Join-Path -Path $sharepath -ChildPath $filename
Start-Transcript
Start-Transcript: Unable to cast object of type 'System.Management.Automation.PSObject' to type 'System.String'.

Error details

Exception             : 
    Type       : System.InvalidCastException
    TargetSite : 
        Name          : ChkCast_Helper
        DeclaringType : [System.Runtime.CompilerServices.CastHelpers]
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Message    : Unable to cast object of type 'System.Management.Automation.PSObject' to type 'System.String'.
    Source     : System.Private.CoreLib
    HResult    : -2147467262
    StackTrace : 
   at Microsoft.PowerShell.Commands.StartTranscriptCommand.BeginProcessing()
   at System.Management.Automation.Cmdlet.DoBeginProcessing()
   at System.Management.Automation.CommandProcessorBase.DoBegin()
CategoryInfo          : NotSpecified: (:) [Start-Transcript], InvalidCastException
FullyQualifiedErrorId : System.InvalidCastException,Microsoft.PowerShell.Commands.StartTranscriptCommand
InvocationInfo        : 
    MyCommand        : Start-Transcript
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 13
    Line             : Start-Transcript
    Statement        : Start-Transcript
    PositionMessage  : At line:1 char:1
                       + Start-Transcript
                       + ~~~~~~~~~~~~~~~~
    InvocationName   : Start-Transcript
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.6
PSEdition                      Core
GitCommitId                    7.4.6
OS                             Fedora Linux 41 (Workstation Edition)
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

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

Visuals

No response

Activity

  1. billkindle commented on Jan 5, 2025

    @billkindle
    Author

    Patrick Meinecke (@SeeminglyScience) mentioned in PowerShell discord that a workaround can be performed by adding this line,$Transcript = [string]$Transcript after setting up the preference variable shown in example #3 of the PowerShell help docs. I can confirm this does work under Windows and Linux.

  2. SeeminglyScience commented on Jan 5, 2025

    @SeeminglyScience
    Contributor

    Start-Transcript supports tilde, the issue is that the variable's value is wrapped in a PSObject and the code doesn't handle it.

    This line should be _outFilename = (string)PSObject.Base(value);

  3. changed the title [-]Start-Transcript doesn't support a path that starts with '~\'[/-] [+]`Start-Transcript` throws when `$Transcript` is a `PSObject` wrapped `string`[/+] on Jan 5, 2025
  4. added
    Issue-BugIssue has been identified as a bug in the product
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Jan 8, 2025
  5. microsoft-github-policy-service commented on Feb 7, 2025

    @microsoft-github-policy-service
    Contributor

    📣 Hey Bill Kindle (@billkindle), how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issueIssue-BugIssue has been identified as a bug in the productUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions