Leo Torres
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
}
ASKER
Looks like the previous variable was in use some how causing issues. I renamed variable but still nothing
Here is my error message
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
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
Sort-DataFile $SortFile
Should be
Sort-DataFile <somefilename.ext>
Should be
Sort-DataFile <somefilename.ext>
ASKER
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.
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.
ASKER
Driving away from computer will post later. Thank you!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
BINGO that worked thank you!
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