I wrote the following script in which Servers.txt
file contains a list of servers:
$Result = @()
foreach ($server in (gc .\Servers.txt)) {
$computer = [ADSI](”WinNT://” + $server + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)
function getAdmins {
$members = ($Group.psbase.invoke(”Members”) | % { $_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null) }) -replace ('WinNT://DOMAIN/' + $server + '/'), '' -replace ('WinNT://DOMAIN/', 'DOMAIN\') -replace ('WinNT://', '')
$members
}
$Result += Write-Output "SERVER: $server"
$Result += Write-Output ' '
$Result += ( getAdmins )
$Result += Write-Output '____________________________'
$Result += Write-Output ' '
}
$Result > c:\results.txt
Invoke-Item c:\results.txt
If I run this script, I get the error:
PS C:> C:\LocalUsers.ps1 Get-Content : Cannot find path 'C:\Servers.txt' because it does not exist. At C:\LocalUsers.ps1:3 char:23
- foreach($server in (gc <<<< .\Servers.txt)){
- CategoryInfo : ObjectNotFound: (C:\Servers.txt:String) [Get-Content], ItemNotFoundException
- FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Why do I keep getting this error? How do I solve this?
Get-Location
should return c:\ but you probably expect something different. Use the full path to avoid this issue. - Matt