X
X
x4IPx2021-11-11 15:13:49
PowerShell
x4IPx, 2021-11-11 15:13:49

The script doesn't work. How to run a script without waiting for it to complete?

Hello everyone)

It is required to write a script that, in case of a change in the configuration file, wrote LOG. At the same time, it must be executed by a separate process without waiting for the end of the cycle.

$ScriptBlock = {

while($true){

$microsipini="$Env:USERPROFILE\AppData\Roaming\MicroSIP\microsip.ini"                              #В каком файле нужно проверять стоку
$namelog=$env:UserName                                                                             #Название файла
$folderlog='\\fs01\LOG\'                                                                 #Куда сохранять лог 
$datatime=Get-Date                                                                                 #Переменная дата/время
$ideallineini = 'autoAnswer='                                                                      #Эталонное значение
$textp='Строка изменена:'                                                                          #Текст в логе
$lineini = Get-Content -Path $microsipini | Select-String -Pattern 'autoAnswer='                   #Присвоение переменной значение определённой строки (Например autoAnswer=)
if( $lineini -notlike	 $ideallineini ) {                                                         #Сравнения эталонного значения с строкой в файле 
   Write-Output $textp$datatime','$env:UserName | Out-File $folderlog$namelog'.log' -Append        #Запись лога в случае несовпадения(-notlike)/совпадения(-like) строк

 
}
  Start-Sleep 3                                                                                          #Таймер цикла.Как часто команда будет обрабатываться.       
                                                                          
  }


}

Start-Job -ScriptBlock $ScriptBlock


If you run it from ISE, then the script works as it should.
But it is not processed if run through powershell.exe :
powershell C:\microsip.ps1
or group policy.

I can't understand the reason. Please help.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
MaxKozlov, 2021-11-11
@x4IPx

It's not very clear what you mean by "doesn't work"
but in any case, you do a Start-Job and this is where your script ends
and Wait-Job is nowhere to be found
In a separate script, you don't need a job
update at all:
And if, in essence, the task , you need to look towards FileSystemWatcher
https://powershell.one/tricks/filesystem/filesyste...
https://devblogs.microsoft.com/powershell-communit...
https://stackoverflow.com/questions/29066742 /watch...
instead of loading the computer with unnecessary work on cyclic reading of files

A
Alexander, 2015-02-24
@mbhusty

a lot of coding if you do it yourself, but there is a script directly from that page Script
copy the DOM menu structure from the site using your link, connect the script indicated above, write

$(document).ready(function () {
        ko.applyBindings(wikiNavViewModel, $('[data-id="wiki"]')[0]);
        nav_slide.init();
        wikiNavViewModel.show_menu(true);
        $('[data-id="wiki"]').css('visibility', 'visible');
    });

well, then add

I
Ilya Korablev, 2015-02-24
@swipeshot

Implement with your own hands. Easier than a steamed turnip.

D
des1roer, 2015-02-24
@des1roer

svanguy what is written on asp.net?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question