How can I select server name in below script using PowerShell? - powershell

$s = Get-Content #("C:\servers.txt") | ForEach-Object {
(Get-Acl $_ ).Access |
Select-Object IdentityReference, FileSystemRights, AccessControlType,
IsInherited
} | Export-Csv "C:\t8.csv" -NoTypeInformation -Encoding UTF8

You mean how you can add the servername as an additional column to the output CSV? Simply add it with a calculated property in the Select-Object:
$server = $_
(Get-Acl $server).Access |
Select-Object #{n='Servername';e={$server}}, IdentityReference, ...
Note that your assignment $s = ... is pointless, because all output goes to the CSV, so nothing is ever assigned to $s.

Related

Look for a specific value in a csv file powershell

so i'm kinda new to PS, I've spent the whole morning trying to figure this out and looking for similar questions/answers here and on Google.
Basically this is my script:
$value = $env:COMPUTERNAME
$csv = Import-Csv -Path '\\UNC\PATH\file.csv'
$props = 'CsSystemFamily','CsDNSHostName', 'CsManufacturer'
Get-ComputerInfo | Select-Object -Property $props | Export-Csv -Path $csv -NoTypeInformation - Delimiter ';' -Append
i'm deploying this as GPO since i need to collect this specific data from some OU's. The thing is i want to check first if the computername exists or not in the CsDNSHostName column so that my script wont add the same computer twice.
Thanks in advance,
I've tried multiple things, but the last thing i found was this:
$value = $env:COMPUTERNAME
if ($file-contains $value) {
write-host 'Computer name already exists'
} else {
Get-ComputerInfo | Select-Object -Property $props | Export-Csv -Path $csv -NoTypeInformation - Delimiter ';' -Append
}
this didn't semm to work since it would just skip if and go straight to else
-contains is the right operator to use, but you must apply it to the array of CsDNSHostName property (column) values, which is easy to do, thanks to member-access enumeration:
$props = 'CsSystemFamily','CsDNSHostName', 'CsManufacturer'
$csvFile = '\\UNC\PATH\file.csv'
$csv = Import-Csv $csvFile -Delimiter ';'
# $csv.CsDNSHostName returns an array of
# all CsDNSHostName column (property) values.
if ($csv.CsDNSHostName -contains $env:COMPUTERNAME) {
Write-Host 'Computer name already exists'
} else {
Get-ComputerInfo |
Select-Object -Property $props |
Export-Csv $csvFile -NoTypeInformation -Delimiter ';' -Append
}

Filtering Data By Property

I'm seraching for a solution to filter entries in a csv in Powershell
My File looks like this
Header1;Header2;Header3
Tom;15;15.12.2008
Anna;17;
Tim;18;12.01.2007
My Code looks atm like this :
$altdaten = Get-Content -Path $altdatenpf | Select-Object -skip 1 |`
ConvertFrom-Csv `
-Delimiter ";"`
-Header $categoriesCSV
$neudaten = Get-Content -Path $neudatenpf | Select-Object -skip 1 |`
ConvertFrom-Csv `
-Delimiter ";"`
-Header $categoriesCSV
$zdaten = foreach ($user in $neudaten)
{
Where-Object $user.Austrittsdatum -EQ ''
}
$zdaten | export-Csv -Path '.\Test\zwischendaten.csv'
In this case i want delete all entrys that are like tim and Tom, they have entrys in header3
Thank you in advance
You can try something like this:
$altdaten = Get-Content -Path .\pathToCsvFile.csv | ConvertFrom-Csv -Delimiter ';'
$zdaten = $altdaten | Where-Object { $_.Header3 -eq $null }
Your equality operator checks actually an empty string. However, in your CSV file, there is nothing. Hence, you need to check for $null.
Alternatively, if you are not sure, you can use:
$zdaten = $altdaten | Where-Object { [string]::IsNullOrEmpty($_.Header3) }
This covers either option and also looks appealing (for me at least).

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.

How do I find text between two words and export it to txt.file

I have a CSV file which contains many lines and I want to take the text between <STR_0.005_Long>, and µm,5.000µm.
Example line from the CSV:
Straightness(Up/Down) <STR_0.005_Long>,4.444µm,5.000µm,,Pass,‌​2.476µm,1.968µm,25,0‌​.566µm,0.720µm
This is the script that I am trying to write:
$arr = #()
$path = "C:\Users\georgi\Desktop\5\test.csv"
$pattern = "(?<=.*<STR_0.005_Long>,)\w+?(?=µm,5.000µm*)"
$Text = Get-Content $path
$Text.GetType() | Format-Table -AutoSize
$Text[14] | Foreach {
if ([Regex]::IsMatch($_, $pattern)) {
$arr += [Regex]::Match($_, $pattern)
Out-File C:\Users\georgi\Desktop\5\test.txt -Append
}
}
$arr | Foreach {$_.Value} | Out-File C:\Users\georgi\Desktop\5\test.txt -Append
Use a Where-Object filter with your regular expression and simply output the match to the output file:
Get-Content $path |
Where-Object { $_ -match $pattern } |
ForEach-Object { $matches[0] } |
Out-File 'C:\Users\georgi\Desktop\5\test.txt'
Of course, since you have a CSV, you could simply use Import-Csv and export the value of that particular column:
Import-Csv $path | Select-Object -Expand 'column_name' |
Out-File 'C:\Users\georgi\Desktop\5\test.txt'
Replace column_name with the actual name of the column. If the CSV doesn't have a column header you can specify one via the -Header parameter:
Import-Csv $path -Header 'col1','col2','col3',... |
Select-Object -Expand 'col2' |
Out-File 'C:\Users\georgi\Desktop\5\test.txt'

powershell group object and create csv with new header header

In Windows Powershell, how can I change the csv output format of executing this command:
$logs = 'System'
$today = (Get-Date).Date
$logs | foreach{
get-eventlog -log $_ -After ([datetime]::today) |
group eventid,EntryType -noel |
sort count -desc |
export-csv "eventLog_$_.csv" -notype
}
My output must be :
"Values","Count","Group","ID","Severity"
"System.Collections.ArrayList","1085","System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]","1501", "Information"
"System.Collections.ArrayList","15","System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]","37", "Information"
"System.Collections.ArrayList","13","System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]","1500", "Information"
"System.Collections.ArrayList","8","System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]","20001", "Information"
But when running the command above, I got this output instead:
"Values","Count","Group","Name"
"System.Collections.ArrayList","1085","System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]","1501,Information"
"System.Collections.ArrayList","15","System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]","37,Information"
"System.Collections.ArrayList","13","System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]","1500,Information"
"System.Collections.ArrayList","8","System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]","20001,Information"
Is there a better way than replacing with csv file modification ?
I not sure why you would want a bunch of lines in your file like "System.Collections.ArrayList" and so forth. You output that you should expect from your command is
Count Name
----- ----
116 7036, Information
5 7040, Information
1 206, Information
1 14204, Information
1 7001, Information
1 201, Information
The extra information can be stripped out with a simple Select-Object. The for each loop is not required unless you acutually have something more in $logs. If the case then you should have -Append on your Export-Csv. You also dont use $today
$logs = 'System'
get-eventlog -log $logs -After ([datetime]::today) |
Group-Object EventID,EntryType -NoElement |
Sort-Object Count -Descending |
Select-Object count,name |
Export-Csv C:\temp\test.txt -NoTypeInformation
A bonus to help
The above answer might be sufficient but this adds a little to it. I mentioned that your loop seemed odd. If you are looking at other logs than System then i could see a point. I have an example below. Also the return for Name contains both and eventID and EventType and we could do something about that. I create a PSCustomObject that splits the Name into its two parts.
$logs = 'System','Application'
$logs | ForEach-Object{
get-eventlog -log $_ -After ([datetime]::today) |
Group-Object EventID,EntryType -NoElement |
Sort-Object Count -Descending |
Select-Object count,name | ForEach-Object{
[PSCustomObject]#{
'EventID' = ($_.Name -split ", ")[0]
'EventType' = ($_.Name -split ", ")[1]
'Count' = $_.Count
}
}
} | Export-Csv C:\temp\test.txt -NoTypeInformation
The CSV output looks a little better now. The following is from the truncated csv
"EventID","EventType","Count"
"7036","Information","118"
"7040","Information","5"
"102","Information","1"
"902","0","1"
"300","Information","1"
"10","Error","1"
"302","Information","1"
If you only have powershell 2.0 you wont have access to [PSCustomObject] in which cause you could do the following instead. Notice the array $csv that is built in the ForEach loop and is then piped into Export-Csv
$csv = #()
$logs = 'System','Application'
$logs | ForEach-Object{
get-eventlog -log $_ -After ([datetime]::today) |
Group-Object EventID,EntryType -NoElement |
Sort-Object Count -Descending |
Select-Object count,name | ForEach-Object{
$properties = #{
'EventID' = ($_.Name -split ", ")[0];
'EventType' = ($_.Name -split ", ")[1];
'Count' = $_.Count;
}
$psobject = new-object psobject -Property $properties
$csv += $psobject
}
}
$csv | Export-Csv C:\temp\test.txt -NoTypeInformation