Output to txt and cvs as one line - powershell

Good day,
I am looking to output result of a Get-NetTcpConnection into a txt and cvs files simultaneously.
This is how I do it now:
$netstat = Get-NetTCPConnection | Select-Object -Property CreationTime,LocalAddress,LocalPort,RemoteAddress,RemotePort,State
$netstat | Export-Csv -Path C:\temp\$env:COMPUTERNAME-$(Get-Date -Format yyyyMMdd-HH.mm).csv -NoTypeInformation
$netstat | FT -AutoSize | Out-File -FilePath C:\temp\$env:COMPUTERNAME-$(Get-Date -Format yyyyMMdd-HH.mm).txt
Is there a way to do that as a one liner instead of using a variable?
Something like this:
Get-NetTCPConnection | Select-Object -Property CreationTime,LocalAddress,LocalPort,RemoteAddress,RemotePort,State |
Export-Csv -Path C:\temp\$env:COMPUTERNAME-$(Get-Date -Format yyyyMMdd-HH.mm).csv -NoTypeInformation;
$_ | FT -AutoSize | Out-File -FilePath C:\temp\$env:COMPUTERNAME-$(Get-Date -Format yyyyMMdd-HH.mm).txt
Thank you!

"Tee-object" is build to pass the data/output straight through the pipeline from one end to other end with out format.
Get-NetTCPConnection | Select-Object -Property CreationTime,LocalAddress,LocalPort,RemoteAddress,RemotePort,State |Tee-Object -FilePath "C:\Users\Narayana\Desktop\Testing Dir\temp2.txt" | Export-Excel -Path "C:\Users\Narayana\Desktop\Testing Dir\temp2.xlsx"

I've tried many attempts to get this done in a one liner like you have requested but, have been fruitless.
However, you can always just make your own function to perform this work and run it as a one liner?
function Get-NetStat{
$netstat = Get-NetTCPConnection | Select-Object -Property CreationTime,LocalAddress,LocalPort,RemoteAddress,RemotePort,State
if ($netstat)
{
$netstat | Export-Csv -Path C:\temp\$env:COMPUTERNAME-$(Get-Date -Format yyyyMMdd-HH.mm).csv -NoTypeInformation
$netstat | FT -AutoSize | Out-File -FilePath C:\temp\$env:COMPUTERNAME-$(Get-Date -Format yyyyMMdd-HH.mm).txt
}
else
{
Write-Host $Error
}
}

Related

Tee-Object to two pipelines?

I have the following code.
$summary = . {
while ($true) {
# Generating huge list of psobject
}
} |
Tee-Object -FilePath 'fname.csv' | # Need to process the input objects before writing to CSV
Group-Object -Property xxx | Select Name,Count
However, I need to process the input objects before writing to fname.csv. Is it possible to Tee the object to two pipelines?
I tried
$summary = . {
while ($true) {
# Generating huge list of psobject
}
} |
For-Each {
$_ | ConvertTo-Csv -NoTypeInformation | Out-File -Append 'file.csv'
$_
} |
Group-Object -Property xxx | Select Name,Count
But the headers are repeated every line in file.csv.
I'm not sure what you want to do. Does this help? The objects from get-process get passed to two different pipelines.
get-process cmd | foreach-object { $_ | measure-object
$_ | export-csv -append whatever.csv }
This should do the work in the question. It doesn't Tee to two pipelines, which may be needed for some use cases, though.
$summary = . {
while ($true) {
# Generating huge list of psobject
}
} |
ForEach {
$_ | Export-Csv -NoTypeInformation -Append 'file.csv'
$_
} |
Group-Object -Property xxx | Select Name,Count
Going after #mklement0 's guidance, does the below make it any simpler,
Get-Process excel | Tee-Object -Variable process | group processname | select name, count
$process | Export-Csv "D:\Op_GetProcessExcel.csv" -NoTypeInformation
Previous suggestion
As #AnsgarWiechers pointed out something like the following should work for you,
Get-Process |
group processname |
select name, count |
ConvertTo-Csv -NoTypeInformation |
Tee-Object "C:\Op_GetProcess.csv"

Export-Csv help PowerShell [duplicate]

