Hello POSH fam
Just wondering if I could get some clarification on why a file will not move using function Move-Item.
Premise of the script is to execute commands to modify a system setting and create/save a log of the script run using Out-File to create a logging of the script run.
I have multiple functions as you can see
The goal of the script is accomplished thanks to the assistance provided in my previous post > https://powershell.org/forums/topic/search-string-in-powercfg-result/
I am not able to output/save my log file unto the desired location in “C:\Temp” when executing the script, however I am able to run the cmdlets in a Powershell session, just not when I try to execute the .ps1 file.
Additionally, As I am fairly new to POSH, for Powershell proper “etiquette” and/or best-practices, does creating multiple functions the way to go or …some other way.
My apologies for the length of the script in advance.
Thanks,
Alex
<# .SYNOPSIS Script to locate Disable_PC_Lock and remove .DESCRIPTION .PARAMETER .EXAMPLE #> # Function to check and/or create folder to store logs function Test-FolderExist { $Folder=Test-Path -Path 'c:\Temp\' if ($Folder -eq $false) { New-Item -Path 'c:\Temp\' -ItemType 'directory' Check-PowerPlan } else { exit } } #Change PowerScheme/Plan function ChangeActivePowerPlan { "Changing Active Power Plan to Balanced.....................................`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append "> Locate additional power plan" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append $NotActivePlan= Get-WmiObject -Class win32_PowerPlan -Namespace root\cimv2\power -Filter "ElementName = 'Balanced'" $NotActiveGuid= $NotActivePlan.InstanceID.split('{')[1] $SetActivePlanGuid= $NotActiveGuid -replace '}',"" "> Power Plan $($NotActivePlan.ElementName) with Guid $SetActivePlanGuid is non-active" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append #Set as active power plan powercfg /setactive "$SetActivePlanGuid" "> Power Plan $($NotActivePlan.ElementName) with Guid $SetActivePlanGuid has been set to active`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append "Active Power Plan changed......................................`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append RemovePowerPlan } # Remove Disable_PC_Lock function RemovePowerPlan { "Removing Power Plan $($PowerScheme.ElementName)................`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append "> Removing Disable_PC_Lock Power Plan from $env:COMPUTERNAME" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append $RemoveDisablePCLock= Get-WmiObject -Class win32_PowerPlan -Namespace root\cimv2\power -Filter "ElementName = 'Disable_PC_Lock'" $RemovePlan= $RemoveDisablePCLock.InstanceID.split('{')[1] $RemovePlanGuid= $RemovePlan -replace '}',"" #Remove power plan powercfg /delete "$RemovePlanGuid" "> Power Plan $($RemoveDisablePCLock.ElementName) with Guid $RemovePlanGuid has been removed`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append sleep -Milliseconds 300 "List of all Power Plans present on $($env:COMPUTERNAME) after removal of (Disable_PC_Lock) power plan.....`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append Get-WmiObject -Class win32_PowerPlan -Namespace root\cimv2\power | select ElementName,InstanceID,IsActive | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append "Logging Process stopped......................................`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append ShowScriptFooter | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append Copy } # Footer function ShowScriptFooter { $FooterPurpose='Script Purpose: Locate and Remove Power Plan Disable_PC_Lock' $FooterExecutionDetails='Script Execution: Executes at device startup via GPO' $Team='Responsible Team: IT Collaboration Operations' $Contact='For all inquiries please contact the Helpdesk' $Date="Script ran on $((Get-Date).ToString())" $message = @" =================================================================================================== $FooterPurpose $FooterExecutionDetails $Team =================================================================================================== $Contact $Date "@ $message } function Copy-File { $file = "c:\REMOVAL_LOG.$env:COMPUTERNAME.log" #Copy-Item $file -Destination c:\Temp -Verbose Move-Item $file -Force } # Main Function function Check-PowerPlan { $UniqueDate = Get-Date -UFormat %Y%m%d_%H%M%S "Logging process started........................................`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append "List of all Power Plans present on $($env:COMPUTERNAME)`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append Get-WmiObject -Class win32_PowerPlan -Namespace root\cimv2\power | select ElementName,InstanceID,IsActive | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append $PowerScheme=Get-WmiObject -Class win32_PowerPlan -Namespace root\cimv2\power -Filter "ElementName = 'Disable_PC_Lock'" if ($PowerScheme -eq $null) { #"Logging process started........................................`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append "> WARNING MESSAGE: Power plan 'Disable_PC_Lock' was not found on $($env:COMPUTERNAME)`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append "Script stopped...............`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append ShowScriptFooter | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append exit } else { Switch ($PowerScheme.IsActive) { 'True' { "> $($PowerScheme.ElementName) is the active plan and must be changed before continuing`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append ChangeActivePowerPlan } 'False'{ "> $($PowerScheme.ElementName) is not the active plan`r`n> Ok to remove power plan`r`n" | Out-File "c:\$Temp\REMOVAL_LOG.$env:COMPUTERNAME.log" -Append RemovePowerPlan } } } } # Call function to start script execution Test-FolderExist