In the company I work for, we wanted to change a timer to an .ini file for around 310 computers, in order to extend this timer.
So I searched around and did find some scripts, but they were not doing exactly what we wanted.
So i sat down googled around and found the way, on how to make this work.
Below you can find the script I created.
In order to run the script what I did was:
- I created a group policy in Computer Configuration -> Policies -> Windows Settings -> Scripts (Startup / Shutdown) and then I clicked in Show Files... and placed the .ps1 file in there. (it should be something like \domain.local\SysVol\domain.local\Policies{907A196A-0859-4AFB-9BE7-6BD4BFA94265}\Machine\Scripts\Startup)
- I pressed Add, clicked on Browse and choose the .ps1 file.
Also in the same Policy I placed a small delay for the script to be executed for 4 minutes. Computer Configuration -> Administrative Templates -> System -> Group Policy --> Configure Logon Script Delay.
I also executed this script by using Lansweeper, which was easier and quicker to be run.
Try it, if you like.
Change it, if you will.
Correct what ever you find wrong, so we can all be helped.
(Just please, write it down, what needs to be changed, so we can all benefit from it!)
Below you can find the script.
Just be carefull when copying it.
The symbols needs to be re-checked.
(Copying-pasting from web pages, breaks some symbols)
$FilePath = "" #Within the quotation marks, you need to place, the path and name of the file, you would like to search and change e.g c:\Test\file.ini
$Search_entry_existing = "" # Within the quotation marks, you need to place, what you are looking for
$Search_entry_change = "" # Within the quotation marks, you need to place, what you want to change in the $Search_entry_existing with the $Search_entry_change
Test-Path -Path $FilePath -PathType Leaf # Checks if the file exists
Select-String -Path $FilePath -Pattern "" # Within the quotation marks, selects the pattern you wish to search for
If (Test-Path $FilePath) {
$Check = Select-String -Path $FilePath -Pattern "" # Within the quotation marks, type the pattern you wish to search for
If ((Get-Content $FilePath) -notcontains 'TimerC=120') { # Within the apostrophes, type the pattern you wish to search, in order to search for something withing the file, that does NOT exist like that TimerC=120 I placed
cls
echo "File Exists"
echo "************************************************************"
echo "*** The File $FilePath contains the string $Check ***"
echo "*** Changing the setting to TimerC to 120 seconds" # Yes I was playing with a timer. The name of what i was trying to change started with TimerC=75 and I wanted to change it to 120 seconds.
(Get-Content $FilePath) -replace '(TimerC)(.*)', '$1=120' | Set-Content $FilePath # Replaces of the string we would like to change and saves the file.`
}
else {
# If the $Search_entry_change is what we wanted already, then no change will be done.
cls
echo " The File $FilePath contains the string $Search_entry_change ***"
echo "*** No Need to Change. TimerC already 120 seconds ***"
}
}
else {
# In case the script does not finds the file in the path we gave earlier in the $FilePath then this means the file does not exists.
Write-Host "File Doesn't Exists. So the Program is not installed !!!"
}