So I'm trying to export a list of resources without the headers. Basically I need to omit line 1, "Name".
Here is my current code:
Get-Mailbox -RecipientTypeDetails RoomMailbox,EquipmentMailbox | Select-Object Name | Export-Csv -Path "$(get-date -f MM-dd-yyyy)_Resources.csv" -NoTypeInformation
I've looked at several examples and things to try, but haven't quite gotten anything to work that still only lists the resource names.
Any suggestions? Thanks in advance!
It sounds like you basically want just text a file list of the names:
Get-Mailbox -RecipientTypeDetails RoomMailbox,EquipmentMailbox |
Select-Object -ExpandProperty Name |
Set-Content -Path "$(get-date -f MM-dd-yyyy)_Resources.txt"
Edit: if you really want an export-csv without a header row:
(Get-Mailbox -RecipientTypeDetails RoomMailbox,EquipmentMailbox |
Select-Object Name |
ConvertTo-Csv -NoTypeInformation) |
Select-Object -Skip 1 |
Set-Content -Path "$(get-date -f MM-dd-yyyy)_Resources.csv"
Powershell 7 is out now. Still no way to export-csv without headers. I get it. Technically it wouldn't be a CSV without a header row.
But I need to remove the header row, so
$obj | convertto-csv | select-object -skip 1 |out-file 'output.csv'
P.S. I didn't need the quotes and I wanted to filter out rows based on a certain property value:
$obj | where-object {$_.<whatever property> -eq 'X' } | convertto-csv -usequotes never | select-object -skip 1 |out-file 'output.csv'

Iterate server list with dbatools?

I am really new to PowerShell and still learning so I am having a requirement to run some of the commands from dbatools and save the results.
$servers = 'E:\DBA\servers.txt'
$outfile = 'E:\DBA\out.csv'
Get-Content $servers | ForEach-Object {Invoke-Command DbaBackupHistory -SQLServer $_ | ConvertTo-CSV -NoTypeInformation | Select-Object -Skip 1 | Out-File -Append $outFile}
I am unsure if this is the correct way to doit
https://dbatools.io/functions/get-dbabackuphistory/
I modified you script and tested. Worked for me. I added 2 more switches to limit result set. -database and -lastfull. You can check documentation for details.
$outfile = 'c:\out.csv'
Get-Content c:\servers.txt|foreach-object {get-DbaBackupHistory -SqlServer $_
-database dbadatabase -lastfull | ConvertTo-CSV -NoTypeInformation |
Select-Object -Skip 1 | Out-File -Append $outFile}

Why won't my script run?

I've built the following PowerShell script and it doesn't seem to work:
"\\example\examplepath\" | % { $_ | select name, #{n="lines"; e={ get-content
$_.FullName | measure-object -line | Select -expand lines } } } | ft -
Autosize | Out-file c:\counts\result.csv
The script is supposed to get a line count for each file and output them to a CSV. Admittedly there around 140,000 files in the folder. Any ideas?
You are missing the Get-ChildItem cmdlet to retrieve all files. The Foreach-Object (%) cmdlet is obsolete here so I removed it. I also removed the Format-Table cmdlet because you are piping the result to Out-File:
Get-ChildItem "\\example\examplepath\" |
Select-Object name, #{n="lines"; e={ get-content $_.FullName | measure-object -line | Select-Object -expand lines } } |
Out-file c:\counts\result.csv

The following is not increment the file count correctly Any Clue as to why?

This powershell code searches the directory and outputs a list of all the files and how old they are to a log file that is parsed buy a different script. all that is working correctly but i also need to keep track of the number of files it found for that dir and the number of files found globally. Thats what the two foreach-Object statements do. but they are staying at 0.
gci -filter *.avi | Select-Object Name, #{Name="Age"; Expression= { (((Get-Date) - $_.CreationTime).Days) }} | Where {$_.Age -ge $daysToKeep} | Out-File -filepath $logFile -append | Foreach-Object {$fileCountCam1++} | Foreach-Object {$fileCount++}
mjolinor's solution is valid, but there's another way (if you can use v3). You can use Tee-Object to write to the file without a loop.
You can also combine your two variable increments into the same script block in the final foreach-object which will speed things up significantly.
gci -filter *.avi |
Select-Object Name, #{Name="Age"; Expression= { (((Get-Date) - $_.CreationTime).Days) }} |
Where {$_.Age -ge $daysToKeep} | Tee-Object -filepath $logFile -append |
Foreach-Object {$fileCountCam1++;$fileCount++}
Out-File is a termnating cmdlet (it doesn't ouput the object to the pipeline), so everything after it isn't getting any input from the pipeline.
See if this works better:
gci -filter *.avi |
Select-Object Name, #{Name="Age"; Expression= { (((Get-Date) - $_.CreationTime).Days) }} |
Where {$_.Age -ge $daysToKeep} |
Foreach-Object {
$_ | Out-File -filepath $logFile -append
$fileCountCam1++
$fileCount++
}