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.
Related
I want to compare 2 csv files test1 and test2. I want to export a test3 file only if the Department or the country has changed with the changed details. I have the following code but this compares the 2 whole files not just the Country and Department:
Compare-Object -ReferenceObject (Get-Content -Path C:\scripts\test1.csv) -DifferenceObject (Get-Content -Path C:\scripts\test2.csv) -PassThru | Where-Object{ $_.SideIndicator -eq "=>" } | % { $_ -Replace ',', ";"} | Out-File -FilePath C:\scripts\test3.csv
This is test1:
This is test2:
So test3 should be the same as test2. Anyone knows how you can compare the 2 files with just the changes of the country or department
Without using Compare-Object, you can do this like below:
$csv1 = Import-Csv -Path 'C:\scripts\test1.csv'
$csv2 = Import-Csv -Path 'C:\scripts\test2.csv'
$csv3 = foreach ($item in $csv2) {
$compare = $csv1 | Where-Object { $_.email -eq $item.email }
if ($compare.Country -ne $item.Country -or $compare.Department -ne $item.Department) {
# output the object from $csv2 to be collected in the new $csv3
$item
}
}
$csv3 | Export-Csv -Path 'C:\scripts\test3.csv' -Delimiter ';' -NoTypeInformation
If you do want to use Compare-Object, this also works:
$csv1 = Import-Csv -Path 'C:\scripts\test1.csv'
$csv2 = Import-Csv -Path 'C:\scripts\test2.csv'
Compare-Object -ReferenceObject $csv1 -DifferenceObject $csv2 -Property Country,Department -PassThru |
Where-Object{ $_.SideIndicator -eq "=>" } | Select-Object * -ExcludeProperty SideIndicator |
Export-Csv -Path 'C:\scripts\test3.csv' -Delimiter ';' -NoTypeInformation
I'm a bit new to PowerShell. I have a working script returning -Line, -Character and -Word to a csv file. I can't figure out how to add the full name of the file into the csv.
get-childitem -recurse -Path C:\Temp\*.* | foreach-object { $name = $_.FullName; get-content $name | Measure-Object -Line -Character -Word} | Export-Csv -Path C:\Temp\FileAttributes.csv
I've tried using Write-Host and Select-Object, but I'm not sure about the syntax.
I've been using the following as a reference.
Results
This is what I'm after
Use Select-Object with a calculated property:
Get-Childitem -recurse -Path C:\Temp\*.* | ForEach-Object {
$fullName = $_.FullName
Get-Content $fullName | Measure-Object -Line -Character -Word |
Select-Object #{ Name = 'FullName'; Expression={ $fullName } }, *
} | Export-Csv -Path C:\Temp\FileAttributes.csv
Note:
Pass -ExcludeProperty Property to Select-Object to omit the empty Property column.
Pass -NoTypeInformation to Export-Csv to suppress the virtually useless first line (the type annotation) in the CSV.
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 need help removing the #{} extensions from object output.
The code bellow is listing the last modified file inside a folder. But the output is inside extension #{}.
I have tried the Out-String but it is not working.
function scriptA() {
Get-ChildItem $path | Where-Object {!$_.PsIsContainer} | Select fullname -last 1
}
function scriptB() {
Get-ChildItem $path2 | Where-Object {!$_.PsIsContainer} | Select fullname -last 1
}
$data1=ScritA
$data2=ScriptB
$result=#()
$list=#{
FirstFile=$data1
SecondFile=$data2
}
$result+= New-Object psobject -Property $list
$result | Export-Csv -append -Path $csv
This will output:
FirstFile #{data1} and SecondFile #{data2}
Change your functions slightly to this -
function scriptA() {
Get-ChildItem $path | Where-Object {!$_.PsIsContainer} | Select-Object -ExpandProperty fullname -last 1
}
function scriptB() {
Get-ChildItem $path2 | Where-Object {!$_.PsIsContainer} | Select-Object -ExpandProperty fullname -last 1
}
Doing that will let you select only the FullName property.
OR
If you do not want to change the functions, change the $list assignment to -
$list=#{
FirstFile = $data1.FullName
SecondFile = $data2.FullName
}
New-Object PSObject -Property #{FirstFile = ($a = (Get-ChildItem $path1),(Get-ChildItem $path2) |
Where-Object {!$_.PSIsContainer} | ForEach-Object {$_[-1].FullName})[0];SecondFile = $a[1]} |
Export-Csv $csv -NoTypeInformation
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