I have this PowerShell query listed below and I require the headers to be machine, path, filewalkmethod
$fs = Get-FileServer
foreach ($s in $fs) {
foreach ($share in $s.Volumes) {
($s.Servername, $share.Share, $share.FileWalkMethod) -join "," |
Out-File -Append D:\data\splunk\otl_varonis\otl_varonis_monitoring.csv
}
}
Sample output:
nas01e,E$,Windows
Updated Query I'm using:
Import-Module -Name VaronisManagement
Connect-Idu
$fs = Get-FileServer
foreach($s in $fs){
$s.Volumes | Select-Object #{n='ServerName'; e={$s.ServerName}}, Share, FileWalkMethod |
Export-CSV D:\data\splunk\otl_varonis\otl_varonis_monitoring_test.csv
-NoTypeInformation -NoClobber }
Untested but this should work:
foreach($s in $fs){
$s.Volumes | Select-Object #{n='Machine'; e={$s.ServerName}}, Share, FileWalkMethod | Export-CSV D:\data\splunk\otl_varonis\otl_varonis_monitoring.csv -NoTypeInformation -Append
}
Note that the parameter -Append for Export-Csv was introduced with PowerShell v3. To make this compatible with earlier versions you could pipeline the loop:
$fs | ForEach-Object {
$machine = $_.ServerName
$_.Volumes | Select-Object #{n='Machine';e={$machine}}, Share, FileWalkMethod
} | Export-Csv D:\data\splunk\otl_varonis\otl_varonis_monitoring.csv -NoType
Related
function Get-CHPEL-Report
{
Clear-Host
Write-Output "Finding Newest CHPEL Report"
$PathCHPEL = Get-ChildItem -Path S:\CHPEL | Sort-Object LastWriteTime | Select-Object -last 1 | Select-Object FullName | ForEach-Object{$_.FullName}
Write-Output "Loading - $PathCHPEL"
#$global:CHPEL = Get-Content -Path $PathCHPEL | Select-Object -Skip 1 | ConvertFrom-Csv -Delimiter "`t"
$CHPEL = Get-Content -Path $PathCHPEL | Select-Object -Skip 1 | ConvertFrom-Csv -Delimiter "`t"
return $CHPEL
Clear-Host
}
$CHPEL = Get-CHPEL-Report
If i run this code I don't get anything written out to the screen and $CHPEL is empty.
But the code does work if I make the variable global in the function and just call the function. How can I make this work with Return so I don't have to have a global variable?
function Get-CHPEL-Report
{
Clear-Host
Write-Output "Finding Newest CHPEL Report"
$PathCHPEL = Get-ChildItem -Path S:\CHPEL | Sort-Object LastWriteTime | Select-Object -last 1 | Select-Object FullName | ForEach-Object{$_.FullName}
Write-Output "Loading - $PathCHPEL"
$global:CHPEL = Get-Content -Path $PathCHPEL | Select-Object -Skip 1 | ConvertFrom-Csv -Delimiter "`t"
Clear-Host
}
Get-CHPEL-Report
Try this:
function Get-CHPEL-Report {
Clear-Host
Write-Host "Finding Newest CHPEL Report"
$PathCHPEL = Get-ChildItem -Path D:\Temp\stackoverflw | Sort-Object LastWriteTime | Select-Object -last 1 | Select-Object FullName | ForEach-Object{$_.FullName}
Write-Host "Loading - $PathCHPEL"
$CHPEL = Get-Content -Path $PathCHPEL | Select-Object -Skip 1 | ConvertFrom-Csv -Delimiter "`t"
return $CHPEL
#Clear-Host #Never executed
}
$CHPEL = Get-CHPEL-Report
Write-Host $CHPEL
I have replaced "Write-Output" by "Write-Host" because it was include in the $CHPEL variable. You can also remove the last "Clear-Host", it is after the "return" and will never be executed.
I'm just did a PowerShell script listing all shared folders of all my servers + getting all NTFS rights of them.
Big lines:
Get-ADComputer -LDAPFilter "(&(objectcategory=computer)(OperatingSystem=*server*))" -Properties name |
Select-Object name -ExpandProperty name >> "$path\servers.txt"
foreach ($_ in Get-Content "$path\servers.txt") {
Get-WmiObject Win32_Share -Computer $_ |
Format-Table path -AutoSize |
Out-File "$path\Path\$_.txt"
}
foreach ($SharedFolder in $SharedFolders) {
$lecteur = [int][char]'A'
1..26 | ForEach-Object {
$LR = [char]$lecteur
$contenu = [System.IO.File]::ReadAllText("$lgpath\$SharedFolder.txt").Replace("${LR}:", "\\$SharedFolder\$LR$").Replace("path", "").Replace("----", "").Replace(" ", "").Replace("\ ", "\")
[System.IO.File]::WriteAllText("$lgpath\$SharedFolder.txt", $contenu)
$lecteur++
}
(Get-Content "$lgpath\$SharedFolder.txt" | Where-Object {
$_ -notmatch '^\s*$'
}) | Out-File "$lgpath\$SharedFolder.txt"
}
foreach ($SharedFolder in $SharedFolders) {
$droits = foreach ($ligne in Get-Content "$lgpath\$SharedFolder.txt") {
Get-NTFSAccess -Path "$ligne"
}
$droits |
select Fullname, Account, AccessRights, InheritanceEnabled |
Export-Csv "$Path\NTFS\NTFS_$SharedFolder.csv" -NoTypeInformation -Delimiter ";" -Encoding UTF8
}
This is working fine for now but I'd like to have one more information: the last account that edited NTFS rights on each folder.
I am trying to give an output "unique". What do I have to change in this code ?
If ($append -Eq "y")
{
$objRights | Export-Csv -Path $outRightsFile -Delimiter ";" -Append -NoTypeInformation
}
Else
{
$objRights | Export-Csv -Path $outRightsFile -Delimiter ";" -NoTypeInformation
}
The Select-Object CmdLet has an -Unique parameter.
Ruthlessly stolen example from the docs:
"a","b","c","a","a","a" | Select-Object -Unique
a
b
c
Therefore your code could simply become:
$objRights |
Select-Object -Unique |
Export-Csv -Path $outRightsFile -Delimiter ";" -NoTypeInformation
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}
I have a script like the following:
$in_file = "C:\Data\Need-Info.csv"
$out_file = "C:\Data\Need-Info_Updated.csv"
$list = Import-Csv $in_file
ForEach ( $user in $list ) {
$zID = $user.zID
ForEach-Object {
Get-QADUser -Service 'domain.local' -SearchRoot 'OU=Users,DC=domain,DC=local' -SizeLimit 75000 -LdapFilter "(&(objectCategory=person)(objectClass=user)(PersonzID=$zID))" | Select-Object DisplayName,samAccountName,#{Name="zID";expression={$zID}} | Export-Csv $out_file -NoTypeInformation -Force
}
}
However, I am not able to get it to output all of the results to the $out_file since it does not seem to append the data to the csv file.
Is there a way to make this append the data to a file?
Convert the foreach loop into a foreach-object, and move the export-csv to outside the outer foreach object so that you can pipe all the objects to the export-csv.
Something like this (untested):
$list | ForEach {
$zID = $_.zID
ForEach-Object {
Get-QADUser -Service 'domain.local' -SearchRoot 'OU=Users,DC=domain,DC=local' -SizeLimit 75000 -LdapFilter "(&(objectCategory=person)(objectClass=user)(PersonzID=$zID))" | Select-Object DisplayName,samAccountName,#{Name="zID";expression={$zID}}
}
} | Export-Csv $out_file -NoTypeInformation -Force
As Sune mentioned, PowerShell v3's Export-Csv has an Append flag but no character encoding protection. manojlds is correct, since your code is writing all new data to a new CSV file.
Meanwhile, you can append data to a CSV by:
Convert the objects to CSV with ConvertTo-Csv
Strip the header —and type information if necessary— and collect the
CSV data only
Append the new CSV data to the CSV file through Add-Content or
Out-File, be sure to use same character encoding
Here is a sample:
1..3 | ForEach-Object {
New-Object PSObject -Property #{Number = $_; Cubed = $_ * $_ * $_}
} | Export-Csv -Path .\NumTest.csv -NoTypeInformation -Encoding UTF8
# create new data
$newData = 4..5 | ForEach-Object {
New-Object PSObject -Property #{Number = $_; Cubed = $_ * $_ * $_}
} | ConvertTo-Csv -NoTypeInformation
# strip header (1st element) by assigning it to Null and collect new data
$null, $justData = $newData
# append just the new data
Add-Content -Path .\NumTest.csv -Value $justData -Encoding UTF8
# create more new data, strip header and collect just data
$null, $data = 6..9 | ForEach-Object {
New-Object PSObject -Property #{Number = $_; Cubed = $_ * $_ * $_}
} | ConvertTo-Csv -NoTypeInformation
# append the new data
Add-Content -Path .\NumTest.csv -Value $data -Encoding UTF8
# verify
Import-Csv .\NumTest.csv
# clean up
Remove-Item .\NumTest.csv
-append is broken in PowerShell v2.0. You can use Dmitry Sotikovs workaround instead: http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/
I would however recommend manojlds excellent solution!
After version 3 you can use:
Export-Csv $out_file -append -notypeinformation -encoding "unicode"