D
D
Digsecman2021-08-17 16:52:32
PowerShell
Digsecman, 2021-08-17 16:52:32

The domain PC accessibility script does not work. Why?

I'm trying to get a list of all PCs saved in csv by adding the "installed" flag field, the script should go through the lines in the file in search of "not installed" and "available" and if the computer is online then install if not then ping and as soon as it is online then install by.
in addition, it checks whether there is a fusion folder, if not, then the contents are copied from the network spheres of the domain,
please help someone who understands the syntax to correct it so that it works.

$dest="\\$all\C$\fusion"
$sourcefile = "\\WIN\c$\fusion\*"
$all=Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(!userAccountControl:1.2.840.113556.1.4.803:=8192))" -Properties name | Sort).name|Import-csv C:\Users\user\Desktop\list.csv|
foreach ($comp in $all) {
  if (-not $comp.installed -and(test-connection $comp.name) -and(-not (test-path "C:\fusion\*"))) { 
if (!(Test-Path -path $dest))
    {
        New-Item $dest -Type Directory
    }
    Copy-Item -Path $sourcefile -Destination $dest;
     {Invoke-command -computername $comp.name -ScriptBlock –ThrottleLimit 120 {powershell "& 'C:\fusion\fusion.bat'"} 
     }
     $comp.installed=$true
  }
}
$all | export-csv C:\Users\user\Desktop\installed.csv

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Morrowind, 2021-08-17
@Digsecman

Hello.
Please understand me correctly. Your script is bad. Such an implementation takes place, but the simpler and more obvious you write, the better. (my personal opinion). Yes, and apparently you yourself are confused in it.
The $all variable receives data from AD by filter and imports something else. - It's ok, you know better what you want to find.
But here is the error in brackets 4 lines , probably in the highlighted place:
$all=Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(!userAccountControl:1.2.840.113556.1.4.803:=8192))" -Properties (name | Sort) .name|Import-csv C:\Users\user\Desktop\list.csv
Just note that you are doing a foreach pipeline next. Why is there a pipeline if the data is in a variable.
The foreach itself only contains if.
You can do this, but do not be surprised that it will crash even more actively. Most likely in the Test-Path -path $dest branch and beyond. You don't have to catch bugs and deal with them. Use try-catch.
At least the Host may not answer you for Test-Path , and there are also a lot of errors when copying ( is there a destination folder on this host where you are copying the batch file? This is also worth checking ) or Invoke-command . MaxKozlov will not let you lie, you can catch a lot of shit on these cmdlets.
Well, the ending "$all | export-csv C:\Users\user\Desktop\installed.csv" - it's up to you how you will track which machines the script took off on which not, but I recommend getting confused in logging.
Outcome: Rewrite the script from scratch. You have a complex filtering in which you yourself are apparently confused. Break it down into 2-3 steps into separate variables. It's better than sitting for hours and not understanding anything.
Make at least full-fledged if else, because mistakes are inevitable.

G
GaryManshow, 2021-08-30
@GaryManshow

Hello, I usually use ADSISearch instead of Get-ADComputer
if there are many computers use parallel processing:

$credential = Get-Credential DOMAIN\user
$Domain = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList $(([adsisearcher]"").SearchRoot.Path), ($credential.UserName), $($credential.GetNetworkCredential().Password)
# поиск ADSI
$Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher
$Searcher.SearchRoot = $Domain
$Searcher.Filter = "(&(objectCategory=computer)(operatingSystem=Windows*)(!(operatingSystem=*server*))(!(userAccountControl:1.2.840.113556.1.4.803:=8192)))"
$out = $Searcher.FindAll().Properties.name | ForEach-Object {
  # пингуем
  Test-Connection -ComputerName $_ -Count 1 -AsJob  -ThrottleLimit 128
} | Get-Job | Receive-Job -Wait | Where-Object { $_.StatusCode -eq 0 } | Select-Object Address | ForEach-Object {
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question