忽略输出-它不应在屏幕上打印

时间:2019-05-20 07:43:44

标签: powershell

我可以为特定的用户或组添加LogOnAsAService特权。运行该功能时,它显示输出。

我的要求是它不应打印输出。

我具有以下作为登录服务权的工作功能。

#region LogOnAsService-Right
Function LogOnAsService-Right
{
param(
[string] $Servername = ("{0}.{1}" -f $env:COMPUTERNAME.ToLower(), $env:USERDNSDOMAIN.ToLower()),
[string] $username = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME)
)
try{
Invoke-Command -ComputerName $Servername -Script {
param([string] $username)
$tempPath = [System.IO.Path]::GetTempPath()
$import = Join-Path -Path $tempPath -ChildPath "import.inf"
if(Test-Path $import) { Remove-Item -Path $import -Force }
$export = Join-Path -Path $tempPath -ChildPath "export.inf"
if(Test-Path $export) { Remove-Item -Path $export -Force }
$secedt = Join-Path -Path $tempPath -ChildPath "secedt.sdb"
if(Test-Path $secedt) { Remove-Item -Path $secedt -Force }
try {
Write-Host ("Granting SeServiceLogonRight to user account: {0} on host: {1}." -f $username, $Servername)
$sid = ((New-Object System.Security.Principal.NTAccount($username)).Translate([System.Security.Principal.SecurityIdentifier])).Value
secedit /export /cfg $export
$sids = (Select-String $export -Pattern "SeServiceLogonRight").Line
foreach ($line in @("[Unicode]", "Unicode=yes", "[System Access]", "[Event Audit]", "[Registry Values]", "[Version]", "signature=`"`$CHICAGO$`"", "Revision=1", "[Profile Description]", "Description=GrantLogOnAsAService security template", "[Privilege Rights]", "$sids,*$sid")){
Add-Content $import $line
}
secedit /import /db $secedt /cfg $import
secedit /configure /db $secedt
gpupdate /force
Remove-Item -Path $import -Force
Remove-Item -Path $export -Force
Remove-Item -Path $secedt -Force
} catch {
Write-Host ("Failed to grant SeServiceLogonRight to user account: {0} on host: {1}." -f $username, $Servername)
$error[0]
}
} -ArgumentList $username

}
catch
{
$_.exception.message
}
}
#endregion

预期结果:不应在屏幕上打印所有步骤。

实际结果-它正在打印每个步骤:

The task has completed successfully.
See log %windir%\security\logs\scesrv.log for detail info.
Completed 1 percent (0/63)  Process Privilege Rights area        
Completed 3 percent (1/63)  Process Privilege Rights area        
Completed 4 percent (2/63)  Process Privilege Rights area        
Completed 6 percent (3/63)  Process Privilege Rights area        
Completed 7 percent (4/63)  Process Privilege Rights area        
Completed 9 percent (5/63)  Process Privilege Rights area        
Completed 11 percent (6/63)     Process Privilege Rights area        
Completed 12 percent (7/63)     Process Privilege Rights area        
Completed 14 percent (8/63)     Process Privilege Rights area        
Completed 15 percent (9/63)     Process Privilege Rights area        
Completed 17 percent (10/63)    Process Privilege Rights area        
Completed 19 percent (11/63)    Process Privilege Rights area        
Completed 20 percent (12/63)    Process Privilege Rights area        
Completed 22 percent (13/63)    Process Privilege Rights area        
Completed 23 percent (14/63)    Process Privilege Rights area        
Completed 25 percent (15/63)    Process Privilege Rights area        
Completed 25 percent (15/63)    Process Group Membership area        
Completed 49 percent (30/63)    Process Group Membership area        
Completed 49 percent (30/63)    Process Registry Keys area        
Completed 49 percent (30/63)    Process File Security area        
Completed 49 percent (30/63)    Process Services area        
Completed 65 percent (40/63)    Process Services area        
Completed 73 percent (45/63)    Process Services area        
Completed 73 percent (45/63)    Process Security Policy area        
Completed 77 percent (48/63)    Process Security Policy area        
Completed 84 percent (52/63)    Process Security Policy area        
Completed 88 percent (55/63)    Process Security Policy area        
Completed 93 percent (58/63)    Process Security Policy area        
Completed 100 percent (63/63)   Process Security Policy area

The task has completed successfully.
See log %windir%\security\logs\scesrv.log for detail info.
Updating policy...

Computer Policy update has completed successfully.

User Policy update has completed successfully.

2 个答案:

答案 0 :(得分:2)

您可以使用

  

|空空

即使正在打印所有行也不设置任何输出

答案 1 :(得分:1)

输出来自第二个secedit

Secedit有一个称为/quiet的可选参数,它禁止显示屏幕和日志。您仍然可以使用Microsoft管理控制台(MMC)的“安全配置和分析”管理单元来查看分析结果。

like:

Secedit /configure /db <database file name> [/cfg <configuration file name>] [/overwrite] [/areas SECURITYPOLICY | GROUP_MGMT | USER_RIGHTS | REGKEYS | FILESTORE | SERVICES] [/log <log file name>] [/quiet]

您的情况:

secedit /configure /db $secedt /quiet

或者强行地,您可以使PS使用Out-Null来执行该操作,或者将其重定向到$Null

secedit /configure /db $secedt | Out-Null

secedit /configure /db $secedt > $null 

所有方法都应该起作用。

希望有帮助。