none
Verbose Logging from within Invoke-Command RRS feed

  • Question

  • Hello,

    I cannot figure out how to get this done. I want to Start-Transcript and then log all the verbose information being done inside an Invoke-Command.

    Here is my script. I have tried putting the logging inside the scriptblock using the $using variable anything I try fails.

    # Resets Counters
    $filesLocked = 0
    $filesDeleted = 0
    $foldersDeleted = 0
    
    # Sets Logging Parameters
    $KeepLogsFor = 15
    $VerbosePreference = "Continue"
    $LogPath = Split-Path $MyInvocation.MyCommand.Path
    Get-ChildItem "$LogPath\*.log" | Where LastWriteTime -LT (Get-Date).AddDays(-$KeepLogsFor) | Remove-Item -Confirm:$false
    $LogPathName = Join-Path -Path $LogPath -ChildPath "$($MyInvocation.MyCommand.Name)-$(Get-Date -Format 'MM-dd-yyyy').log"
    Start-Transcript $LogPathName -Append
    
    # Connects to Home Drives & Finds Files / Folders in CIP to be Deleted
    Invoke-Command -ComputerName <servername> -Scriptblock {
    $dirs = Get-Childitem -Path "E:\Home" -Exclude Windows -Directory;
        ForEach ($dir in $dirs) {
        $newDirs = "$dir\Documents\CIP\"
        $testpath = Test-Path $newDirs
            If ($testpath) {
            #Deletes Files Only
                $childItemsFile = Get-ChildItem -Path $newDirs -Include st_* -Exclude STAC,upload -File -Recurse -Force
                    ForEach ($childItemFile in $childItemsFile) { 
                       Try {
                            [IO.File]::OpenWrite($childItemFile).close()                       
                            Remove-Item -Path $childItemFile -Force -Verbose -ErrorAction Stop
                            $filesDeleted++                                               }
                        Catch {
                              $filesLocked++
                              }
                                         
                    }
                    #Deletes Folders after making sure they are empty
                    Get-ChildItem -Path $newDirs -Include st_* -Exclude STAC,upload -Recurse -Force | 
                        ForEach-Object {
                                If($_.PSIsContainer -eq $true) {
                                    If ((Get-ChildItem -Path $_.FullName) -eq $null) {
                                    Remove-Item -Path $_.FullName -Force -Verbose 
                                    $foldersDeleted++
                                    }
                               }
                        }    
            } 
        }   
    Return "Files Locked: $filesLocked Files Deleted: $filesDeleted Folders Deleted: $foldersDeleted"
    
    } -Credential $cred
    Stop-Transcript

    From what I have read for some reason verbose logging will not transfer thru the invoke-command back to the machine that the script is running on. Any suggestions?

    Thanks,

    Phil

    Thursday, November 12, 2015 2:12 PM

Answers

  • Hi Phil,

    As per my testing Invoke-Command supresses Verbose output anywhre but the screen. Even when the query is to the same computer.

    As a workaround you can redirect the verbose stream 4 to Stdout 1 using this 4>&1

    Invoke-Command -ComputerName . {write-verbose "Hello" -verbose 4>&1}

    You will not see the VERBOSE: prefix, but the actual content will be visible.

    You might also try using $VerbosePreference = "Continue" inside the ICM script block. Because command is running on the other computer the current local session setting won't apply.

    PS C:\Scripts> $VerbosePreference = "Continue"
    PS C:\Scripts> Invoke-Command -ComputerName Server-2 {$VerbosePreference}
    
    PSComputerName                          RunspaceId                              Value
    --------------                          ----------                              -----
    Server-2                                                                        SilentlyContinue
    


    References:

    Understanding Streams, Redirection, and Write-Host in PowerShell

    Capture Warning, Verbose, Debug and Host Output via alternate streams


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.



    • Edited by Satyajit321 Thursday, November 19, 2015 12:46 PM
    • Marked as answer by Phil_Adler Thursday, November 19, 2015 1:39 PM
    Thursday, November 19, 2015 12:42 PM

All replies

  • Hi Phil,

    As per my testing Invoke-Command supresses Verbose output anywhre but the screen. Even when the query is to the same computer.

    As a workaround you can redirect the verbose stream 4 to Stdout 1 using this 4>&1

    Invoke-Command -ComputerName . {write-verbose "Hello" -verbose 4>&1}

    You will not see the VERBOSE: prefix, but the actual content will be visible.

    You might also try using $VerbosePreference = "Continue" inside the ICM script block. Because command is running on the other computer the current local session setting won't apply.

    PS C:\Scripts> $VerbosePreference = "Continue"
    PS C:\Scripts> Invoke-Command -ComputerName Server-2 {$VerbosePreference}
    
    PSComputerName                          RunspaceId                              Value
    --------------                          ----------                              -----
    Server-2                                                                        SilentlyContinue
    


    References:

    Understanding Streams, Redirection, and Write-Host in PowerShell

    Capture Warning, Verbose, Debug and Host Output via alternate streams


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.



    • Edited by Satyajit321 Thursday, November 19, 2015 12:46 PM
    • Marked as answer by Phil_Adler Thursday, November 19, 2015 1:39 PM
    Thursday, November 19, 2015 12:42 PM
  • That worked perfectly, thanks so much for your help!
    Thursday, November 19, 2015 1:39 PM
  • You're welcome Phil.

    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Saturday, November 21, 2015 11:50 AM