简体   繁体   中英

Getting Output of ADSI Command in Powershell

I'm trying to delete some local administrator users on multiple computers. Here is the script I used and it works:

Import-Csv c:/input.csv | ForEach-Object {
    ([ADSI]"WinNT://$($_.ComputerName)").Invoke("Delete", "user", $_.Username)
}

The problem is that I want to get the results of the non-deleted users directly to a text file. What commands would help me? I've tried Out-File , but it's giving me an empty file.

Use Get-WmiObject to query a remote host for local users:

$csv = Import-Csv 'C:\input.csv'
$lcl = 'LocalAccount=True'

$csv | ForEach-Object { [ADSI]... }

$csv | Sort-Object -Unique ComputerName | ForEach-Object {
  Get-WmiObject -Computer $_.ComputerName -Class Win32_UserAccount -Filter $lcl
} | Select-Object PSComputerName, Name | Export-Csv 'C:\output.csv' -NoType

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM