I am trying to true up out systems as we have a discrepancy from AD and our inventory management tool. I need to attempt to remote to a ton of PCs (~500). I am trying to build a GUI/script that will remote to a computer if there is no user logged in. I can do this one by one, but being as there are a ton of PCs that are in AD, but do not exist it takes a long time for each one to fail. I want to try and start a job for all of the computers in a list but only launch mstsc
if there is no user logged in.
#Clear previous jobs
Get-Job | Remove-Job
$computers = $richtextbox1.Text.Split("`n") | % { $_.trim() }
foreach ($computer in $computers) {
if ($computer -ne "") {
Start-Job -ScriptBlock {
if (Test-Connection -ComputerName $args[0] -Count 1) {
$uname = ""
$uname = Get-WmiObject -Class Win32_Computersystem -ComputerName $args[0] |
Select-Object -Expand UserName
if ($uname -like "") {
C:\Windows\System32\mstsc.exe /v:$args /h:768 /w:1024
# At this point the mstsc box would pop up, and the job
# can be killed if possible
}
}
} -ArgumentList $computer
}
}
If I remove if ($uname -like "")
it will start mstsc
. Another issue is when I do this to a block of 60 computers it will start mstsc
for ~6 computers, wait about 45 seconds then open a couple more windows (only about 10 of the computers existed). So I think there might be an issue with jobs being running and taking too long? What would be the best way to cancel a job if it is taking too long to execute?
I'm not sure if the issue is related to $args
/ $args[0]
. When working with jobs in the past I had to use $args[0]
. I also know that this can produce inconsistent results with getting the logged on user with Windows 10, so if there is a better option there, that would be helpful.
I ended up using this check to see if a user was logged in, and it worked.
$NOTNULLIFLOCKED = $null
$NOTNULLIFLOCKED = gwmi -Class win32_process -computername $comp -Filter "Name='LogonUI.exe'"
if ($NOTNULLIFLOCKED -ne $null)
{
# No Active Session (LogonUI.exe Running)
# If LogonUI.exe is running, that means the login box is currently present on the screen.
& C:\Windows\System32\mstsc.exe "/v:$comp" "/h:768" "/w:1024"
}
I am running into another issue now, but I will post another question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.