Append/prepend data to CSV column - powershell

I have spent over a week researching this question and none of the helps have worked. I started with this:
$File = "C:\FOLDER\index - Copy.csv"
Import-Csv -Delim ',' $File |
ForEach-Object {$_.col1 = "C:\FOLDER\$($_.col1)"; $_} |
Export-Csv Index2.csv -Delim ',' -NoTypeInformation
which I found elsewhere on this site and modified as I would expect. But I received this error:
Exception setting "col1": "The property 'col1' cannot be found on this object.
Verify that the property exists and can be set."
At line:2 char:69
+ Import-Csv -Delim ',' $File | ForEach-Object {$_.col1 = "C:\FOLDER\$($_.col1)";$_ ...
+ ~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
All I want to do is take a CSV file that looks like this:
Path,Filename
123456789,654321
234567891,543216
345678912,432165
and ultimately end with this:
Path,Filename
C:\FOLDER\123456789.jpg,654321.jpg
C:\FOLDER\234567891.jpg,543216.jpg
C:\FOLDER\345678912.jpg,432165.jpg

Related

JSON to CSV Conversion for a huge file

I do not have Powershell experience. Looking for some help to convert a huge JSON file to CSV.
I tried the below code which I found online, however it throws below error.
Code Snippet:
$file = "C:\Users\Desktop\Fk9b3ug5u\records.txt"
$pathToOutputFile = "C:\Users\Desktop\Fk9b3ug5u\Fk9b3ug5u.csv"
((Get-Content -LiteralPath "C:\Users\Desktop\Fk9b3ug5u\records.txt") | ConvertFrom-Json).results |
ConvertTo-Csv -NoTypeInformation |
Set-Content $pathToOutputFile
Error :
ConvertTo-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Users\sneshah\convertJsonToXML.ps1:5 char:5
+ ConvertTo-Csv -NoTypeInformation |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertTo-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToCsvCommand
In the code you posted, results is an object inside JSON. If your records.txt doesn't contain that object, you won't have any data when you try to get it with .results. If the entire file is able to be converted to CSV, you will just need to remove .results from your script.

Is there a reason my Powershell script is now returning null-value expression errors?

As the title suggests, I have a script that I've been running daily to parse tables from a web page and export those to a csv. A few days ago though, I realized the script has been returning the following errors:
You cannot call a method on a null-valued expression. At C:\Users\Luke\DailyStats\NHLStats.ps1:16 char:1
+ $headers = $rows.item(1).children | select -ExpandProperty InnerText
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull Cannot index into a null array. At C:\Users\Luke\DailyStats\NHLStats.ps1:23 char:14
+ $headers = #($headers[0];'TEAM';$headers[1..($headers.Length-1)])
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray Cannot index into a null array. At C:\Users\Luke\DailyStats\NHLStats.ps1:23 char:33
+ $headers = #($headers[0];'TEAM';$headers[1..($headers.Length-1)])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
I don't get why all of a sudden it randomly stopped working. Any and all help is greatly appreciated.
My script:
$url = "https://www.hockey-reference.com/leagues/NHL_2020.html"
#getting the data
$data = Invoke-WebRequest $url
#grab the third table
$table = $data.ParsedHtml.getElementsByTagName("table") | Select -skip 2 | Select -First 1
#get the rows of the Team Statistics table
$rows = $table.rows
#get table headers
$headers = $rows.item(1).children | select -ExpandProperty InnerText
#$headers = $headers | % { if ($_ -eq "GF") { "GFPG" } else { $_ }}
#count the number of rows
$NumOfRows = $rows | Measure-Object
#Manually injecting TEAM header and replace any headers you want to change
$headers = #($headers[0];'TEAM';$headers[1..($headers.Length-1)])
#enumerate the remaining rows (we need to skip the header row) and create a custom object
$allData = #{}
$out = for ($i=2;$i -lt $NumofRows.Count;$i++) {
#define an empty hashtable
$objHash=[ordered]#{}
#getting the child rows
$rowdata = $rows.item($i).children | select -ExpandProperty InnerText
for ($j=0;$j -lt $headers.count;$j++) {
#add each row of data to the hash table using the correlated table header value
$objHash[$headers[$j]] = $rowdata[$j]
}
#turn the hashtable into a custom object
[pscustomobject]$objHash
$allData.Add($i, $objHash)
}
$out | Select TEAM,AvAge,GP,W,L,OL,PTS,PTS%,GF,GA,SOW,SOL,SRS,SOS,TG/G,EVGF,EVGA,PP,PPO,PP%,PPA,PPOA,PK%,SH,SHA,PIM/G,oPIM/G,S,S%,SA,SV%,SO -SkipLast 1 | Export-Csv -Path "C:\Users\Luke\DailyStats\$((Get-Date).ToString("'NHL Stats' yyyy-MM-dd")).csv" -NoTypeInformation
Looks like your $table variable is returning null. When running that |select -skip 2 | select -first 1, it isn't returning any data to the $table variable.
This seems to be related to the -skip 2 selection.
When running just the portion of the $table = $data.ParsedHtml...
It only returns two objects.
So possibly you need to update your selection to properly retrieve the table that you want?
I am only seeing the 'standings EAS' and 'standings WES' tables being pulled.

Attempting to get the sum of all file line counts in a directory

I'm attempting to get the number of lines in all files in my directory. I've tried using some kind of variable I set to 0 and looping through all the files and getting their line number.
> $i=0;ls | foreach{$i += [int](get-content $_ | measure-object -line)};$i
However, every time I try adding it to the variable I set to 0, it shoots out an odd error:
Cannot convert the "Microsoft.PowerShell.Commands.TextMeasureInfo" value of type
"Microsoft.PowerShell.Commands.TextMeasureInfo" to type "System.Int32".
At line:1 char:19
+ $i=0;ls | foreach{$i += [int](get-content $_ | measure-object -line)};$i
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
Why does this operation not work and how could it be resolved?
Is there a quicker way of obtaining the number of lines in all files in a directory?
You need to get the Lines:
$i=0;ls -File | foreach{$i += [int](get-content $_ | measure-object -line).Lines};$i
I've also added the -File switch to ls/gci to only get the content of files.
Version with aliases expanded:
$i=0;Get-ChildItem -File | ForEach-Object {$i += [int](Get-Content $_ | Measure-Object -line).Lines};$i

Errors in listing AD groups for AD PCs

I am writing a script for work and trying to determine why my code is showing errors. I am new to this coding and want to understand what is wrong.
The errors I get are from the tag .... PC listings in my .txt file.
Ex: Get-Content : Cannot find path 'F:\tag 77909' because it does not exist.
My confusion is that when I write-host after the .Replace code it prints correctly
Ex:You cannot call a method on a null-valued expression. + $Notags =$PC.Replace <<<< ("tag ", "PC")
+ CategoryInfo : InvalidOperation: (Replace:String) [], RuntimeEx
ception
+ FullyQualifiedErrorId : InvokeMethodOnNull
Last error I get is that it only prints out the last PC.... ID in my .txt file listing??? I am unsure why given I have a foreach loop
**MY CODE SO FAR:**
Import-Module activedirectory
$compImports = Get-Content "C:\Temp\temp\input.txt"
$groupExport = "C:\temp\temp\output.txt"
Clear-Content $groupExport
$Header = "PC Name" + "|" + "Group Name" + "|" + "Group Description"
#Write header
$Header | Out-File $groupExport -Append
#get PC tag listing
$PCs = Get-Content $compImports
#For loop to change all "tag " to "PC"
foreach($PC in $PCS)
{
$Notags =$PC.Replace("tag ", "PC")
}
#loop to get information and print it out
foreach ($Notag in $Notags) {
$computerobj = Get-ADComputer $Notag -Properties memberof
$computerobj.memberof | ? {$_ -match '^CN=APP.*'} `
| % {get-adgroup $_ -Properties name, description} | `
% {$computerobj.Name + "|" + $_.name + "|" + $_.description `
| Out-File $groupExport -Append}
}
I see at least one issue here
$compImports = Get-Content "C:\Temp\temp\input.txt"
...
$PCs = Get-Content $compImports
You are calling Get-Content twice which would generate the error you are seeing most likely.
Could be simplified as
$PCs = Get-Content "C:\Temp\temp\input.txt"
Your other error should go away as a result since $PCs should contain real data at that point.

Windows Powershell Export-CSV column/row value as filename

I'm having a problem with Powershell. I need to import a csv file, format it and export it.
Now one of the fields is the same value on every line so I want to name the export file the same as a cell value.
I am importing the csv with
Import-Csv c:\tmp\200114.csv
Formatting the output with
Select-Object #{expression={$_.code}; label='ID NUMBER'} etc etc but one of the fields being DESCRIPTION
Then exporting with:
Export-Csv -NoTypeInformation c:\tmp\test1.csv
So basically I want to name the file something like the following where DESCRIPTION is the Field name and the row is 1(as they are all the same):
Export-Csv -NoTypeInformation c:\tmp\#{expression={$_.DESCRIPTION[1]};}.csv
But I just get:
Export-Csv : Cannot validate argument on parameter 'Delimiter'. The argument is null. Supply a non-null argument and tr
y the command again.
Ideally I could like the file name to be:
Todays Date - DESCRIPTION Column ROW 1 Value.csv
Many thanks for any input....
SAMPLE DATA:
ID NUMBER,NAME,ADDRESS 1,ADDRESS 2,CITY,STATE,ZIP,Spare,PHONE #,DESCRIPTION,Spare,Spare,VALUE
1,Name 1,address 1,address 2,city 1,state 1,zip 1,,Phone 1,CC098-1,,,NCV
2,Name 2,address 2,address 3,city 2,state 2,zip 2,,Phone 2,CC098-1,,,NCV
Thanks, I am very grateful for this. I am struggling to get this working though. It must be to do with:
Select #{n='ID NUMBER';e={$_.code}}, #{n='DESCRIPTION';e={...}}
I Don't know the Select command and can't find anything on it, I can't even see how the n= & e= etc are doing. I trimmed it down to:
$csv = Import-Csv c:\tmp\200114.csv | Select-Object #{expression={$_.code}; label='ID NUMBER'} | $filename = "$(Get-Date -f 'yyyy-MM-dd') - $($csv[0].DESCRIPTION).csv" | $csv | Export-Csv $filename -NoTypeInformation
but I just get errors:
Expressions are only allowed as the first element of a pipeline.
At line:1 char:108
+ $csv = Import-Csv c:\tmp\200114.csv | Select-Object #{expression={$_.code}; label='ID NUMBER'} | $filename <<<< = "$
(Get-Date -f 'yyyy-MM-dd') - $($csv[0].DESCRIPTION).csv" | $csv | Export-Csv $filename -NoTypeInformation
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
Thanks again.
Capture the processed CSV in a variable:
$csv = Import-Csv c:\tmp\200114.csv | Select #{n='ID NUMBER';e={$_.code}},
#{n='DESCRIPTION';e={...}},
...
Construct your filename:
$filename = "$(Get-Date -f 'yyyy-MM-dd') - $($csv[0].DESCRIPTION).csv"
Then export the data:
$csv | Export-Csv $filename -NoTypeInformation