To replace the nsclient.ini file content on the remote server and restart the nscp service.
clear
ForEach ($system in Get-Content "C:\system.txt")
{
Write-Host $system
get-childItem "\\$system\c$\Program Files\NSClient++\nsclient.ini" -recurse | ForEach {
(Get-Content $_ | ForEach {$_ -replace 'allow arguments = false', 'allow arguments = true'}) | Set-Content $_
}
get-childItem "\\$system\c$\Program Files\NSClient++\nsclient.ini" -recurse | ForEach {
(Get-Content $_ | ForEach {$_ -replace 'allow nasty characters = false', 'allow nasty characters = true'}) | Set-Content $_
}
#Change this values to suit your needs:
$SvcName = 'nscp'
$SvrName = $system
#Initialize variables:
[string]$WaitForIt = ""
[string]$Verb = ""
[string]$Result = "FAILED"
$svc = (get-service -computername $SvrName -name $SvcName)
Write-host "$SvcName on $SvrName is $($svc.status)"
Switch ($svc.status) {
'Stopped' {
Write-host "Starting $SvcName..."
$Verb = "start"
$WaitForIt = 'Running'
$svc.Start()}
'Running' {
Write-host "Stopping $SvcName..."
$Verb = "stop"
$WaitForIt = 'Stopped'
$svc.Stop()}
Default {
Write-host "$SvcName is $($svc.status). Taking no action."}
}
if ($WaitForIt -ne "") {
Try { # For some reason, we cannot use -ErrorAction after the next statement:
$svc.WaitForStatus($WaitForIt,'00:00:10')
} Catch {
Write-host "After waiting for 10 Seconds, $SvcName failed to $Verb."
}
$svc = (get-service -computername $SvrName -name $SvcName)
if ($svc.status -eq $WaitForIt) {$Result = 'SUCCESS'}
Write-host "$Result`: $SvcName on $SvrName is $($svc.status)"
}
$svc.Start()
$WaitForIt = 'Running'
if ($WaitForIt -ne "") {
Try { # For some reason, we cannot use -ErrorAction after the next statement:
$svc.WaitForStatus($WaitForIt,'00:00:10')
} Catch {
Write-host "After waiting for 10 Seconds, $SvcName failed to $Verb."
}
$svc = (get-service -computername $SvrName -name $SvcName)
if ($svc.status -eq $WaitForIt) {$Result = 'SUCCESS'}
else { $Result = 'UNSUCCESSFUL'}
Write-host "$Result`: $SvcName on $SvrName is $($svc.status)"
}
#Invoke-Command -ComputerName $system {Restart-Service nscp}
Write-Host "----------------------------------------------"
}
# c:\system.txt should contain the server names in every newline.
# The Invoke-Command requires WinRM service to be running to restart the remote service and it need to be configured as well, using "winrm quickconfig"
No comments:
Post a Comment