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
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"
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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\Group Policy\Mac hine\*.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.
Remove-Item -Path "C:\Windows\System32\Group
And if it works with IP addresses and not with names, you have problems in DNS, not with the script.
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.
Make sure there no empty lines (including after the last name), or use this to read the names:
Open in new window