How to change powershell script from reading the input.txt file to querying local computer?

Hi,
Could you please help to modify this script so I can run it locally on the computer and it would just use local computer instead of needing the 'serversALL.csv' file. I am running this script locally.


Thank you so much!
 
$servers= get-content 'serversALL.csv'
$output = 'LocalAdminsBeforeMigratedServers.csv' 
$results = @()
foreach($server in $servers)
{
$admins = @()
$group = [ADSI]"WinNT://$server/Administrators" 
$members = @($group.psbase.Invoke("Members"))
$members | foreach {
$obj = new-object psobject -Property @{
 Server = $Server
 username = $_.GetType().InvokeMember('ADSPath', 'GetProperty', $Null, $_, $Null) -replace ('WinNT://', '') -replace('/', '\')
 }
 $admins += $obj
 } 
$results += $admins
}
$results| Export-csv $Output -NoTypeInformation
creative555Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subash SundharanIT Infrastructure Architect Commented:
Quick answer to your question is to change first line to
$servers = $env:computername

Open in new window

But we could make it lot simpler by removing the foreach loop and reducing the number of variables used..
Subash SundharanIT Infrastructure Architect Commented:
Modified code.. This will give the same result..
([ADSI]"WinNT://$($env:computername)/Administrators").psbase.Invoke("Members") | foreach {
  new-object psobject -Property @{
  Server = $env:computername
  username = $_.GetType().InvokeMember('ADSPath', 'GetProperty', $Null, $_, $Null) -replace ('WinNT://', '') -replace('/', '\')
  }
} | Export-csv LocalUser.csv -NoTypeInformation

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
creative555Author Commented:
Oh. excellent! THank you so much. I was wondering if it is possible to also add the command to script "set-executionpolicy unrestricted".....I have to manually run this command on each server because execution policy is disabled on all servers. Also, would you recommend putting a GPO for all servers to set-execution to unrestricted so I can run the scripts for migrations? Is it possible to even deploy GPO like this and then disable it after we migrate the servers?
Subash SundharanIT Infrastructure Architect Commented:
It's not recommended to set the execution policy as unrestricted. You could temporarily bypass the execution policy.

for example..

Powershell -executionpolicy bypass -file “C:\test\Script.ps1″
creative555Author Commented:
Thank you so much!!! You are excellent!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.