使用powershell在CSV中编辑值

使用powershell在CSV中编辑值,powershell,csv,Powershell,Csv,对于一个小项目,我想从一个小的CSV文件创建广告用户。我制作了每隔几分钟运行一次的作业(PowerShell脚本),以根据我在脚本中定义的用户的状态获取用户创建。每例;在广告中创建用户时,我将状态更改为addmailbox。然后另一个作业将出现并添加邮箱 一切都很顺利,但我注意到一个错误。我在CSV文件中编辑状态的方式是错误的,因为我只想更改CSV中所有用户的状态。但我似乎无法让任何其他方法起作用 #When encountering any error, stop the script. $E

对于一个小项目,我想从一个小的CSV文件创建广告用户。我制作了每隔几分钟运行一次的作业(PowerShell脚本),以根据我在脚本中定义的用户的状态获取用户创建。每例;在广告中创建用户时,我将状态更改为
addmailbox
。然后另一个作业将出现并添加邮箱

一切都很顺利,但我注意到一个错误。我在CSV文件中编辑状态的方式是错误的,因为我只想更改CSV中所有用户的状态。但我似乎无法让任何其他方法起作用

#When encountering any error, stop the script.
$ErrorActionPreference = "Stop";

#Check if there is a data file.
$FileExists = Test-Path C:\inetpub\wwwroot\melle\data.csv

if($FileExists -eq $true) {
    $Users = Import-Csv -Path "C:\inetpub\wwwroot\melle\data.csv"
    foreach ($user in $Users) {
        $Status = $User.Status;
        $userPrincipalName = $User.userPrincipalName;
        $SAMAccountName = $User.SAMAccountName;
        $userInstance =  $User.userInstance;
        $Name = $User.Name;
        $displayName = $User.DisplayName;
        $Path = '"' + $User.Path + '"';
        $GivenName = $User.GivenName;
        $Surname = $User.Surname;
        $SIP = $userPrincipalName;

        if ($Status -eq "CreateUser") {
            try {
                #create user
                Import-Module ActiveDirectory; 
                New-ADUser -SAMAccountName $SAMAccountName -Instance $userInstance -Name $name -DisplayName $displayName -Path "correct.path.com" -GivenName $givenname -Surname $surname -userPrincipalName $userprincipalname -AccountPassword (ConvertTo-SecureString -String "bla" -AsPlainText -Force) -PassThru | Enable-ADAccount;

                #change status
                (Get-Content  C:\inetpub\wwwroot\melle\data.csv) | Foreach-Object {$_ -replace 'CreateUser','AddMailbox'}  | Out-File  C:\inetpub\wwwroot\melle\data.csv

                #exit on completion
                Exit(Write-Host 'User was created in AD.');
            } Catch {
                #write any errors to error log.
                $_ | Out-File C:\inetpub\wwwroot\melle\errors.log -Append;
            }
        } else {
            Exit(Write-Host 'No user with status CreateUser was found.');
        }
    }
}else {
    Exit(Write-Host 'No data.csv file was found.');
}
我现在的做法是
(获取内容C:\inetpub\wwwroot\melle\data.csv)| Foreach对象{$\u替换'CreateUser','AddMailbox'}|输出文件C:\inetpub\wwwroot\melle\data.csv
,但我只想为脚本在
Foreach
中与之对话的行定义它

我尝试在这里和不同的网站上搜索解决方案,但我无法获得其他人用于我的脚本的解决方案。有些脚本太高级了,我无法理解

只有在循环中更改我正在处理的行的状态的正确方法是什么

更新:已解决 工作脚本:

#Check if there is a data file.
$FileExists = Test-Path C:\inetpub\wwwroot\melle\data.csv

if($FileExists -eq $true) {
$Users = Import-Csv -Path "C:\inetpub\wwwroot\melle\data.csv"

$UsersThatNeedToBeCreated = $Users | Where-Object {$_.Status -eq 'CreateUser'};

# Check that we have users that need to be created, might need fine-tuning.
if($UsersThatNeedToBeCreated -eq $null -or $UsersThatNeedToBeCreated.Count -eq 0){
    # Write-Host doesn't have a meaningful return code, so you can separate those lines.

    Exit(Write-Host 'No user with status CreateUser was found.');
}else{
    # This way it's only run once
    Import-Module ActiveDirectory; 
}

$Users | ForEach-Object {
    if($_.Status -eq 'CreateUser'){
        try {
            #create user
            New-ADUser -SAMAccountName $_.SAMAccountName -Instance "'" + $_.userInstance + "'" -Name $_.name -DisplayName $_.displayName -Path "working.path.here" -GivenName $_.givenname -Surname $_.surname -userPrincipalName $_.userprincipalname -AccountPassword (ConvertTo-SecureString -String "Welcome01" -AsPlainText -Force) -PassThru | Enable-ADAccount;

            #change status
            $_.Status = 'AddMailbox';
        } Catch {
            #write any errors to error log.
            $_ | Out-File C:\inetpub\wwwroot\melle\errors.log -Append;
        }
    }
}

$Users | ConvertTo-Csv | Out-File 'C:\inetpub\wwwroot\melle\data.csv'


Exit(Write-Host 'User was created in AD.');
}else {

Exit(Write-Host 'No data.csv file was found.');
}

