Forum Discussion
charlie4872
Sep 01, 2022Brass Contributor
Get Logon Server From List Of Computers
Hello I am trying to find the logon server $env:logonserver from a list of computers in a .txt file and have the results exported out to a .csv file for a domain migration project I am working on. I...
farismalaeb
Sep 02, 2022Steel Contributor
You dont need to use the variable before the foreach.
Use this.
$computers = get-content C:\MyFile.txt
foreach ($computer in $computers){
$results=invoke-command -computer $computer -scriptblock {$env:logonserver}
Add-Content C:\MyResult.txt -Value $results
}
- charlie4872Sep 02, 2022Brass ContributorThanks for the reply Farismalaeb. I mad the changes you suggested and it returned no results. The .txt file is empty. My goal is to have it output a .csv file with two columns.
Computername Logonserver
For each of the computers in the computers.txt file.- LainRobertsonSep 02, 2022Silver Contributor
Taking a quick look, the LogonServer environment variable only seems to be set for Interactive (logonType = 2, or 10 in the case of RDP for the RemoteTerminal logonType.)
Running remote commands will use the Network logon (logonType = 3), and not all logon processes are executed in this scenario. This impacts the session environment variables created, with some like LogonServer not being created.
If you run the following command, you'll find that LogonServer isn't listed.
Invoke-Command -ComputerName somecomputer.yourdomain.com -ScriptBlock { $ev = [System.Environment]::GetEnvironmentVariables(); $ev.Keys | ForEach-Object { [PSCustomObject] @{ Variable = $_; Value = $ev[$_]; } } } | Sort-Object -Property Variable | Format-Table -AutoSize PSComputerName, Variable, Value;
Cheers,
Lain