Powershell updates Nagios Windows Client
Beans found on a newly configured Nagios server today that the Windows Server's memory check still uses check_nt, and check_nt checks the sum of physical memory and virtual memory; I only need to check the physical memory, so I need to change it to check_nrpe. The configuration on the Nagios server is not mentioned much. download and install the relevant plug-ins, configure command. cfg and windows. cfg, then the client modifies the corresponding NSClient configuration file, and the test passes.
Then the problem arises. I have more than 80 Windows servers that need to modify the corresponding configuration file, and more than 10 Windows servers do not have NSClient installed at all. I don't want to manually install the configuration one by one, A simple script was written for unified modification.
The basic idea is to get the Windows server name from AD and determine whether the server is online. Scan the online server to check whether the NSClient is installed. If yes, check whether the server is backed up. Otherwise, update the configuration file after the backup. If no server is installed, copy the installation file to the local temp folder, install it, and update the configuration file.
There are several points worth mentioning:
1. When invoke-command is used for remote operations, the default security mechanism will prohibit access to network resources! Therefore, I need to copy the MSI file to a local machine to install it.
2. When using msiexec in Powershell to install the msi file, you must use the start-process format, as shown in
start-Process-FilePathmsiexec.exe-ArgumentList"/ic:\temp\NSCP.msi/q"-Wait-PassThru
For remote use, note that the first option is not to access network resources, the second option is not to use interactive installation, and the third option is to create files related to user files, such as shortcuts; any violation will cause failure.
3. when copying an object, I used copy... | the format of out-null, which is the same as start-process copy.exe-wait. It is used to ensure that the next command is executed only after the current command is successfully completed.
4. this script is not well written. For example, I did not judge whether the operating system is 64-bit or 32-bit, nor did I handle exceptions or errors. I will complete it later, however, you can process what you need.
Write-Host"ScanningOnlineServers..."$a=get-adcomputer-filter{operatingsystem-like"*20*"}$computers=@()foreach($bin$a){if(Test-Connection-computername$b.name-Count1-Quiet){$temp=[psobject]@{'name'=$b.name}$computers+=$temp}}Write-Host"ScanningNagiosClients..."$c2=@()$computers|ForEach-Object{$path="\\"+$_.name+"\c$\ProgramFiles\NSClient++\nsclient.ini"$bakpath="\\"+$_.name+"\c$\ProgramFiles\NSClient++\nsclient.ini.bak"if((Test-Path-Path$path)-and!(Test-Path-Path$bakpath)){copy$path$bakpathcopy"\\sydav01\c`$\programfiles\NSClient++\nsclient.ini"$path#"Restartnscpserviceon"+$_.nameInvoke-Command-ComputerName$_.name{restart-servicenscp}}else{$path+"Folderdoesn'tEsixt"$temp=[psobject]@{'name'=$_.name}$c2+=$temp}}$end=$falsewhile($end-eq$false){Write-Host"Followingserversdon'thaveNagiosClientInstalled."$c2.name$option=read-host"DoyouwanttoInstall?(Y/N)"switch($option){"Y"{$c2|foreach-object{$path2="\\"+$_.name+"\c$\temp\NSCP.msi"if(Test-Path$path2){}else{New-Item$path2-Force}Write-host"CopyingNSCP.msifilesto"$path2copy'\\sydit01\c$\Temp\NSCP-0.4.4.15-x64.msi'$path2|Out-NullWrite-host"Copyingiscompletedandstarttoinstall"Invoke-Command-ComputerName$_.name-ScriptBlock{Start-Process-FilePathmsiexec.exe-ArgumentList"/ic:\temp\NSCP.msi/q"-Wait-PassThru}$path3="\\"+$_.name+"\c$\ProgramFiles\NSClient++\nsclient.ini"Write-host"Installationiscompletedandnowisupdtingconfigfile"copy"\\sydav01\c$\programfiles\NSClient++\nsclient.ini"$path3Invoke-Command-ComputerName$_.name{restart-servicenscp}}$end=$true;}"N"{$end=$true}default{"PleaseanswerYorN"}}}
Run the snippet, copy the file to a server, install, update the file, and restart the service.
The memory information is obtained successfully from the Nagios server.