none
Stream Output For SupportsShouldProcess RRS feed

  • Question

  • Hi guys, 

    I'm using CmdletBinding() within some of my custom Powershell Functions which I've into modules. I've just used Dave Wyatt's Get-CallerPreference to resolve the passing of common parameters to ensure -Verbose and -Debug are supported as I have logging functions which write to file. 

    I'm able to log everything to file. Verbose, Debug, standard output, warnings and errors are all written to file successfully with some stream redirection for commands like Invoke-SQLCmd. 

    I've got SupportsShouldProcess set to $True for some functions where the ConfirmPreference is more than Medium. What I'm finding is when I try and write the output to file, a boolean is returned, presumably because $PSCmdlet.ShouldProcess is enclosed within an If statement like below:

    if ($pscmdlet.ShouldProcess($ENV:COMPUTERNAME, "Remove this file"))
    {

    }

    What I'm looking for is to be able to write the confirm and whatif statements to file for auditing purposes. Does the ShouldProcess statement use Write-Host and the output is lost or am I missing something?

    Is there a way I could have found this information and answered this for myself?

    Thanks

    Tuesday, October 13, 2015 2:29 PM

Answers

  • That makes sense, then aa a workaround you can detect the parameters (whatif etc) and construct the given command yourself.

    Cheers,

    Ruud
    Twitter:    Blog: www.ruudborst.nl  LinkedIn:    
    Note: 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.

    • Marked as answer by AJPatankar Wednesday, October 14, 2015 7:48 AM
    Tuesday, October 13, 2015 3:56 PM

All replies

  • Use $PSBoundParameters before the shouldprocess method to detect all used parameters.

    Cheers,

    Ruud
    Blog: www.ruudborst.nl  Twitter:   LinkedIn:   
    Note: 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.

    Tuesday, October 13, 2015 2:50 PM
  • Hi Ruud, 

    Thanks for your quick response.

    I should have explained in my post, that what I'd be looking for is a way to take the "Performing the operation "Remove this file" on target "localhost" string and use that with later commands? I've already got a function accepting pipeline input where I can pipe the string in and thats written to file as well as displayed to the host.

    Is there a way using $PSBoundParameters that I can capture that string output?

    Cheers, 

    Tuesday, October 13, 2015 3:06 PM
  • Not that I'm aware of but why don't you use 'start-transcript', you can use it in the function.

    Cheers,

    Ruud
    Twitter:    Blog: www.ruudborst.nl  LinkedIn:   
    Note: 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.

    Tuesday, October 13, 2015 3:21 PM
  • Hi Ruud, 

    I had previously been using Start-Transcript, I still use it within my $profile but I was finding PowerShell jobs more and more useful and as of PowerShell v4 I don't believe transcription was supported within background jobs so I followed the logging path.

    I guess as I've moved back to using the host a bit more Start-Transcript would be fine. The thing I prefer about my logging over Start-Transcript is I can tag messages and I'm able to use something like baretail to do the highlighting on the logs making it that bit easier to read.

    Thanks,

    Tuesday, October 13, 2015 3:36 PM
  • I just tested the following command in PowerShell v4 on Windows 7 and v5 on Windows 10

    Start-Job -ScriptBlock {Start-Transcript C:\log.txt ; Get-ChildItem C:\ ; Stop-Transcript}

    Once the job completed, C:\log.txt existed on PowerShell v5 and contained the directory listing output. The file didn't exist on the v4 host. 

    Transcript support looks to have been added for background jobs in PowerShell v5 which is handy to know. 

    Cheers

    Tuesday, October 13, 2015 3:47 PM
  • That makes sense, then aa a workaround you can detect the parameters (whatif etc) and construct the given command yourself.

    Cheers,

    Ruud
    Twitter:    Blog: www.ruudborst.nl  LinkedIn:    
    Note: 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.

    • Marked as answer by AJPatankar Wednesday, October 14, 2015 7:48 AM
    Tuesday, October 13, 2015 3:56 PM
  • I wasn't entirely certain what you meant with that, but I don't really know why I didn't come up with the solution below earlier..

    $Action = 'Stopping Exchange services for maintenance'
    Write-Info -Message "Performing the operation $($Action) on $($ServerGoingIntoMaintenance)" -Level Verbose -AddToExistingLog -LogPath $LogPath
     
    If ($PSCmdlet.ShouldProcess($ServerGoingIntoMaintenance, $Action)){
     
    }

    Write-Info is the custom logging function so this would work which adds the following log entry to file

    [ 2015-10-13 ]-[ 17:02:29 UTC +01:00 ]-[ localhost ]-[ VERBOSE ]-[ Performing the operation Stopping Exchange services for maintenance on server ]

    Thanks for your help

    Tuesday, October 13, 2015 4:05 PM
  • That's exacty what I meant, construct it yourself based on function based cmdlet input.

    Cheers,

    Ruud
    Twitter:    Blog: www.ruudborst.nl  LinkedIn:    
    Note: 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.


    Wednesday, October 14, 2015 7:44 AM