Open RobertoMarcelino opened 3 years ago
Hi Roberto. Thanks so much for this. It's very interesting and useful! I have a couple of questions you might be able to clarify:
## Option: Hostname
# Hostname=
#Hostname=Windows host
I wonder if it would be possible to build an if / then into the script? It might be difficult, becahse my conf file still has "# Hostname". Hm something to think about...
I am using the agent version 5.2.5, frontend 5.2.5 as well and the script is version v5.x.
After removing the parentheses, it started to work on the Windows Server 2019 version. On the other tested systems, they worked with the parentheses.
Regarding variables, I am not a PowerShell expert. What I did was search Google for commands that would do what I needed.
Then I wondered if a question could not be added:
$token="Hostname"
$extractedValue = Get-Content $Senderarg3
$result = (($extractedValue -split [System.Environment]::NewLine) | where {$_ -Like "$token*"}).Substring("$token=".Length);
if $hostname = empty or nonexistent
$hostname = $env:computername
else
$hostname = $result
endif
Or something similar. You better than anyone else can evaluate and see if it is possible.
With this addition to the code, there will be no problem with your configuration file.
In my configuration file I use the following syntax:
HostMetadata=:osname=Windows:tipo=Workstation:
In Zabbix, I created an action to import and add to the Workstations group.
$file = Get-Content $Senderarg3
$file | foreach {
$items = $_.split("=")
if ($items[0] -eq "Hostname"){$result = $items[1]}
}
if (-not($result)){
$hostname = $env:computername
} else {
$hostname = $result.Trim()
}
I changed the location from where to download the .PS1 file, because when it comes from Github, it has different encoding and doesn't run.
I tried to change the command line but to no avail.
powershell.exe -NoProfile -ExecutionPolicy Bypass -command Invoke-WebRequest -Uri 'https://mysite/file.ps1' -OutFile 'C:\Users\zabbix\scripts\file.ps1' -UseBasicParsing
$token="Hostname" $extractedValue = Get-Content $Senderarg3 $result = (($extractedValue -split [System.Environment]::NewLine) | where {$_ -Like "$token*"}).Substring("$token=".Length);
if $hostname = empty or nonexistent $hostname = $env:computername else $hostname = $result endif
I'd have to test this. The problem is, the default config file actually has the hostname parameter, but it is hashed out. I'm not familiar enough with PS to know witout testing if your modification would still pick up hostname on the congif file...
I changed the location from where to download the .PS1 file, because when it comes from Github, it has different encoding and doesn't run.
I tried to change the command line but to no avail.
powershell.exe -NoProfile -ExecutionPolicy Bypass -command Invoke-WebRequest -Uri 'https://mysite/file.ps1' -OutFile 'C:\Users\zabbix\scripts\file.ps1' -UseBasicParsing
I'm reall ynot sure why it is not working for you... Mine all download the file from github and it runs fine. I'm a little stumped.
I tested the script on several operating systems and some worked, others didn't.
Systems tested: Windows Server 2008 R2 Standard = OK Windows Server 2008 R2 Enterprise = OK Windows Server 2012 Standard = OK Windows Server 2012 R2 Standard = OK Windows Server 2016 Standard = OK Windows Server 2016 Datacenter = OK Windows Server 2019 Standard = Not OK
Some are redundant: Standard, Enterprise, Datacenter. It is a way of showing that they were run on several servers with different operating systems.
On some servers it ran perfect. In others, I had to change the script.
On all Windows Server 2019 Standard, you presented the following error message:
After removing the parentheses on lines 216 and 240, the script ran perfectly:
Response from "server-zabbix.com:10051": "processed: 1; failed: 0; total: 1; seconds spent: 0.000064"
I provide IT services to several companies in different locations and there are machines with the same name (Example: SVR-APP) but which are configured in the zabbix_agentd.conf file as Hostname = SVR-APP-01, Hostname = SVR-APP-02 .
They do not work, because the hostname is not found on Zabbix hosts.
So, I added just after the variable $ Senderarg8 = '1' the following code snippet:
I changed the $env:computername variables to $hostname.
It worked!