2

I have written an automation script in Powershell that runs a small number of functions many times...

I am beginning to get my head around Powershell, but I still do pleeeenty of stupid stuff... I have to wonder if I am missing the obvious.

Each time a function runs, it relies on a few variables passed to it along with some user input... Nothing to tricky, restarting remote services, syncing time services etc.. I would like to log the results as I go, but it seems like I will be adding more code to log, than the actual code that I am using to do the work. Is this normal, or am I missing something?

I have looked into the Try/Catch/Finally blocks, but they seem rather unwieldy and I seem to be nesting functions pretty deep with that approach... Here is a stripped down example of a function that I am using:

    Function MyFunc($Param1, $Param2){
Do{
  $Var = Get-Something | Select Name, MachineName, Status 
 $NotherVar = Read-Host -Prompt "Do you want to Stop or Start or check the $Var (1 to Start, 2 to stop, 3 to check, 4 to continue)?" 
    If ($SetState -eq 1) 
     {
      Do Stuff
    }
    ElseIf ($Var -eq 2) 
       {
      Do Stuff
    }
    ElseIf ($Var -eq 3)
       {
      Do Stuff
    }
  }
    Until ($Var -eq 4)
Do other stuff
} 

Is it possible to simply Add-Content $logpath $FunctionResult without an act of congress?

3
  • Wow... Downvoted. That's a bit harsh. Did I post to the wrong forum? Was it something I said? Bad breath? I don't get it... Commented Nov 13, 2017 at 22:56
  • I guess I am wondering if there is something like -Tee, 2> or something similar to record the success/failure of a function to a logfile... Commented Nov 13, 2017 at 22:59
  • In addition to my answer below, Start-Transcript may do all you are looking for as well. Commented Nov 13, 2017 at 23:26

1 Answer 1

2

Create a "run and log" function and call it with your "do stuff" as a scriptblock parameter to this function.

EXAMPLE:

function Invoke-RunScriptblockAndLogResult([scriptblock]$sb)
{
    $result = Invoke-Command -ScriptBlock $sb

    # log result
    Write-Host "RESULT: $result"
}

Invoke-RunScriptblockAndLogResult {2+2; 3*3}

$myScript = {
    4+4
    5*5
}

Invoke-RunScriptblockAndLogResult $myScript

OUTPUT:

RESULT: 4 9
RESULT: 8 25

Of course the Write-Host above would be your Add-Content or whatever...

Sign up to request clarification or add additional context in comments.

6 Comments

I regret that I have -ne 15 reputation and cannot give you the +1 that you so richly deserve... I will eventually circle back and +1 when I get there. At first glance this appears like it is just what the Dr. ordered. You sir, are genius. I appreciate your time and effort. May the Force be with you.
Now all I need to do is figure out what you did. I kinda get it, maybe... Scripting is not my natural habitat, but I am learning in a hurry. Again, Thank you!
Yay... Rep -eq 15 so here is your +1 as promised.
Welcome to stack overflow @MarcEverlove.
I have lurked forever... I try to search before I ask... And when I find something close, I tweak it until I cry tears of blood, then I search some more ... then ask.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.