Script runs; no errors, I think I'm doing something wrong

I’m new to PowerShell, and I’ve been doing a lot of research. I’m using a Logging_functions library and a template I found on gist. From everything I have read the code looks correct(to my understanding, which isn’t a lot concerning PS), although I get no errors, and a log file does not manifest. The functions that I am calling from the Logging_functions library are all over place as I wasn’t sure where they needed to be and I tried to add the Log-Start and New-item lines in a few places and no matter where I put the lines in the code it wont create the log file. The Script is supposed to search the HKLM and HKCU registries and replace any strings that match a particular key word with a “0”

EDIT: Thank you guys for the input as I have refined the script, and removed a lot of stuff that was not necessary and I also removed the Logging_function altogether, as it did way more than I really needed, I replaced it with an Out-File write-output statement for what I need. What I have now is a script that runs without errors, and tells me that it is searching the two Registry Hives(HKCU and HKLM) for any type of string value named “skysail” and it is to change it to a “0” Well I’m not so sure my Set-ItemProperty statement is correct, as I’m not sure Dword is the right value type and I don’t know how to rephrase it to check for the string types that I need. When I look at on of the keys in a registry I need to “0” out it says it is a Default String.

#---------------------------------------------------------[Initialisations]--------------------------------------------------------
 
# Makes PowerShell pull you up about everything..
Set-StrictMode -Version Latest
# You don't need to put this in usually, but just to make sure in your ise.
$ErrorActionPreference = "SilentlyContinue"# Revert to "Continue" when modifying or adding to script
#Dot Source required Function Libraries -- not implemented yet

 
#----------------------------------------------------------[Declarations]----------------------------------------------------------
$logFile = "C:\PowerShell Script Log\reg_replace_log.txt" # Out-File -filepath $logFile

#counter for changes in reg
$numRegChangeHKCU = 0
$numRegChangeHKLM = 0
 
            
# Don't have this going to null.. hides the output from you
#$null = New-PSDrive -Name HKEY -PSProvider Registry -Root Registry: HKEY_CURRENT_USER -Root Registry: HKEY_LOCAL_MACHINE
 
 
#Enter the string value to search for in the variable below.
$SearchString = "skysail"
#==================================================================================
# Main Code for HKCU:
# Write a message to the user to let them know the script
# has started.
Write-Host "Searching: HKCU..."
Write-Output "Searching: HKCU..." | Out-File -filepath $logFile #without -Append this will overwrite output file on script start

# Registry Keys in $HKCUValues variable
 
# Take your error actions off - let it do the default which is to show you the error, but continue..
$HKCUValues = Get-ChildItem HKCU:\ -Recurse -ErrorAction SilentlyContinue # Revert to "Continue" when modifying or adding to script 

#For Each registry Key in Registry Directory
ForEach ($regkeyHKCU in $HKCUValues)
{ 
    if((get-itemproperty -Path $regkeyHKCU) -match $SearchString)
     {
        Set-ItemProperty -Path $regkeyHKCU -Value "0" -type Dword
        Write-Host "Changes in the registry for HKCU completed $regkeyHKCU"
        Write-Output "Changes in the registry for HKCU completed $regkeyHKCU" | Out-File -filepath $logFile -Append
        $numRegChangeHKCU++
     }
}



Write-Host "Searching: HKLM..."
Write-Output "Searching: HKLM..." | Out-File -filepath $logFile -Append

# Registry Keys in $HKLMValues variable
$HKLMValues = Get-ChildItem HKLM:\ -Recurse -ErrorAction SilentlyContinue # Revert to "Continue" when modifying or adding to script

#For each Registry Key in Registry Directory
ForEach($regkeyHKLM in $HKLMValues)
{
    if((Get-ItemProperty -Path $regkeyHKLM) -match $SearchString)
    {
        Set-ItemProperty -Path $regkeyHKLM -Value "0" -type Dword
        Write-Host "Changes in Registry for HKLM Completed on $regkeyHKLM"
        Write-Output "Changes in Registry for HKLM Completed on $regkeyHKLM" | Out-File -filepath $logFile -Append
        $numRegChangeHKLM++
    }
}
Write-Host "Number of registry changes in HKCU: $numRegChangeHKCU"
Write-Host "Number of registry changes in HKLM: $numRegChangeHKLM" 
Write-Output "Number of registry changes in HKCU: $numRegChangeHKCU" | Out-File -filepath $logFile -Append
Write-Output "Number of registry changes in HKLM: $numRegChangeHKLM" | Out-File -filepath $logFile -Append




# Write a message to let the user know the script completed. 
Write-Host "completed."
Write-Output "completed." | Out-File -filepath $logFile -Append


# == End of Main Code =======================================

So…

OK.

The LogFunction is physically in the right spot. A problem is that no information is being passed into it. You’re relying on variables declared elsewhere in the script, which is a poor practice, but it should work. It makes debugging a little harder.

I do see you dot-sourcing Logging_Functions.ps1. Dot-sourcing is kind of lame; ideally, that should probably be structured as a script module. But again, it should work - it’s just trailer trashy. Do these Log-* functions exist in there?

Frankly, I’m having some trouble following the logic here. I see your LogFunction function, but I don’t see where you actually run that function. It’s possible I’m just missing it, but if you don’t run the function… it isn’t going to do anything. I see “Script Execution goes here,” but you’re not actually executing anything. That’s just a comment.

I think you may need to just back off a little. You’ve got a lot going on in your script. If the goal is to figure out these logging functions, then it would make sense to write a very small testing script that does nothing but create log entries. Get that working. Then, once you know how it works, incorporate it into your registry-editing script.

This is where I found the Logging_functions library PowerShell: How to easily create log files for your scripts – 9to5IT

And also the template that I used. I will try to see if I can get the LogFunction working by itself(without my script) but I think I see part of the problem now is that I’m not really sure how to invoke the LogFunction properly, and I think that what I read from the above link when I started may have confused me a little bit. I don’t exactly know the priority of where the new-item is instanced, and from that perspective I’m not really sure where I need to invoke the functions from the library. Do I make several instances of the LogFunction or do I just need one? Does the Log file need to be created before the template script? or after declarations? And the ‘execution starts here’ was part of the original template that I did not remove, and the lines above and below that were both commented. Thank you for your advice Don.

Whoa. Slow down.

Can we maybe start with ONE question I could try and answer, and not 20?

To run the function you simply type the name…

function run-script { ‘this is my script block’}
run-script

That site crashed my machine so I won’t go there again but I also see several other functions not defined in the main. log-start, log-write, log-error? Seems the original poster didn’t include his custom modules.

I apologize about the website crashing your machine, and I have done away with that logging_function altogether, as Don is correct in saying that too much is going on for what I want to accomplish, I have edited the original post and updated the script. Thank you guys for your feedback, I have learned a lot over the past few days, and I’m now on PowerShell Day 3, so bear with me as I might not be familiar with some of the terms being thrown around.