Powershell вывод файла журнала - PullRequest
0 голосов
/ 04 октября 2018

Здравствуйте, я новый сценарий, мне поручено создать сценарий, который будет удалять файл из определенных папок, который будет развернут через SCCM на многих компьютерах.

Проблема в том, что я хочу вывести файл журнала со словами:

"<ComputerName> File Terminated"
"<ComputerName> File Not terminated"

Вот что у меня есть:

$users = "C:\Users"
$logFile = "C:\TTS\Logs\GoogleTerm.log"
$source = "\AppData\Roaming\Google"
$exclude = "Default"

$input = get-ChildItem -Path $users  

foreach ($folder in $input) {
    if ($exclude -notcontains $folder.Name) {
        Remove-Item -Path ($Users + "\" + $folder.Name + $source) -recurse -force 
    } 
}

echo "Google Terminated on:"$env:computername >> $logfile

Ответы [ 2 ]

0 голосов
/ 04 октября 2018

Это было мое успешное решение, спасибо вам, ребята, за помощь!

$users =  "C:\Users"

$logFile = "C:\TTS\Logs\GoogleTerm.log"


$exclude = "Default"



$input = get-ChildItem -Path $users 



foreach ($folder in $input) {
if ($exclude -notcontains $folder.Name) {
    if (Test-Path ($Users + "\" + $folder.Name + $source)){
    Remove-Item -Path ($Users + "\" + $folder.Name + $source) -recurse -force

    Add-Content $logFile "Google Terminated on:        $env:computername      UserName:$folder  "
   }
   else{
    Add-Content $logFile "Google Already Terminated on:$env:computername      UserName:$folder  "
   }
}
   else{
        Add-Content $logFile "Not Terminated on:           $env:computername      Username:$folder"
   }
}
0 голосов
/ 04 октября 2018

Добавлен else к вашему if для обработки обоих результатов, а затем используется Add-Content вместо echo:

foreach ($folder in $input) {
    if ($exclude -notcontains $folder.Name) {
        Remove-Item -Path ($Users + "\" + $folder.Name + $source) -recurse -force 
        Add-Content $logfile "$env:computername File Terminated"
    } 
    else {
        Add-Content $logfile "$env:computername File Not Terminated"
    }
}

Add-Content $logfile "Google Terminated on: $env:computername"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...