Using an XML for configuration to work with this script

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 1 year, 6 months ago.

  • Author
    Posts
  • #27605
    Profile photo of Mel VW
    Mel VW
    Participant

    Hi, I am constantly getting an error about the RPC server unavailable. Can you tell me what is wrong with the code on either the XML document or the ps1 script?

    Xml Document:

    '12.10.10.4'
    "DeviceID='E:'"
    "backup_server_1"

    '12.10.10.5'
    "DeviceID='E:'"
    "backup_server_2"

    Script (I will change it to objects at some point but for now just to get it working is key for me)
    `
    [xml]$configFile = Get-Content "C:\_x\config.xml"

    $ConvertToGB = (1024 * 1024 * 1024)

    "Server" + "," + "DriveLetter" + "," + "SitesHosted" + "," + "Size" + "," + "Free"

    foreach($config in $configFile.config.Server)
    {
    $disk = Get-WmiObject Win32_LogicalDisk -ComputerName $config.ip_address -Filter $config.partition | Select-Object Size,FreeSpace
    $config.ip_address + $config.partition + $config.host_example + ([math]::round(($disk.Size / $ConvertToGB),2)) + "GB" + "," + ([math]::round((($disk.FreeSpace / $ConvertToGB)),2)) + "GB"
    }
    `

  • #27606
    Profile photo of Mel VW
    Mel VW
    Participant

    having trouble pasting my xml document...
    but xml is like the following

    config
    server
    within server
    there is
    ip_address
    partition
    and hostname which is a description

  • #27607
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    RPC server unavailable means that WMI can't make contact with the remote server. Either because it doesn't recognise what you are trying to pass as the computername/IP address or because DCOM is disabled on the remote machine (check the Windows firewall – it blocks DCOM by default)

    I'd use a CSV file rather than XML for this as you can read the CSV file and each row becomes an object

    Your script is needlessly complicated – see the comments on your previous post

  • #27608
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You also need to add error handling in your script. If a computer is offline or WMI can't connect, you should account for that:

    foreach($config in $configFile.config.Server) {
        if (Test-Connection -ComputerName $config.ip_address -Count 2 -Quiet) {
            try {
                $disk = Get-WmiObject Win32_LogicalDisk -ComputerName $config.ip_address -Filter $config.partition -ErrorAction Stop | Select-Object Size,FreeSpace
                $config.ip_address + $config.partition + $config.host_example + ([math]::round(($disk.Size / $ConvertToGB),2)) + "GB" + "," + ([math]::round((($disk.FreeSpace / $ConvertToGB)),2)) + "GB"
            }
            catch {
                "There was a problem getting WMI information from {0}. {1}" -f $config.ip_address, $_.Exception.Message
    
            }
    
        }
        else {
            "{0} could not be reached (i.e. PINGed)" -f $config.ip_address
        }    
    }
    
  • #27614
    Profile photo of Mel VW
    Mel VW
    Participant

    Thanks Rob and also Richard.

    Another question, what if I wanted to add a status if the gigs are say over 15 gigs?
    e.g.
    if over 15 gigs, it is "OK"
    if it is less than 15 gigs then "Warning"
    else below 10 gigs is "Critical"

  • #27615
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    I would suggest searching for "Powershell Storage Report". There are a plethora of examples on how to get storage information from servers with threshold indications like you are asking

You must be logged in to reply to this topic.