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 =======================================