locked
Newbie question RRS feed

  • Question

  • Hi,

    I am a newbie with power shell scrpting,I really appreciate if someone can look at this,

    I want to run a script from my windows 8.1 that is going to check the disks or volumes of our servers and it must send a report by mail.

    but when try to run the script get this error:

    Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
    At C:\My Scripts\diskspace.ps1:10 char:46
    + $tableFragment= Get-WMIObject  -ComputerName $Computer Win32_LogicalDisk `
    +                                              ~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    script must look in to a list.txt file for a list of the server/PCs, just for a test I put only the IP of one PC (windows 7) in this txt file but when run the script get the above error, the script is:

    $users = "toaddress@yourdomain.com" # List of users to email your report to (separate by comma)
    $fromemail = "fromaddress@yourdomain.com"
    $server = "yourmailserver.yourdomain.com" #enter your own SMTP server DNS name / IP address here
    $list = $args[0] #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
    $computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
    # Set free disk space threshold below in percent (default at 10%)
    [decimal]$thresholdspace = 10

    #assemble together all of the free disk space data from the list of servers and only include it if the percentage free is below the threshold we set above.
    $tableFragment= Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
    | select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
    | Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace} `
    | ConvertTo-HTML -fragment

    # assemble the HTML for our body of the email report.
    $HTMLmessage = @"
    <font color=""black"" face=""Arial, Verdana"" size=""3"">
    <u><b>Disk Space Storage Report</b></u>
    <br>This report was generated because the drive(s) listed below have less than $thresholdspace % free space. Drives above this threshold will not be listed.
    <br>
    <style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
    ol{margin:0;padding: 0 1.5em;}
    table{color:#FFF;background:#C00;border-collapse:collapse;width:647px;border:5px solid #900;}
    thead{}
    thead th{padding:1em 1em .5em;border-bottom:1px dotted #FFF;font-size:120%;text-align:left;}
    thead tr{}
    td{padding:.5em 1em;}
    tfoot{}
    tfoot td{padding-bottom:1.5em;}
    tfoot tr{}
    #middle{background-color:#900;}
    </style>
    <body BGCOLOR=""white"">
    $tableFragment
    </body>
    "@

    # Set up a regex search and match to look for any <td> tags in our body. These would only be present if the script above found disks below the threshold of free space.
    # We use this regex matching method to determine whether or not we should send the email and report.
    $regexsubject = $HTMLmessage
    $regex = [regex] '(?im)<td>'

    # if there was any row at all, send the email
    if ($regex.IsMatch($regexsubject)) {
        send-mailmessage -from $fromemail -to $users -subject "Disk Space Monitoring Report" -BodyAsHTML -body $HTMLmessage -priority High -smtpServer $server
    }


    Shahin

    • Moved by Bill_Stewart Wednesday, December 17, 2014 3:46 PM This is not "fix code for me so it works to my exact specifications" forum
    Tuesday, November 11, 2014 2:46 PM

All replies

  • Tuesday, November 11, 2014 2:50 PM
  • Hi,

    Thanks for your quick reply,

    if by require argument you mean a file with name of the server, yes I have a file in the sam folder as the script that contain the IP address of the PC that I want to check the drive.

    Thanks


    Shahin

    Tuesday, November 11, 2014 3:13 PM
  • Is $Computers an array of computer names? If so, it should be enumerated in a ForEach loop. The -ComputerName parameter should be the NetBIOS name of one computer at a time.

    Richard Mueller - MVP Directory Services

    Tuesday, November 11, 2014 3:14 PM
  • if by require argument you mean a file with name of the server, yes I have a file in the sam folder as the script that contain the IP address of the PC that I want to check the drive.

    Yeah, that's what I was referring to.

    Add in a Write-Output line before Get-WmiObject that will echo back what's in $computers to make sure that you're getting the proper data.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, November 11, 2014 3:24 PM
  • ok I add the write-output in the same code line as Get-WmiObject and also add the pause to the end of the script and I run the script and no error this time, and I get the message press any key to continu so I think this would mean the script has been run correctly, but I dont get any email! :(


    $tableFragment= Write-Output Get-WMIObject -ComputerName $Computer Win32_LogicalDisk `
    | select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
    | Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace} `
    | ConvertTo-HTML -fragment

    Any idea?

    Thanks


    Shahin

    Tuesday, November 11, 2014 3:34 PM