GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Cannot determine line endings as the text probably contain mixed line endings. · Issue #2074 · PowerShell/PSScriptAnalyzer

Cannot determine line endings as the text probably contain mixed line endings. #2074

New issue
Copy link
New issue
Copy link
Open
#2301
Open
Cannot determine line endings as the text probably contain mixed line endings.#2074
#2301
Copy link
Labels
Issue - Bug

Description

@andrecrms
André Rodrigues (andrecrms)
opened on Mar 5, 2025
Issue body actions

Before submitting a bug report:

  • Make sure you are able to repro it on the latest released version
  • Perform a quick search for existing issues to check if this bug has already been reported

Steps to reproduce

$scriptContent = Get-Content -Path "C:\temp\myscript_posh_v2.ps1" -Raw
Invoke-Formatter -ScriptDefinition $scriptContent | Set-Content -Path "C:\temp\myscript_posh_v2.ps1"
$error[0] | select *

Expected behavior


Actual behavior

PSMessageDetails :
Exception : System.ArgumentException: Cannot determine line endings as the text probably contain mixed line endings.
Parameter name: text
at Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText.GetNumNewLineCharacters(String text, String[]& lines)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText.GetNewLineCharacters(String text, String[]& lines)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText..ctor(String text)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.Formatter.Format[TCmdlet](String scriptDefinition, Settings settings, Range range, TCmdlet cmdlet)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand.ProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject :
CategoryInfo : NotSpecified: (:) [Invoke-Formatter], ArgumentException
FullyQualifiedErrorId : System.ArgumentException,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at , : line 2
PipelineIterationInfo : {}

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Environment data

> $PSVersionTable

Name                           Value                                                                                                                                                                        
----                           -----                                                                                                                                                                        
PSVersion                      5.1.26100.1882                                                                                                                                                               
PSEdition                      Desktop                                                                                                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                      
BuildVersion                   10.0.26100.1882                                                                                                                                                              
CLRVersion                     4.0.30319.42000                                                                                                                                                              
WSManStackVersion              3.0                                                                                                                                                                          
PSRemotingProtocolVersion      2.3                                                                                                                                                                          
SerializationVersion           1.1.0.1       

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.23.0
Reacted by Accurio

Activity

  1. andrecrms commented on Mar 5, 2025

    @andrecrms
    André Rodrigues (andrecrms)
    on Mar 5, 2025
    Author
    More actions

    Full script:

    $VMs = @("server1", "server2")
    $Connection = @()
    $Jobs = @()
    $LogFile = "$(Get-Location)\execution_log.txt"

    Function to log messages to a file

    function Log-Message {
    param ([string]$Message)
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    "$timestamp - $Message" | Out-File -FilePath $LogFile -Append
    }

    Log script start

    Log-Message "Script execution started."
    Write-Host "Execution started. Logs will be saved to: $LogFile"

    Establish the sessions and execute the SQL command on each server in parallel

    foreach ($VM in $VMs) {
    Try {
    # Attempt to create a session to the remote machine
    $Session = New-PSSession -ComputerName $VM -ErrorAction Stop
    Write-Host "Successfully connected to $VM"
    Log-Message "Successfully connected to $VM"

        # Start a background job to execute SQL commands
        $Job = Invoke-Command -Session $Session -ScriptBlock {
            Try {
                Import-Module sqlps -ErrorAction SilentlyContinue
    
                # Get the SQL Server instance name
                $serverName = $env:COMPUTERNAME.ToLower()
                $instanceNames = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
                $queryResult = ""
    
                foreach ($instanceName in $instanceNames) {
                    $sqlInstance = if ($instanceName -eq "MSSQLSERVER") { 
                        $serverName 
                    }
                    else { 
                        "$serverName\$instanceName" 
                    }
    
                    # Define the SQL query
                    $query = "
                    
                    USE [master]
    

    GO

                    "
    
                    Try {
                        # Attempt to execute the query
                        $result = Invoke-Sqlcmd -ServerInstance $sqlInstance -Query $query -QueryTimeout 65535 -ErrorAction Stop
                        $queryResult = "SQL executed successfully on $sqlInstance."
                        $status = "Success!"
                    }
                    Catch {
                        # Capture any errors from the SQL query execution
                        $lineNumber = $_.InvocationInfo.ScriptLineNumber
                        $queryResult = "Error executing SQL at line $($lineNumber): $($_.Exception.Message)"
                        $status = "Fail"
                    }
                }
    
                # Return the results
                [PSCustomObject]@{
                    ComputerName = $serverName
                    SQLResult    = $queryResult
                    Status       = $status
                }
            }
            Catch {
                # Capture any critical errors within the script block
                $lineNumber = $_.InvocationInfo.ScriptLineNumber
                [PSCustomObject]@{
                    ComputerName = $env:COMPUTERNAME
                    SQLResult    = "N/A"
                    Status       = "Fail - Critical error at line $($lineNumber): $($_.Exception.Message)"
                }
                Log-Message "Critical error on $env:COMPUTERNAME at line $($lineNumber): $($_.Exception.Message)"
            }
        } -AsJob
    
        # Add the job to the jobs list
        $Jobs += $Job
    }
    Catch {
        # Capture errors when trying to connect
        $lineNumber = $_.InvocationInfo.ScriptLineNumber
        $Connection += [PSCustomObject]@{
            ComputerName = $VM
            Status       = "Fail"
            ErrorMessage = "Connection error at line $($lineNumber): $($_.Exception.Message)"
        }
        Write-Host "Failed to connect to $VM. Error: $($_.Exception.Message)"
        Log-Message "Failed to connect to $VM at line $lineNumber. Error: $($_.Exception.Message)"
    }
    

    }

    Wait for all jobs to finish

    $Jobs | ForEach-Object {
    # Wait for the job to complete
    Wait-Job -Job $_

    # Fetch the result of the job
    $JobResult = Receive-Job -Job $_
    
    # Output the result for each server
    Write-Host "Result for $($JobResult.ComputerName):"
    Write-Host "SQL Execution Result: $($JobResult.SQLResult)"
    Write-Host "Status: $($JobResult.Status)"
    Write-Host "----------------------------------------------------"
    Log-Message "Result for $($JobResult.ComputerName): SQL Execution Result: $($JobResult.SQLResult) - Status: $($JobResult.Status)"
    
    # Check the job status before removing it
    if ($_.State -eq 'Completed') {
        # Clean up the job
        Remove-Job -Job $_
    }
    

    }

    If there were any connection errors, output them and log

    if ($Connection.Count -gt 0) {
    Write-Host "Connection Issues:"
    Log-Message "Connection Issues:"
    $Connection | ForEach-Object {
    Write-Host "Computer: $($.ComputerName), Error: $($.ErrorMessage)"
    Log-Message "Computer: $($.ComputerName), Error: $($.ErrorMessage)"
    }
    }

    Log script completion

    Log-Message "Script execution completed."
    Write-Host "Execution completed. Log file saved at: $LogFile"

  2. Christoph Bergmeister (bergmeister)
    added
    Issue - Bug
    on Mar 12, 2025
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

    Issue - Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.