I have a script to deploy monitoring agent to servers. This script is executed at startup by GPO.
Code: Select all
# Service Stop
Start-Transcript -Path C:\Windows\cmk.txt
#-------------------------------------------------
# Functions
#-------------------------------------------------
function CompareFileVersion {
Param(
[Parameter(Mandatory=$true)]
[String]$PrimaryFile,$SecondaryFile
)
Try {
$Primary = Get-FileHash $PrimaryFile -Algorithm SHA256 -ErrorAction Stop | Select Hash -ExpandProperty Hash
}
Catch {
Write-Host "Error: Cannot get primary file Hash!"
Exit
}
Try {
$Secondary = Get-FileHash $SecondaryFile -Algorithm SHA256 | select Hash -ExpandProperty Hash
}
Catch {
$Secondary = $null
}
If($Primary -eq $Secondary) {
Return $True
}
Else {
Return $False
}
}
#------------------------------------------------
# Stop agent
#------------------------------------------------
If(!(CompareFileVersion \\monitor\AGENT\CMK\check_mk_agent.exe C:\Windows\CMK\check_mk_agent.exe)){
Write-Verbose "Newer version of agent was found. Updating..." -Verbose
If (Get-Service -Name "check_mk_agent" -ErrorAction SilentlyContinue) {
If((get-service -Name "check_mk_agent").Status -ne "Stopped"){
stop-service -Name "check_mk_agent" -Force -Verbose
$loop = 0
While((get-service -Name "check_mk_agent").Status -ne "Stopped"){
Start-sleep -s 2
$loop++
If($loop -eq 10) {
Write-Verbose "Agent was not Stopped in time limit." -Verbose
Break
}
}
}
#------------------------------------------------
# Remove Agent
#------------------------------------------------
Write-Verbose "Removing Agent..." -Verbose
Start-Process C:\Windows\CMK\check_mk_agent.exe -ArgumentList remove -Verbose
$exist = $true
$loop = 0
While ($exist -eq $true){
If (!(Get-Service -Name "check_mk_agent" -ErrorAction 'Ignore')) {
$exist = $false
Write-Verbose "Removing service..." -Verbose
}
Start-Sleep -Seconds 2
$loop++
If($loop -eq 10) {
Write-Verbose "Agent was not removed in time limit." -Verbose
Break
}
}
}
#-------------------------------------------------
# Install agent
#-------------------------------------------------
Copy-Item "\\monitor\AGENT\CMK" "C:\Windows" -Recurse -Force -Filter * -Verbose
Start-Process C:\Windows\CMK\check_mk_agent.exe -ArgumentList install -Verbose
Write-Verbose "Instaling Agent..." -Verbose
$exist = $false
$loop = 0
While($exist -eq $false) {
If(Get-Service -Name "check_mk_agent" -ErrorAction Ignore) {
$exist = $true
Write-Verbose "Installing service..." -Verbose
}
Start-Sleep -Seconds 2
$loop++
If($loop -eq 10)
{
Write-Verbose "Agent was not installed in time limit." -Verbose
Break
}
}
#-------------------------------------------------
# Start Agent
#-------------------------------------------------
Write-Verbose "Staring Agent..." -Verbose
Start-Service -Name check_mk_agent -Verbose
$loop = 0
While ((get-service -Name "check_mk_agent").status -eq "Stopped") {
Start-Sleep -Seconds 2
$loop++
If($loop -eq 10)
{
Write-Verbose "Agent was not started in time limit." -Verbose
Break
}
}
Write-Verbose "Finish" -Verbose
}
Else{
Write-Verbose "Agent is up to date." -Verbose
}
Stop-Transcript
This works correctly.
I want to execute this script to update agent without restart server remotely. So I use:
Code: Select all
$SelectedItems = $lstServers1.SelectedItems
$session = new-pssession -computername $SelectedItems -Name "DeployCMK"
Invoke-command -session $session -scriptblock { Code Above }
This is also OK.
I wanna to get probgress bar that will indicate phase of deployment:
stop agent seciton 10%
copy file section 20%
remove section 30% etc.
But i dont know how, because of remote execution. I tryed to split up scrip to multiple parts. As it is in first post.
But problem is first part if I run script on multiple servers at time. In first part there is check if agent is up to date using filehash. If agent is up to date update will be skipped. If run script on multiple servers and one of them is up to date, it skip all of them.
This is hard to explain.
SOlution can be use of:
Code: Select all
foreach ($server in $SelectedItems)
{
$session = new-pssession -computername $SelectedItems -Name "DeployCMK"
Invoke-command -session $session -scriptblock { Code Above }
}
But I will aplly on 100 servers, it will spend a lot of time.
Jan