Powershell将所有详细日志记录导出到csv文件

时间:2016-02-11 15:06:23

标签: powershell office365

我继承了一个无效的脚本。我需要捕获通常输出到控制台的所有内容,包括脚本中的Success和Error条目。这只是脚本的一小部分,它只捕获错误。将所有输出都输出到文件而不是控制台时,我们将不胜感激。

一个例子是Write-Verbose "VERIFYING contact for $($User.WindowsEmailAddress)"

我知道这是写入控制台,但我需要它写入脚本最底部定义的日志。

Catch
                        {Out-File -InputObject "$(Get-Date -Format MM.dd.yyyy-HH:mm:ss);$($WriteMode);ERROR;Target;$($targetUser.Split('@')[1]);$($User.WindowsEmailAddress);Update;;;Error updating user: $($Error[0])" -FilePath $LogFilePath -Append}

我希望这是有道理的。

### UPDATES

    ForEach ($User in $colUpdContact)
    {
        Write-Verbose "VERIFYING contact for $($User.WindowsEmailAddress)"

        #Filter used to find the target contact object(s)
        $strFilter = "WindowsEmailAddress -eq `"$($User.WindowsEmailAddress)`""
        Try
            {$colContacts2 = Invoke-Command -Session $targetSession -ScriptBlock {param ($strFilter) Get-Contact -Filter $strFilter} -ArgumentList $strFilter -ErrorAction Stop}
        Catch
            {Out-File -InputObject "$(Get-Date -Format MM.dd.yyyy-HH:mm:ss);$($WriteMode);ERROR;Target;$($targetUser.Split('@')[1]);$($User.WindowsEmailAddress);Find;;;Error getting contact: $($Error[0])" -FilePath $LogFilePath -Append}
        ForEach ($Contact in $colContacts2)
        {
            #initialize update string and cmd string
            $strUpdateContact = $null
            $updateCmd = $null
            $strWriteBack = $null
            $writeBackCmd = $null

            #Iterate through attributes and append to the strUpdateContact string if the attribute value has changed
            ForEach ($Attrib in $arrAttribs)
            {
                If ($User.$Attrib -ne $Contact.$Attrib)
                {
                    if($ReadOnly){
                        Add-Content -Path $readOnlyFilePath -Value  "   Changing $Attrib"
                        Add-Content -Path $readOnlyFilePath -Value  "       Before: $($Contact.$Attrib)"
                        Add-Content -Path $readOnlyFilePath -Value  "       After: $($User.$Attrib)"
                    }
                    $strUpdateContact += " -$($Attrib) `"$($User.$Attrib)`""
                    Out-File -InputObject "$(Get-Date -Format MM.dd.yyyy-HH:mm:ss);$($WriteMode);CHANGE;Target;$($targetUser.Split('@')[1]);$($User.WindowsEmailAddress);Update;$($Contact.$Attrib);$($User.$Attrib);" -FilePath $LogFilePath -Append
                }
            }

            #Check if LegacyExchangeDN has been written back to User object
            $mailContact = Invoke-Command -Session $targetSession -ScriptBlock {param ($contact) Get-MailContact $($contact.WindowsEmailAddress)} -ArgumentList $Contact -ErrorAction Stop
            $x500 = "X500:$($mailContact.LegacyExchangeDN)"
            $userRec = Invoke-Command -Session $sourceSession -ScriptBlock {param ($User) Get-Recipient $($User.WindowsEmailAddress)} -ArgumentList $User -ErrorAction Stop

            if($UserRec.emailAddresses -notcontains $x500){
                $userName = ($user.UserPrincipalName).Split('@')[0]
                if($userName -eq "")
                {
                    $userName = $user.SamAccountName
                }
                $strWriteBack = "Set-ADUser -Identity $userName -Add @{ProxyAddresses=`"$x500`"} -Server $sourceDC -Credential `$sourceDCCredential"
            }

            #If there is anything to update
            If ($strUpdateContact.Length -gt 0)
            {
                Write-Verbose "Updating attributes for $($User.WindowsEmailAddress)"
                #Prepend the command for the contact being modified
                $strUpdateContact = "Set-Contact $($User.WindowsEmailAddress) " + $strUpdateContact
                If ($ReadOnly)
                    {Add-Content -Path $readOnlyFilePath -Value  $strUpdateContact}
                Else
                {
                    Try
                    {
                        #Create the complete command and invoke it
                        $updateCmd = "Invoke-Command -Session `$targetSession -ScriptBlock {$($strUpdateContact)}"
                        Invoke-Expression $updateCmd -ErrorAction Stop
                    }
                    Catch
                        {Out-File -InputObject "$(Get-Date -Format MM.dd.yyyy-HH:mm:ss);$($WriteMode);ERROR;Target;$($targetUser.Split('@')[1]);$($User.WindowsEmailAddress);Update;;;Error updating contact: $($Error[0])" -FilePath $LogFilePath -Append}
                }
            }
            If ($strWriteBack){
                Write-Verbose "Updating X500 for $($User.WindowsEmailAddress)"
                Out-File -InputObject "$(Get-Date -Format MM.dd.yyyy-HH:mm:ss);$($WriteMode);CHANGE;Target;$($targetUser.Split('@')[1]);$($User.WindowsEmailAddress);Update;;$x500;" -FilePath $LogFilePath -Append
                If($ReadOnly){
                    Add-Content -Path $readOnlyFilePath -Value  $strWriteBack
                }
                else{
                    Try
                    {
                        Invoke-Expression $strWriteBack -ErrorAction Stop
                    }
                    Catch
                        {Out-File -InputObject "$(Get-Date -Format MM.dd.yyyy-HH:mm:ss);$($WriteMode);ERROR;Target;$($targetUser.Split('@')[1]);$($User.WindowsEmailAddress);Update;;;Error updating user: $($Error[0])" -FilePath $LogFilePath -Append}
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

为什么你不使用Start-Transcript将所有信息输出到日志文件中,然后你可以手动复制你想要的任何东西?

命令的示例:

Start-Transcript -Path $TranscriptOutputFile -Append -Force

#Your script; write-output 'something update';

Stop-Transcript

write-output命令输出的所有内容都将附加到日志文件中。