Skip to content

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

Closed

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. added
    Needs-Triage
    on Jan 5, 2025
  2. 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.

  3. 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);

  4. 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
  5. added
    Issue-Bug
    Up-for-Grabs
    and removed
    Needs-Triage
    on Jan 8, 2025
  6. added
    In-PR
    on Feb 6, 2025
  7. 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-PRIssue-BugUp-for-Grabs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions