Reputation: 65
When I run my code, it gives me the following error.
What I am having the code do is taking the csv then filtering the name column to only be administrators, then I am having the caption column filter all of the operating systems that I have provided. The if statement is saying that If the column Type0 equals domain then put in the "Unique Account Name" column the Domain0 following the Account0. Else it will put it in Netbios name and then Account0. Below will be the excel sheet. and the desired output of each.
Import-Csv 'U:\Local Group Members.csv' | where-Object {($_.Name0 -eq "administrators") -and ($_.caption0 -match "Microsoft Windows 10 Enterprise|Microsoft Windows 7 Enterprise|Microsoft Windows 7 Professional|Microsoft Windows 8 Enterprise|Microsoft Windows 8 Pro|Microsoft Windows 8.1 Enterprise|Microsoft Windows 8.1 Pro")} |
Select-Object "Netbios_name0", "Name0", "Account0","category0","Domain0","Unique Account Name","Type0","caption0", "Excluded" |
ForEach-Object
If ($_.Type0 -eq 'Domain') {
$_.UniqueAccountName = "$($_.Domain0) - $($_.Account0)"
} Else {
$_.UniqueAccountName = "$($_.Netbios_name0) - $($_.Account0)"
}
Export-Csv -notypeinformation U:\LGMbestone.csv
I am a powershell Novice, and I am stuck, and how do I can I get this code to run?
Upvotes: 0
Views: 4566
Reputation: 61
you can't pipe to an if statement, and you're going to need to iterate through the result line by line to modify the object, which calls for a foreach loop. Also, piping to Export-CSV is better than calling it with an input object. An example based on your code is below. Hope this helps!
$csv = Import-Csv 'U:\Local Group Members.csv' |
Where-Object {($_.Name0 -eq "administrators") -and ($_.caption0 -match "Microsoft Windows 10 Enterprise|Microsoft Windows 7 Enterprise|Microsoft Windows 7 Professional|Microsoft Windows 8 Enterprise|Microsoft Windows 8 Pro|Microsoft Windows 8.1 Enterprise|Microsoft Windows 8.1 Pro")} |
Select-Object "Netbios_name0", "Name0", "Account0","category0","Domain0","Unique Account Name","Type0","caption0", "Excluded"
#Modify each line based on your parameters
Foreach ($row in $csv) {
If ($row.Type0 -eq 'Domain') {
$row."Unique Account Name" = "$($row.Domain0) - $($row.Account0)"
Write-Host $row."Unique Account Name"
} Else {
$row."Unique Account Name" = "$($row.Netbios_name0) - $($row.Account0)"
}
}
#Export CSV
$csv | Export-Csv C:\LGMbestone.csv -NoTypeInformation
Upvotes: 2
Reputation: 9163
looks like one formatting issue. With foreach , it should be something like this:
$import=Import-Csv 'U:\Local Group Members.csv' | where-Object {($_.Name0 -eq "administrators") -and ($_.caption0 -match "Microsoft Windows 10 Enterprise|Microsoft Windows 7 Enterprise|Microsoft Windows 7 Professional|Microsoft Windows 8 Enterprise|Microsoft Windows 8 Pro|Microsoft Windows 8.1 Enterprise|Microsoft Windows 8.1 Pro")} |
Select-Object "Netbios_name0", "Name0", "Account0","category0","Domain0","Unique Account Name","Type0","caption0", "Excluded"
ForEach($imp in $import)
{
If ($imp.Type0 -eq 'Domain')
{
$imp.UniqueAccountName = "$($imp.Domain0) - $($imp.Account0)"
}
Else
{
$imp.UniqueAccountName = "$($imp.Netbios_name0) - $($imp.Account0)"
}
Export-Csv "$imp.UniqueAccountName" -notypeinformation "U:\LGMbestone.csv" -Append
}
Note: I have not checked the import-csv part. I believe its properly taking the data and you should use foreach loop for iterating each row value
Upvotes: 0