简体   繁体   English

如何在PowerShell脚本中导入CSV文件?

[英]How do I import a CSV file in a PowerShell Script?

I am writing a code to update an Active Directory using input from a CSV file. 我正在编写代码,以使用CSV文件中的输入来更新Active Directory。 I am able to get the script to run all of the way to the end, but when I check the Active Directory, it is not updated. 我能够使脚本一直运行到最后,但是当我检查Active Directory时,它没有更新。 When I check the log file, it does not list any CSV file as being used. 当我检查日志文件时,它没有列出正在使用的任何CSV文件。 Everything is in the same folder and the log file writes fine. 一切都在同一个文件夹中,并且日志文件写得很好。

I have tried altering the path that the script searches for the CSV file to C:/PATH/TO/FILE/filename I have also tried with quotes, without quotes, with -Path, and without -Path. 我尝试将脚本搜索CSV文件的路径更改为C:/ PATH / TO / FILE / filename。我也尝试使用引号,不带引号,-Path和-Path。 I have looked at the other StackOverflow articles and they all seem to say that this should work... I think the key line here is the 3rd from the top. 我看过其他StackOverflow文章,他们似乎都说这应该起作用...我认为这里的关键是自上而下的第3条。 "$Users = Import-csv -Path users.csv" I could be wrong though. “ $ Users = Import-csv -Path users.csv”虽然我可能是错的。


        Import-Module ActiveDirectory
        #Import and Specify CSV File
        $Users = Import-csv -Path users.csv
        #Specify Log File
        $LogFile = ".\UpdateUsersCsv.log"

        # Write to the log file. Abort the script if the log file cannot be                 updated.
        Add-Content -Path $LogFile -Value                                 "================================================"
        Add-Content -Path $LogFile -Value "UpdateUsersCsv.ps1"
        Add-Content -Path $LogFile -Value "Update Users from CSV file: $Users"
        Add-Content -Path $LogFile -Value "Started: $((Get-Date).ToString())"
        Add-Content -Path $LogFile -Value "Log file: $LogFile"
        Add-Content -Path $LogFile -Value "------------------------------------------------"

        # Initialize counters.
        $Updated = 0
        $NotFound = 0

        #Find Users
        foreach($User in $Users){
            $ADUser = Get-ADUser
            #If found, update Users and increment $Updated
            If($ADUser){
                Set-ADUser $User.SamAccountName -Description $User.NewDescription
                $Updated = $Updated + 1
            #If not found, increment $NotFound
            }ElseIf(!$ADUser){
                Add-Content -Path $LogFile -Value "User $ASUser not found."
                       $NotFound = NotFound + 1
            }
        }


        Add-Content -Path $LogFile -Value "Number of users updated: $Updated"
        Add-Content -Path $LogFile -Value "Number of users not found: $NotFound"

        Add-Content -Path $LogFile -Value         "================================================"

        Write-Host "Script Completed, check log file $LogFile for more information."

The Active directory should be updated with whatever information is in the CSV file. 应该使用CSV文件中的任何信息来更新Active Directory。 For example, if I used : myName, myDescription goes here It should update the description of the user called "myName" to say "myDescription goes here" Then the log file should say something along the lines of: From csv file: nameOfCSV Users updated = 1 Users not found = 0 例如,如果我使用了:myName,则myDescription会移到此处它应该将名为“ myName”的用户的描述更新为“ myDescription移至此处”,然后日志文件应该这样写:从csv文件:nameOfCSV用户更新= 1未找到用户= 0

If the user is not found, it should report that accordingly, but that is not my current problem. 如果找不到该用户,则应相应地报告该问题,但这不是我当前的问题。

When I run the script and open the log file, there is no csv file shown, and the users updated and users not found fields are left at 0. This leads me to believe that the problem is that the csv file is not importing. 当我运行脚本并打开日志文件时,没有显示csv文件,并且“更新的用户”和“未找到的用户”字段保留为0。这使我相信问题在于csv文件未导入。

I see what the problem is. 我明白问题出在哪里。 Your code will only update the AD, if the user is found. 如果找到用户,您的代码将仅更新AD。 The command to look for an AD user is Get-Aduser -identity <SamAccountName> 查找AD用户的命令是Get-Aduser -identity <SamAccountName>

In your code, you have simply used $ADUser = Get-ADUser without any parameters. 在您的代码中,您仅使用了$ADUser = Get-ADUser而没有任何参数。 I am surprised why you do not get any errors or prompt that asks for -identity parameter value. 我很惊讶为什么您没有收到要求-identity参数值的任何错误或提示。

In your csv, you probably have a column like this. 在您的csv中,您可能有一个像这样的列。

在此处输入图片说明

Then in your foreach loop would change like 然后在你的foreach循环中会像

foreach($User in $Users){
    $ADUser = Get-ADUser $User.UserID
...
}

Now, if the user exists, your code will execute the Set-AdUser block. 现在,如果用户存在,您的代码将执行Set-AdUser块。 In your current code, since it can't actually find the user, $Aduser would presumably (still surprised it gets that far without prompting for Identity) be empty and would always hit the ElseIf block. 在您当前的代码中,由于实际上无法找到用户,因此$Aduser可能为空(仍然感到惊讶,它在不提示输入Identity的情况下$Aduser这么远),并且总是打ElseIf块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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