Powershell创建用户脚本无法解析

时间:2014-09-09 21:07:19

标签: powershell

所以我的php问题解决后我继续使用我的PowerShell脚本来创建基于从php进程生成的CSV的用户。但是,当我运行脚本时,不会创建用户,并且没有创建错误日志来向我显示原因。 (虽然这是因为我认为我在脚本中缺少正确记录错误的东西。)在我第一次在这个DC上创建所有用户之前,我使用过这个脚本的一个版本。所以我不知道为什么会这样做。所以我再次想到我会发布我的代码,看看是否有人可以找出问题所在。很抱歉打扰你。看起来这似乎是最好的问题。

Try
{
    Import-Module ActiveDirectory -ErrorAction Stop
}
Catch
{
    Write-Host "[ERROR]`t ActiveDirectory couldn't be loaded. Script will stop." 
    Exit 0
}
$path = "\\NetworkShare\Csvfile"
$date = Get-Date
$addn = (Get-ADDomain).DistuinguishedName
$dnsroot = (Get-ADDomain).dnsroot
$i = 1
$log = "\\NetworkShare\Csvfile.log"
$enabled = $True
Function Start-Commands
{
    Create-Users
}
Function Create-Users
{
    "Processing started (on " + $date + "): " | Out-File $log -append
    "--------------------------------------------" | Out-File $log -append
    Import-CSV $path | ForEach-Object {
        If (($_.FirstName -eq "") -Or- ($_.LastName -eq ""))
        {
            Write-Host "[ERROR]`t No FirstName or LastName provided. Processing skipped for line $($i)`r`n"
            "[ERROR]`t Please provide valid GivenName, LastName and Initials. Processing skipped for line $($i)`r`n" | Out-File $log -append
        }
        Else
        {
           #Set Target OU
           $location = $_.Department + ".$($addn)"
           #Replace dots in names to avoid errors
           $replace = $_.Lastname.Replace(".","")
           #Create Account using name convention of First Initial and Last name
           $sam = $_.FirstName.substring(0,1).ToLower() + $_.LastName.ToLower()
           Try { $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)" }
           Catch { }
           If (!$exists)
           {
            #Set variables according to CSV headers. If headers differ change variables below as well
            $setpass = ConvertTo-SecureString -AsPlainText $_.Password -force
            }
            Try
            {
                Write-Host "[INFO]`t Creating user : $($sam)"
                "[INFO]`t Creating user : $($sam)" | Out-File $log -append
                New-ADUser $sam -GivenName $_.FirstName -Initials $_.Initials `
                -Surname $_.LastName -DisplayName ($_.LastName + "," + $_.FirstName) `
                -UserPrincipalName ($sam + "@" + $dnsroot) -OfficePhone $_.Number `
                -AccoutPassword $setpass -Enabled $enabled
                Write-Host "[INFO]`t Created new user : $($sam)"
                "[INFO]`t Created new user : $($sam)" | Out-File $log -append

                $dn = (Get-ADUser $sam).DistuinguishedName
                 #Move to OU set above
                 If ([adsi]::Exists("LDAP://$($location)"))
                {
                    Move-ADObject -Identity $dn -TargetPath $location
                    Write-Host "[INFO]`t User $sam moved to target OU : $($location)"
                    "[INFO]`t User $sam moved to targ OU : $($location)" | Out-File $log -append
                }
                Else 
                {
                    Write-Host "[ERROR]`t Target OU not found. User Wasn't Moved!"
                    "[ERROR]`t Target OU not found. User wasn't moved!" | Out-File $log -append
                }
                #Object Renamed
                $newdn = (Get-ADUser $sam).DistuinguishedName
                Rename-ADObject -Identity $newdn -NewName ($_.FirstName + " " + $_.LastName)
                Write-Host "[INFO]`t Renamed $($sam) to $($_.GivenName) $($_.LastName)`r`n"
                 "[INFO]`t Renamed $($sam) to $($_.GivenName) $($_.LastName)`r`n" | Out-File $log -append
            }
            Catch
             {
                Write-Host "[ERROR]`t Oops. An error has occured: $($_.Exception.Message)`r`n"
             }
           Else
            {
          Write-Host "[SKIP]`t User $($sam) ($($_.FirstName) $($_.LastName)) already exists or returned an error!`r`n"
          "[SKIP]`t User $($sam) ($($_.FirstName) $($_.LastName)) already exists or returned an error!" | Out-File $log -append

            }
        }
      $i++ 
      "--------------------------------------------" + "`r`n" | Out-File $log -append 
    } 
}

Write-Host "STARTED SCRIPT`r`n"
Start-Commands
Write-Host "STOPPED SCRIPT`r`n"

1 个答案:

答案 0 :(得分:0)

我认为你错误地包装了 If(!$ exists)代码。根据我的编辑,这是If语句的全部内容:

If (!$exists)
           {
            #Set variables according to CSV headers. If headers differ change variables below as well
            $setpass = ConvertTo-SecureString -AsPlainText $_.Password -force
            }

如果我没有弄错的话,你希望以后将所有代码包含在Else子句中。

尝试将第84行的结束括号} 移到第81行的 Else 之前。