Forum Discussion
rakshi
Apr 26, 2021Copper Contributor
The term <function-name> is not recognized as the name of a cmdlet, function, ...
This is my code. I'm trying to unzip files, edit them, and zip them back. For some reason functions DeGZip-File and GZip-File are not being recognized. Could anyone point out what could be wrong here?
Function DeGZip-File {
[CmdletBinding()]
param($infile)
$infile
$outfile = ($infile -replace '\.gz$','')
try
{
$input = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
$output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None)
$gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress)
$buffer = New-Object byte[](1024)
while($true){
$read = $gzipstream.Read($buffer, 0, 1024)
if ($read -le 0){break}
$output.Write($buffer, 0, $read)
}
return $outfile
}
catch
{
Write-Host "$_.Exception.Message" -ForegroundColor Red
}
finally
{
$gzipStream.Close()
$output.Close()
$input.Close()
}
}
#Zip them back after adding the headers
Function Gzip-File {
[CmdletBinding()]
param($outfile)
$srcFile = Get-Item -Path $outfile
$newFileName = "$($srcFile.FullName).gz"
try
{
$srcFileStream = New-Object System.IO.FileStream($srcFile.FullName,([IO.FileMode]::Open),([IO.FileAccess]::Read),([IO.FileShare]::Read))
$dstFileStream = New-Object System.IO.FileStream($newFileName,([IO.FileMode]::Create),([IO.FileAccess]::Write),([IO.FileShare]::None))
$gzip = New-Object System.IO.Compression.GZipStream($dstFileStream,[System.IO.Compression.CompressionMode]::Compress)
$srcFileStream.CopyTo($gzip)
}
catch
{
Write-Host "$_.Exception.Message" -ForegroundColor Red
}
finally
{
$gzip.Dispose()
$srcFileStream.Dispose()
$dstFileStream.Dispose()
}
}
$headerProcess = {
#param($file,$headerArray)
$file = $folder + "\" + $file
$unZippedFile = DeGZip-File($file)
$unZippedFile[1]
$unZippedFile = $unZippedFile[1]
$filedata = import-csv $unZippedFile -Header $headerArray
$filedata | export-csv $unZippedFile -NoTypeInformation
GZip-File $unZippedFile
}
function Add-Headers([string]$folder, [string]$schemaFilePath){
if($schemaFilePath -eq 'TallOptionsSchema.txt')
{
$tallOptionsSchema = Get-Content $schemaFilePath
$strArray = [string[]]$tallOptionsSchema
$headerArray = $strArray -join "`t"
}
else
{
$JSONFromFile = Get-Content -Raw -Path $schemaFilePath | ConvertFrom-Json
$header = $JSONFromFile.schema.name
$strArray = [string[]]$header
$headerArray = $strArray -join "`t"
}
$filesToModify = Get-ChildItem $folder -Recurse -File
foreach ($file in $filesToModify)
{
Start-Job -Name $file.Name -ScriptBlock $headerProcess -ArgumentList ($file,$headerArray)
}
}
$folderPath = 'D:\Working\options'
$schemafile = 'D:\Working\options\schema.json'
Add-Headers $folderPath $schemafile
Get-Job
# Wait for it all to complete
While (Get-Job -State "Running")
{
Start-Sleep 10
}
# Getting the information back from the jobs
Get-Job | Receive-Job
Get-Job | remove-job -Force
- A few of things that jump out upon a quick glance of the code
1 - use function names with approved verbs e.g. DeGZip-File = Invoke-DeGZip (https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands?view=powershell-7.1)
2 - the $input variable is automatic built in variable which could lead to unexpected results
3 - on line 65 you are calling the function inside a variable with the filename in brackets - typically would pass the file to the function calling naming the parameter e.g. Invoke-DeGZip -infile $file
4 - the infile parameter doesn't have a type would expect this to be a string value with being passed a filepath
Not a full review of the code - I would start with editing the code in Visual Studio Code as it will pull out some of these and other warnings and errors, also look at coding good habits. like declaration of variables in a section, all functions in another section then an execution section which only contains the "running code" that calls the variables there are some good templates for PowerShell scripts that you can use to assist 🙂
- SteveMacNZIron ContributorA few of things that jump out upon a quick glance of the code
1 - use function names with approved verbs e.g. DeGZip-File = Invoke-DeGZip (https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/approved-verbs-for-windows-powershell-commands?view=powershell-7.1)
2 - the $input variable is automatic built in variable which could lead to unexpected results
3 - on line 65 you are calling the function inside a variable with the filename in brackets - typically would pass the file to the function calling naming the parameter e.g. Invoke-DeGZip -infile $file
4 - the infile parameter doesn't have a type would expect this to be a string value with being passed a filepath
Not a full review of the code - I would start with editing the code in Visual Studio Code as it will pull out some of these and other warnings and errors, also look at coding good habits. like declaration of variables in a section, all functions in another section then an execution section which only contains the "running code" that calls the variables there are some good templates for PowerShell scripts that you can use to assist 🙂