简体   繁体   中英

If Else statement Powershell CSV

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. Excel表格 希望输出唯一帐户名

 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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM