Sep 01 2022 02:42 PM
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 attempted this whit the code below but it is not exporting anything useful. Can someone tell me what I am doing wrong and what the proper code would be to get this info?
Thanks in advance!!
$computers = get-content .\computers.txt
$results = foreach ($computer in $computers){
invoke-command -computer $computer -scriptblock {$env:logonserver}}
$results | export-csv .\logonservers.csv
Sep 01 2022 11:00 PM
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
}
Sep 02 2022 05:05 AM
Sep 02 2022 06:16 AM - edited Sep 02 2022 06:17 AM
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