Link to home
Start Free TrialLog in
Avatar of haz sh
haz sh

asked on

Ping to verify computer is on internal network to delete a GP file - Continued

Please assist.

In the $dc variable. If I have a single name in $DC variable the scripts works fine but if I have more than one name in the variable  $DC's  the script does not execute.

I want the $computers variable to ping $dc variable and if any one of the DCs is reachable execute the script.  Below is the script



$dc = Get-Content ".\dc.txt"
$computers = Get-Content ".\names.txt"
$Registrypol = "\\{0}\C$\Windows\System32\gp\*.txt"
$commentcmtx = "\\{0}\C$\Windows\System32\gp\mp\*.txt"
$Softwaredistribution = "\\{0}\C$\Windows\SoftwareDistributiontest"
$logfile = "\\HAZ-CM\temp\offline.txt"

Add-Content $logfile $(Get-Date)
Add-Content $logfile "Computer is Offline"
foreach ($computer in $computers) {
      Test-Connection -Source $computer -ComputerName $dc -Count 2 -ErrorAction SilentlyContinue
      If ($?) {
            Add-Content $logfile "$computer is Online"
            "$computer" >> $logfile
            Get-Service -Name wuauserv -ComputerName $computer| Stop-Service -Force
            $path = $Registrypol -f $computer
            Remove-Item -Path $path -Force
            $path = $commentcmtx -f $computer
            Remove-Item -Path $path -Force
            $path = $Softwaredistribution -f $computer
            If (Test-Path -Path $path) {
                  Remove-Item -Path $path -Recurse -Force
            }
            Get-Service -Name wuauserv -ComputerName $computer| Start-Service
            Invoke-WmiMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000021}" -ComputerName $computer
            Invoke-WmiMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000022}" -ComputerName $computer
            Invoke-WmiMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000113}" -ComputerName $computer
            Invoke-WmiMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000108}" -ComputerName $computer
      } Else {
            Add-Content $logfile $(Get-Date)
            Add-Content $logfile "$computer is Offline"
      }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oBdA
oBdA

Then "\\mypc\finalfix\dc.txt" is empty, or (more likely) it has empty lines in it.
Make sure there no empty lines (including after the last name), or use this to read the names:
$dc = Get-Content "\\mypc\finalfix\dc.txt" | Where-Object {$_}

Open in new window

You've gone back to removing the folders locally on the machine where the script is running, not on the target machines.
Remove-Item -Path "C:\Windows\System32\GroupPolicy\Machine\*.pol" will remove all .pol files on the machine running the script, whether wrapped in a "ForEach ($computer) ..." loop or not.

And if it works with IP addresses and not with names, you have problems in DNS, not with the script.
Avatar of haz sh

ASKER

Thank you oBdA ! The script is pushed from SCCM and runs locally, hence the above is the solution. Thank you for your time and support.