Link to home
Start Free TrialLog in
Avatar of Leo Torres
Leo TorresFlag for United States of America

asked on

Powershell File Sort

I have been trying to get this code to work for some time now. I am trying to create a function that takes a path as a parameter (Some text file). The code should consume the data in the file and sort entire file then overwrite existing file with a new sorted file at the original location.



Function Sort-DataFile{
[cmdletbinding(  
    DefaultParameterSetName = '',  
    ConfirmImpact = 'low'  
)]  
    Param(  
        [Parameter(  
            Mandatory = $True,  
            Position = 0,  
            ParameterSetName = '',  
            ValueFromPipeline = $True)]  
            [array]$File                                    
        )  

Get-Content $File | Sort-Object | Set-Content $File
Write-host "File Sorted"
$File | Out-file -width 300 -filepath $File


Sort-DataFile $File #$Path
}

Open in new window

Avatar of becraig
becraig
Flag of United States of America image

Function Sort-DataFile{
[cmdletbinding(  
    DefaultParameterSetName = '',  
    ConfirmImpact = 'low'  
)]  
    Param(  
        [Parameter(  
            Mandatory = $True,  
            Position = 0,  
            ParameterSetName = '',  
            ValueFromPipeline = $True)]  
            [array]$File                                    
        )  

Get-Content $File | Sort-Object | Set-Content $File
Write-host "File Sorted"



}

Sort-DataFile $File #$Path

Open in new window


Place the function you call outside the function declaration.
Also once you set-content the file is overwritten - no need for another out-file
Avatar of Leo Torres

ASKER

Looks like the previous variable was in use some how causing issues. I renamed variable but still nothing

Function Sort-DataFile{
[cmdletbinding(  
    DefaultParameterSetName = '',  
    ConfirmImpact = 'low'  
)]  
    Param(  
        [Parameter(  
            Mandatory = $True,  
            Position = 0,  
            ParameterSetName = '',  
            ValueFromPipeline = $True)]  
            [array]$SortFile                                    
        )  
Get-Content $SortFile | Sort-Object | Set-Content $SortFile
Write-host "File Sorted"
}

Sort-DataFile $SortFile

Open in new window


Here is my error message
Sort-DataFile : Cannot bind argument to parameter 'SortFile' because it is null.
At \\ServerName\N\Powershell\Functions\Sort-DataFile.ps1:23 char:15
+ Sort-DataFile $SortFile #$Path
+               ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Sort-DataFile], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Sort-DataFile

Open in new window

Sort-DataFile $SortFile

Should be

Sort-DataFile <somefilename.ext>
Iam pass a value by call function from command line
Can you show me exactly what you are running when you call this from the command line ?

I need to figure out what you are passing in since this is complaining the input value is null.
Cannot bind argument to parameter 'SortFile' because it is null.
Driving away from computer will post later. Thank you!
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BINGO that worked thank you!