【问题标题】:POWERSHELL on server 2019 : script not working服务器 2019 上的 POWERSHELL:脚本不起作用
【发布时间】:2020-01-21 11:00:51
【问题描述】:

这是 powershell 脚本:

copy-item "\\xxx.xlsx" -destination "c:\liste\liste.xlsx" -force
$filepath = "c:\liste\liste.xlsx"

$Excel = New-Object -ComObject excel.application
$Workbook = $excel.Workbooks.open($filepath)
$Worksheet = $Workbook.WorkSheets.item("Feuil1") 
$Worksheet.activate()

$line = 4
$col = 1
$MobilePhone=''
$DECTPhone=''
$FixePhone=''
$DN=''

while ($worksheet.Cells.Item($line,1).Value())
    {
    if ($worksheet.Cells.Item($line,4).Value() -like "*06-??-??-??-??*" -or $worksheet.Cells.Item($line,4).Value() -like "*07-??-??-??-??*"  -or $worksheet.Cells.Item($line,4).Value() -like "*???-???-????*" -or $worksheet.Cells.Item($line,4).Value() -like "*????-???-????-????*")
        { 
        $MobilePhone = $worksheet.Cells.Item($line,4).Value()
        }
    if ($worksheet.Cells.Item($line,5).Value() -like "*[0-9][0-9][0-9][0-9]*")
        {  
        $DECTPhone = $worksheet.Cells.Item($line,5).Value() 
        }
    if ($worksheet.Cells.Item($line,6).Value() -like "*01-??-??-??-??*" -or $worksheet.Cells.Item($line,6).Value() -like "*02-??-??-??-??*" -or $worksheet.Cells.Item($line,6).Value() -like "*03-??-??-??-??*" -or $worksheet.Cells.Item($line,6).Value() -like "*04-??-??-??-??*" -or $worksheet.Cells.Item($line,6).Value() -like "*05-??-??-??-??*" -or $worksheet.Cells.Item($line,6).Value() -like "*09-??-??-??-??*" -or $worksheet.Cells.Item($line,6).Value() -like "*????-???-????-????*")
        { 
        $FixePhone = $worksheet.Cells.Item($line,6).Value()
        }

    $NOM = $worksheet.Cells.Item($line,1).Value()
    $PRENOM = $worksheet.Cells.Item($line,2).Value()

    if ((Get-ADObject -LDAPFilter "(&(GivenName=$PRENOM)(Sn=$NOM))" | ft -HideTableHeaders DistinguishedName | Out-String).Trim()) 
        {
        $DN = (Get-ADObject -LDAPFilter "(&(GivenName=$PRENOM)(Sn=$NOM))" | ft -HideTableHeaders DistinguishedName | Out-String).Trim()
        #Dans le cas ou l'utilisateur à plusieurs comptes, on place $DN dans un foreach
        foreach ( $a in $($DN -split "`r`n") )
            {
            if ( $MobilePhone -eq '' )
                {
                Set-ADObject -Identity $a -Clear mobile
                }
            else
                {
                    if ($MobilePhone -like "*0?-??-??-??-??*")
                    {
                    Set-ADObject -Identity $a -Replace @{mobile=$MobilePhone}
                    }
                }

            if ( $DECTPhone -eq '' )
                {
                Set-ADObject -Identity $a -Clear otherTelephone
                }
            else
                {
                Set-ADObject -Identity $a -Replace @{otherTelephone=$DECTPhone}
                }   

            if ( $FixePhone -eq '' )
                {
                Set-ADObject -Identity $a -Clear telephoneNumber
                }
            else
                {
                    if ($FixePhone -like "*0?-??-??-??-??*")
                    {
                    Set-ADObject -Identity $a -Replace @{telephoneNumber=$FixePhone}
                    }
                }               
            }
        }
    $MobilePhone=''
    $DECTPhone=''
    $FixePhone=''
    $DN=''

    $line++
    }

$workbook.Close()
$excel.Quit()

脚本在 Windows 2008 R2 上运行,但在 Windows server 2019 上不运行。

Set-ADObject : 输入无效的 « System.Double »。 Nom du paramètre : otherTelephone Au caractère Ligne:63 : 5 + ... Set-ADObject -Identity $a -Replace @{otherTelephone=$DECT ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : InvalidArgument : (CN=XXXX\, Xx...T,DC=XXX,DC=XXX:ADObject) [Set-ADObject], ArgumentException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.SetADObject

当我使用 get-adobject 初始化 $NOM$PRENOM$DN 时,set-adobject 命令可以正常工作而不会出现 system.double 错误. 感谢您的帮助。

【问题讨论】:

    标签: windows powershell


    【解决方案1】:

    找到了。 您应该在每个 set-adobject 行上加上双引号。

    Set-ADObject -Identity $a -Replace @{otherTelephone="$DECTPhone"}

    解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多