#Deleted

32 messages · Page 1 of 1 (latest)

vestal verge
#

If you use a param on your script, then you can just .\Script.ps1 -DebugLog

[CmdletBinding()]
param(
    [switch]$DebugLog
)

if ($DebugLog) {
    Start-Transcript "D:\_Debug.log"
}

function Test-Reg {
    Write-Host "Adding test registry keys..."
    reg.exe add HKCU\test /f $nul
    reg.exe query HKCU\test $nul

    if ($LASTEXITCODE -eq 0) {
        Write-Host "Done."
    }
    else {
        Write-Host "Failed."
    }

    Write-Host "Deleting test registry keys..."
    reg.exe delete HKCU\test /f $nul
    reg.exe query HKCU\test $nul

    if ($LASTEXITCODE -eq 0) {
        Write-Host "Failed."
    }
    else {
        Write-Host "Done."
    }
}

Test-Reg

if ($DebugLog) {
    Stop-Transcript
}
sweet dune
#

Having some trouble understanding what you want. Start-Transcript is the easy mode with logging. Everything else requires code changes. A Command like Tee-Object comes to mind. You can also look at help about_Redirection.

tardy talon
#

Just store the output to a variable and log it if desired. You don’t have to use the variable if you wish or you could pipe it into a logging function that optionally writes it to a file if some flag is set

#

Also keep in mind it’s $null (2 l’s) and not $nul in PowerShell

#

I would recommend using the registry provider instead of reg.exe. You won’t have to start a new executable and have a richer object to work with

sweet dune
#

yeahhh. you'll just have to change your code. yeah like jordan says above, I guess you could just pipe everything to | Out-DebugLog -Enable:$DebugLog. I would just selectively use Write-Information and -InformationVariable

#

you just have to rethink all of it. i didn't realize batch could parameterize the whole redirection section as a %var%

#

The %nul% would just be | Out-DebugLog -Enable:$DebugLog though

#

You can further shorten it with $PSDefaultParameterValues['Out-DebugLog:Enable'] = $true if block at the top. Then you only need the |Out-DebugLog

#

i'd urge the dropping of native binaries where possible and using write-verbose, write-debug, write-information though

sweet dune
#

i see. yeah vscode debugging is nice. get-psbreakpoint and set-psbreakpoint are nice for debugging. can do it remotely too.

#

psframework module has cool logging capabilities too.

#

i do get the flexibility and immediacy with a straight log file though.

#

glad you got something working. 🙂

coral nebula
#

Hi, just been reading this post and well I would do something more like:

function Write-Log {
    param(
        [Parameter(Mandatory)]
        [string]$Message,

        [string]$Path = "$PSScriptRoot\script.log",

        [ValidateSet('INFO','WARN','ERROR')]
        [string]$Level = 'INFO'
    )

    # Ensure directory exists
    $dir = Split-Path $Path
    if (-not (Test-Path $dir)) {
        New-Item -ItemType Directory -Path $dir -Force | Out-Null
    }

    $timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
    $entry = "[$timestamp] [$Level] $Message"

    Add-Content -Path $Path -Value $entry
}
Write-Log -Message "Starting script $(Get-Date)"
Write-Host "Determining if to add required registry item"
if (Test-Path HKCU:\test)
{
    Write-Log "The required registry key already exists no further action."  
}
else
{
    Write-Log "About to add the item test to HKCU"
    try
    {
        New-Item -Path "HKCU:\test" -Verbose -ErrorAction Stop
    }
    catch
    {
        Write-Host -ForegroundColor Yellow "There was a problem please check the log file $($PSScriptRoot)\script.log"
        Write-Log "The script failed to add HCKU the issue was: $($Error[0].exception.message)" -Level ERROR
    }

}
Write-Host "Script completed"
sweet dune
#

won't you have to rewrite everything or at least copy all code in the if into the else block at bottom? I don't know how much you need start-transcript if you are replacing the %nul% in batch with |Out-DebugLog

sweet dune
sweet dune
#

well i would look at psframework. every time you call write-psfmessage it logs

#

but yeah it sounds like you have your vision. I didn't know you were wrapping the ps1 in more batch

#

but then you have two files...

#

employer context ? or are you making like gaming scripts or worse 😉

#

so its public but you can't share here? 😉

#

it would have been alot easy for advice to see the full vision

#

oh...yeah...worse....

coral nebula
sweet dune
#

And once you start writing to a log you have to consider purging it or doing ground hogs day on it

coral nebula
sweet dune
#

Unless it is a single run and then at that point you might as well use the native commands with redirection if someone wants them in a log.

#

Yeah the “write log” function is fair still. It lacks dependencies and can get the job done.