JSON to CSV Conversion for a huge file - powershell

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.

Related

Powershell pipeline foreach-object variable is null

I have a super simple script that I swear I use almost every day, but for some unknown reason my $_. variable is null.
Could someone please spot check it? there is only one column in the CSV I am importing, however it has no header so i don't know if that is what is causing it.
$results = import-csv C:\####\####\####\finddestinguishednamesof.csv | foreach-object {
Get-ADGroup $_. -Properties SamAccountName,DistinguishedName
}
$results | select SamAccountName,DistinguishedName |
Export-Csv C:\Users\laruemi\Desktop\test.csv -NoTypeInformation
I keep getting this error and do not know why.
Get-ADGroup : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.
At C:\Users\laruemi\Desktop\getdestinguishedname.ps1:2 char:13
+ Get-ADGroup $_. -Properties SamAccountName,DistinguishedName
+ ~~
+ CategoryInfo : InvalidData: (:) [Get-ADGroup], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADGroup
The list of CSV's I am importing is just a list of SAMAccountNames with no header. i dont think that should cause this error, but it might. Can someone please give me a sanity check?

I can't get Powershell StartsWith function to work

I'm trying to create a Powershell script that prints out only certain AD groups from the Folder Permission settings. However for some reason Powershell doesn't recognize StartsWith function.
("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).StartsWith("sl_test")) { continue }; $_ }
When I run this I got errors similar to this for every foreach object:
Method invocation failed because [System.Security.Principal.NTAccount] does not contain a method named 'StartsWith'.
At C:\temp\test.ps1:1 char:56
+ ("C:\folder" | get-acl).Access | ForEach-Object { if (($_.IdentityReference).St ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Any suggestions on how to get this to work?
IdentityReference is a [System.Security.Principal.NTAccount]according to your error message.
But .StartWith is a method on the String type. If you call a method, Powershell does no magic for you, AFAIK.
Try ... ($_.IdentityReference) -match "^sl_test" ..., which should do the implicit string conversion.
If you want the string representation of an IdentityReference (regardless of whether it's and NTAccount object or a SID), you can reference the Value property:
$_.IdentityReference.Value.StartsWith('sl_test')
Try:
Get-Acl -Path "C:\folder" | Select-Object -ExpandProperty Access | Where-Object {$_.IdentityReference -like "sl_test*" }
You can customize the output with an additional | Select-Object -Property XY

Append/prepend data to CSV column

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

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

PowerShell remote event log parsing

I wrote a small script to grab event log entries from a remote machine and write it to a .csv file. The script works when targeting a single machine, but when I try to implement a for loop and loop it over all machines in Active Directory, I get this error:
Method invocation failed because [Microsoft.ActiveDirectory.Management.ADComputer]
does not contain a method named 'op_Addition'.
At Y:\srp.ps1:7 char:143
+ ... | Export-Csv $($computer + ".csv")
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Export-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty.
Provide an argument that is not null or empty, and then try the command again.
At Y:\srp.ps1:7 char:141
+ ... 0 | Export-Csv $($computer + ".csv")
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv],
ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,
Microsoft.PowerShell.Commands.ExportCsvCommand
The error indicates there's a problem with the Export-Csv command, but running the command by itself creates the log files needed. Here is the full script, for reference:
# Gets SRP event log entries from remote machine and writes them to a .csv file
# of the same name.
Write-Output "Running..."
$computers = Get-ADComputer -filter {(Name -like "PC*") -or (Name -like "LT*")}
foreach ($computer in $computers) {
Get-EventLog -LogName Application -Source Microsoft-Windows-SoftwareRestrictionPolicies
-ComputerName $computer -Newest 10 | Export-Csv $($computer + ".csv")
} #end foreach
Write-Host "Done."
Any ideas as to why this error appears when I try to loop over computers in AD?
It looks like Get-ADComputer returns ADComputer objects, but you're passing it to Get-EventLog's ComputerName parameter, which takes a string, as-is. I'm assuming you'll need to grab the name property from the Microsoft.ActiveDirectory.Management.ADComputer object.