方法调用失败,因为[System.DBNull]不包含名为'op_Addition'的方法]]

问题描述 投票:0回答:1

下面提到我的代码

cls  

Write-Host "Application Started..."


<# 
if(!$args -or $args.length -lt 1)
{
    Write-Host 'Usage: Usage: <Pass Path to configfile.cfg>'
    Exit 99
}
else
{
    [string]$inputFile = $args[0]      
}
#>

#Write-Host "Config file: $inputFile"

$elapsed = [System.Diagnostics.Stopwatch]::StartNew() 
$iUsers = 0

try 
{    
    # Database variables
    #Test
    $sqlserver = 'CRDBTTENABD01\TTDEV'
    #Model
    #$sqlserver = 'CRDBTTENABM01\TTMOD'
    #Prod
    #$sqlserver = 'CRDBTTENABP01\TTPROD'
    $database = 'inv_Turnkey_Utilities'
    $table = '[dbo].[tbl_FIM_View_Copied]'   ####change to [dbo].[tbl_FIM_View_Transformed] for daily feed #####
    $TabDelimiter = "`t" 
    $CommaDelimiter = ","

    $MyInvocation.MyCommand.Definition 
    $directorypath = Split-Path -Parent $MyInvocation.MyCommand.Definition

    #$Log
    $Log = $directorypath +'\Zapproved.log'
    Write-Host 'Log: $Log'  
    If((Test-Path -Path $Log)){ 
        Remove-Item -Path $Log -Force 
    } 
    #Create file 
    New-Item -Path $Log -ItemType "file" -Force 

    #output file
    $Output =  $directorypath +'\ZapprovedUsers.csv'
    If((Test-Path -Path $Output)){ 
        Remove-Item -Path $Output -Force 
    } 
    #Create file 
    New-Item -Path $Output -ItemType "file" -Force  

    $strHeader = "employeeStatus,LastName,BusinessCity,BusinessState,DeptName,Email,FirstName,HireDate,JobTitle,SupervisorName,TerminationDate,LoginID,Rehiredate,DivisionDesc,AESCid,SupervisorAESCID,User_Type,Location,LocationHierarchy,SupervisorEmail,AlternateLookupIdentifier"
    $strHeader | Out-File -Append -Encoding ascii -Force -filepath $Output    

    #TA Users List
    $UserDataDT = New-Object System.Data.DataTable

    $SQLConnection = New-Object System.Data.SqlClient.SqlConnection
    $SQLConnection.ConnectionString = "Server=$sqlserver;Database=$database;Integrated Security=True"   
    $SQLCmd= New-Object System.Data.SqlClient.SqlCommand
    $SQLConnection.open();
    $SQLCmd.Connection = $SQLConnection
    $SqlCmd.CommandTimeout = 600  ## Allow 10 minutes to complete the operation.    

    $sqltoexecute  = "select employeeStatus,LastName, " 
    $sqltoexecute += "BusinessCity,BusinessState,DeptName,Email,FirstName, "     
    $sqltoexecute += "HireDate, "
    $sqltoexecute += "JobTitle,SupervisorName,TerminationDate,LoginID,Rehiredate, "
    $sqltoexecute += "DivisionDesc,AESCid,SupervisorAESCID,User_Type,Location,LocationHierarchy,SupervisorEmail "
    $sqltoexecute += "from $table"  #test 10 users
    $SQLCmd.CommandText = $sqltoexecute                 
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SQLCmd
    $SqlAdapter.Fill($UserDataDT)       
    #$DataRowCount = $SqlAdapter.Fill($UserDataDT) 
    #$DataRowCount  

    foreach ($Row in $UserDataDT) 
    {
        $LastName                         = ""
        $BusinessCity                     = ""
        $BusinessState                    = ""   
        $DeptName                         = ""        
        $Email                            = ""        
        $FirstName                        = ""       
        $HireDate                         = ""        
        $JobTitle                         = ""        
        $SupervisorName                   = ""  
        $TerminationDate                  = ""  
        $LoginID                          = ""     
        $Rehiredate                       = ""     
        $DivisionDesc                     = ""   
        $AESCid                           = ""   
        $SupervisorAESCID                 = "" 
        $User_Type                        = "" 
        $Location                         = ""
        $LocationHierarchy                = ""
        $SupervisorEmail                  = ""
        $AlternateLookupIdentifier        = ""

        $employeeStatus                   = $Row.employeeStatus   
        $LastName                         = $Row.LastName 
        $BusinessCity                     = $Row.BusinessCity
        $BusinessState                    = $Row.BusinessState    
        $DeptName                         = $Row.DeptName         
        $Email                            = $Row.Email            
        $FirstName                        = $Row.FirstName         
        $HireDate                         = $Row.HireDate         
        $JobTitle                         = $Row.JobTitle         
        $SupervisorName                   = $Row.SupervisorName   
        $TerminationDate                  = $Row.TerminationDate  
        $LoginID                          = $Row.LoginID          
        $Rehiredate                       = $Row.Rehiredate       
        $DivisionDesc                     = $Row.DivisionDesc     
        $AESCid                           = $Row.AESCid           
        $SupervisorAESCID                 = $Row.SupervisorAESCID 
        $User_Type                        = $Row.User_Type        
        $Location                         = $Row.Location         
        $LocationHierarchy                = $Row.LocationHierarchy
        $SupervisorEmail                  = $Row.SupervisorEmail  
        $AlternateLookupIdentifier        = $Row.AESCid

        $LastName = $LastName -replace ",",' '
        $BusinessCity = $BusinessCity -replace ",",';'
        $DeptName = $DeptName -replace ",",';'
        $JobTitle = $JobTitle -replace ",",';'    
        $SupervisorName = $SupervisorName -replace ",",';'  
        $DivisionDesc = $DivisionDesc -replace ",",';'

        $LastName.Length

        #write to file   if ($cdExitCode -ne 0 -or ($matches[1] -ne 0 -and $matches[1] -ne $null))
        if (($LastName.Length -gt 0)) {
            $employeeStatus                   + $CommaDelimiter + `
            $LastName                         + $CommaDelimiter + `
            $BusinessCity                     + $CommaDelimiter + `
            $BusinessState                    + $CommaDelimiter + `
            $DeptName                         + $CommaDelimiter + `
            $Email                            + $CommaDelimiter + `
            $FirstName                        + $CommaDelimiter + `
            $HireDate                         + $CommaDelimiter + `
            $JobTitle                         + $CommaDelimiter + `
            $SupervisorName                   + $CommaDelimiter + `
            $TerminationDate                  + $CommaDelimiter + `
            $LoginID                          + $CommaDelimiter + `
            $Rehiredate                       + $CommaDelimiter + `
            $DivisionDesc                     + $CommaDelimiter + `
            $AESCid                           + $CommaDelimiter + `
            $SupervisorAESCID                 + $CommaDelimiter + `
            $User_Type                        + $CommaDelimiter + `
            $Location                         + $CommaDelimiter + `
            $LocationHierarchy                + $CommaDelimiter + `
            $SupervisorEmail                  + $CommaDelimiter + `           
            $AlternateLookupIdentifier  | Out-File -Append -Encoding ascii -Force -filepath $Output  

            $iUsers++   
        }    

    } 

    Write-Host "Total Elapsed Time   : $($elapsed.Elapsed.ToString())" 
    Write-Host "Total number of users: $iUsers"
    Write-Host "Application Completed..." 

    EXIT 0

} 
catch [Exception] {
    Write-Host "catch Exception..."

    Write-Host ($_.Exception )

    Exit 99
}

在加载29k条记录中的8k条记录后,出现以下错误捕获异常...

**System.Management.Automation.RuntimeException: Method invocation failed because [System.DBNull] does not contain a method named 'op_Addition'.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)** 

下面提到我的代码cls写主机“应用程序已启动...”'退出99} else ...

powershell
1个回答
0
投票

正如Lee_Dailey所提到的,如果您只是通过管道传输到Export-Csv,则可以跳过所有此手工拼接在一起的csv数据:

© www.soinside.com 2019 - 2024. All rights reserved.