2
votes

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?

5
How and where (Directory) are you running this script? From the sounds of it Get-Location should return c:\ but you probably expect something different. Use the full path to avoid this issue. - Matt
Might not be an issue but be careful as you have smart quotes in your code there. Depending on how you save and run the script you can have issues. - Matt
Below is not working too: foreach($server in (gc -Path C:\Servers.txt)){ What should I do? - Nikhil Tamhankar
Hi, You didn't answer any of my questions I asked earlier. Where is the servers.txt file actually located? - Matt
File is stored in C: - Nikhil Tamhankar

5 Answers

3
votes

Go to ControlPanel/Appearanceand Personalization/FileExplore Options/View and uncheck "Hide extensions for known file types. Now check your *.txt file possibly you will see Computres.txt.txt Delete last .txt

2
votes

As per a comment your txt file is in the root of C: and that should be fine. Problem is nowhere does your script confirm the running location relative to that txt file. I imagine if you called Get-Location you would see the a different path and likely the one where the script is running from.

Either of the following options would be preferable.

  1. Move the script file and txt into the same directory.
  2. Use the absolute path when calling the path(s). foreach($server in (gc "c:\Servers.txt")){
1
votes

I was having a similar problem. What I was doing wrong was my folder options was set to hide extensions for known file types, resulting in my .txt file actually being a .txt.txt file.

0
votes

try to change ...(gc .\Servers.txt)... in ...(gc -Path .\Servers.txt)...

Look this: https://technet.microsoft.com/en-us/library/hh849787.aspx

0
votes

Either your Servers.txt file is in another location or its extension is different.

Use Get-Location to make sure you are in the correct path of the Servers.txt file. Use dir to see if you see the Servers.txt file in the directory your in.

It may be possible your Servers.txt file extension is Servers.txt.txt