L
L
L0ns2021-11-02 10:00:56
PowerShell
L0ns, 2021-11-02 10:00:56

Why is the output of the powersheel script not working correctly?

Good afternoon.
Tell me what I'm doing wrong? There is a small script for granting access to network folders, it is divided into several points, one of them does not work correctly if you use this construction:

Write-Host ″Menu″ -BackgroundColor White -ForegroundColor Red
Write-Host ″1. Посмотреть права на папку″ -ForegroundColor Green
Write-Host ″2. Exit″ -ForegroundColor Green

while($true)
{
$choice = Read-Host ″Выберите пункт меню″

Switch($choice){

  1 {
        $path = "C:\Temp"
        (get-acl $path ).access | select IdentityReference, IsInherited, FileSystemRights
         
    }

  2 {Write-Host ″Exit″; return}
    default {Write-Host ″Wrong choice, try again.″ -ForegroundColor Red}
    }
}


Then I get a normal output only after the third time, first it outputs an empty string, then it outputs duplicated values, and the third time the output is normal.


″Menu″
″1. View folder permissions″
″2. Exit″
″Select Menu Item″: 1

″Select Menu Item″: 1
IdentityReference IsInherited FileSystemRights
----------------- ----------- --- -------------
BUILTIN\Administrators True FullControl
NT AUTHORITY\SYSTEM True FullControl
BUILTIN\Users True ReadAndExecute, Synchronize
NT AUTHORITY\Authenticated True Modify, Synchronize
NT AUTHORITY\Authenticated True -536805376
BUILTIN\ True FullControl Administrators
NT AUTHORITY\SYSTEM True FullControl
BUILTIN\Users True ReadAndExecute, Synchronize
NT AUTHORITY\Authenticated True Modify, Synchronize
NT AUTHORITY\Authenticated True -536805376
″Select Menu Item″: 1
BUILTIN\Administrators True FullControl
NT AUTHORITY\SYSTEM True FullControl
BUILTIN\ Users True ReadAndExecute, Synchronize
NT AUTHORITY\Tested True Modify, Synchronize
NT AUTHORITY\Tested True -536805376
″Select Menu Item″:


Can anyone suggest how to fix the situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2021-11-02
@L0ns

This is due to the peculiarities of how Output works.
The easiest way to decide in your case is to add | Out-Host after select
if you are doing interactive, then always use xxx-Host cmdlets,
mix default output with output to host - just get glitches like this
. And, yes, code formatting is creepy. Don't skimp on translations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question