How to give an unique output in powershell - powershell

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

Related

Powershell Function Not Working With Return

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.

How to return the fullname with measure-object attributes

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.

find dot in CSV and replace with found names in Active Directory group

I have a TXT file and converted it to CSV.
Now I want to have a script which:
Look in UserID column and look if there is a name with dot in it (ex: xyz.aaa). The names with dot in them are not UserID but they are Group.
Then it should look for them in AD.
Then replace the found names in the same CSV file.
Find all CNs in TXT file and list them in CSV File:
Select-String -Path $TXTFile -Pattern 'CN=(.*?),' -AllMatches |
Select-Object -Expand Matches |
ForEach-Object { $_.Groups[1].Value } |
select #{L="UserID"; E={$_}} |
Export-CSV $CSVFile1 -Delimiter ";"
what I tried and it does not work, which definately wrong is:
look for . in UserID column which are Groups, then look for them in AD. Replace the members in Groupnames.
Get-ChildItem -Path $CSVFile1 | ForEach-Object {
(Get-Content $_.UserID).Replace('.','Get-ADGroupMember -identity "$_.UserID" -Recursive | Select Name') | Out-File $_.UserID
}
Something like this should do the trick I think
$ProcessedList = Import-Csv -Path $CSVFile1 -Delimiter ';' |
Select-Object -Property *,
#{
Name='ProcessedUserID';
Expression={
if ($_.UserID -match '\.') {
(Get-ADGroupMember -Identity $_.UserID |
Select-Object -ExpandProperty SamAccountName) -join ','
}
else {
$_.UserID
}
}
}
$ProcessedList
$ProcessedList | Export-CSV -Path 'Whatever' -NoTypeInformation -Delimiter ';'

Add columns for CSV output

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

Possible to combine .csv where-object filters?

I'm trying to filter a .csv file based on a location column. The column has various location entries and I only need information from the rows that contain certain locations, that information then gets exported out to a separate .csv file. I can get it to work by searching the .csv file multiple times with each location filter, but I haven't had any luck when trying to combine it into 1 search.
What I have now is:
$csv = Import-Csv "${filepath}\temp1.csv"
$csv | Where-Object location -like "co*" | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -NoTypeInformation
$csv | Where-Object location -like "cc*" | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -Append -NoTypeInformation
$csv | Where-Object location -like "dc*" | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -Append -NoTypeInformation
$csv | Where-Object location -like "mf*" | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -Append -NoTypeInformation
What I'd like to have is something like below. I don't get any errors with it, but all I get is a blank .csv file:
$locations = "co*","cc*","dc*","mf*"
$csv = Import-Csv "${filepath}\temp1.csv"
$csv | Where-Object location -like $locations | select EmployeeNumber | Export-Csv "${filepath}\disablelist.csv" -NoTypeInformation
I've been lurking here for a while and I'm usually able to frankenstein a script together from what I find, but I can't seem to find anything on this. Thanks for your help.
You can replace multiple -like tests with a single -match test using an alternating regex:
$csv = Import-Csv "${filepath}\temp1.csv"
$csv | Where-Object {$_.location -match '^(co|cc|dc|mf)'} |
select EmployeeNumber |
Export-Csv "${filepath}\disablelist.csv" -NoTypeInformation
You can build that regex from a string array:
$locations = 'co','cc','dc','mf'
$LocationMatch = '^({0})' -f ($locations -join '|')
$csv = Import-Csv "${filepath}\temp1.csv"
$csv | Where-Object { $_.location -match $LocationMatch } |
select EmployeeNumber |
Export-Csv "${filepath}\disablelist.csv" -NoTypeInformation