正如您已经注意到的,您当前的方法不起作用。您正在获取CSV的全部内容并替换每个实例。该
Import Csv
语句实际上为您提供了一个允许您更改值的数据结构。你只需要事后再写下来

这种方法仍然很容易出错,您将遇到问题。如果我理解正确,您希望跟踪状态,并拥有多个执行不同操作的脚本。您迟早会遇到这样的情况,即由于相互竞争的访问请求,它们会相互覆盖更改和/或锁定。如果你想这样做,你应该考虑使用支持基于行的锁定的数据库(最有可能的是)。否则,您需要找到一种方法使它们按顺序运行,或者自己实现基于行的锁定

也就是说,一个可能的解决方案可能如下所示。我没有运行它,但基本结构应该是正确的。覆盖更改的一个例子是创建邮箱可能需要很长时间。由于该文件仅在脚本开始时读取,在脚本结束时写入,因此可能会在这段时间内发生更改

#When encountering any error, stop the script.
$ErrorActionPreference = "Stop";

#Check if there is a data file.
$FileExists = Test-Path C:\inetpub\wwwroot\melle\data.csv

if($FileExists -eq $true) {
    $Users = Import-Csv -Path "C:\inetpub\wwwroot\melle\data.csv"

    $UsersThatNeedToBeCreated = $Users | Where-Object {$_.Status -eq 'CreateUser'};

    # Check that we have users that need to be created, might need fine-tuning.
    if($$UsersThatNeedToBeCreated -eq $null -or $UsersThatNeedToBeCreated.Count -eq 0){
        # Write-Host doesn't have a meaningful return code, so you can separate those lines.
        Write-Host 'No user with status CreateUser was found.'
        Exit;
    }else{
        # This way it's only run once
        Import-Module ActiveDirectory; 
    }

    $Users | ForEach-Object {
        $Status = $User.Status;
        $userPrincipalName = $User.userPrincipalName;
        $SAMAccountName = $User.SAMAccountName;
        $userInstance =  $User.userInstance;
        $Name = $User.Name;
        $displayName = $User.DisplayName;
        $Path = '"' + $User.Path + '"';
        $GivenName = $User.GivenName;
        $Surname = $User.Surname;
        $SIP = $userPrincipalName;

        if($_.Status -eq 'CreateUser'){
            try {
                #create user
                New-ADUser -SAMAccountName $SAMAccountName -Instance $userInstance -Name $name -DisplayName $displayName -Path "correct.path.com" -GivenName $givenname -Surname $surname -userPrincipalName $userprincipalname -AccountPassword (ConvertTo-SecureString -String "bla" -AsPlainText -Force) -PassThru | Enable-ADAccount;

                # change status
                $_.Status = 'AddMailbox';
            } Catch {
                # write any errors to error log.
                $_ | Out-File C:\inetpub\wwwroot\melle\errors.log -Append;
            }
        }
    }

    $Users | ConvertTo-Csv -NoTypeInformation | Out-File 'C:\inetpub\wwwroot\melle\data.csv'

    Write-Host 'User was created in AD.'
    Exit
}else {
    Write-Host 'No data.csv file was found.'
    Exit
}

嗨,赛斯,谢谢你的帮助。我喜欢您所做的更改,但CSV中的状态不会以这种方式更改。它保持不变。此外,不能使用Exit();在()中没有任何表达式,我注意到。CSV文件插入了以下行:
#键入System.Management.Automation.PSCustomObject
。用户没有被创建,但是我注意到我在脚本中使用的变量已经不存在了,所以我将再次定义这些变量。对不起,我只关注于更改状态。您应该能够在没有括号的情况下运行
Exit
。很难猜测你的实际目标是什么。写入主机将写入实际控制台,并且无法轻松重定向。此外,通过使用括号调用
Exit
,您(可能)正在从
Write Host
返回空结果,这对您没有任何帮助。您是对的,问题是我不需要捕捉这些错误,我只希望脚本在发生错误时退出。我要记录的唯一错误是命令中是否出现任何错误,该命令正在工作。我做了一些小改动,让它工作了。非常感谢!我将用更新的脚本编辑我的帖子。此外,正如您在编辑中看到的,您需要传递
-NoTypeInformation
,以跳过。。。键入转换为Csv的
信息。如果您有多个脚本出于上述原因访问此文件,我仍然不建议您使用这种方法。如果这是有帮助的,如果你愿意的话,不妨考虑把它标记为一个答案或是上投票。再次感谢你,因为我在这上面停留了一段时间,你帮了我很大的忙。如果我有足够的代表投票,我会这么做:)