INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

You cannot call a method on a null-valued expression

You cannot call a method on a null-valued expression

(OP)
Hi All,

I'm writing a PS function to add to my Exchange 2007 migration script that will stop migrating users if the log disk space gets too low, thus breaking the CCR and SCR.

I am very new to PS and this is one of my first attempts so please bare with me. Here is the script thus far:

CODE

function getFreeSpace([string]$server, [string]$sg){
$volumes = Get-WmiObject Win32_Volume -computername $server
    foreach ($vol in $volumes){
        #$label = $vol.Label
        #Write-Host $label
        $strSG = "Logs " + $sg
        if($vol.Label.contains($strSG)){
            [math]::round($vol.freespace/[long]$vol.Capacity,2)*[int]100
        }
    }
}

if(getFreeSpace "svr-excca06" "SG6" -lt [int]99){
    $strFS = getFreeSpace "svr-excca06" "SG6"
    "Free Space = $strFS %"
    #I'll do something else here later on
}

I am thinking that one of the volumes doesn't have a label, thus when I call contains it returns null. Fair enough. So rather than creating the $label variable, I put it straight into the if statement. In my head, if $vol.Label.contains is null, then, surely it should ignore it and not complain?

The output eventually returns what I want, after the error:

CODE

ERROR: You cannot call a method on a null-valued expression.
ERROR: At line:22 char:25
ERROR: +         if($vol.Label.contains( <<<< $strSG)){
ERROR: You cannot call a method on a null-valued expression.
ERROR: At line:22 char:25
ERROR: +         if($vol.Label.contains( <<<< $strSG)){
Free Space = 99.00 %

*** PowerShell Script finished. ***

If any of you geni out there have any pointers for me, I'd be very greatful if you would share them.

Many thanks

Woter

RE: You cannot call a method on a null-valued expression

(OP)
Ok, I've worked it out, so I don't get any errors by telling the script to ignore null entries. I also had the evaluation wrong too. It might still be wrong, but it works for what I want smile Still open to suggestions though.

CODE

function getFreeSpace([string]$server, [string]$sg){
$volumes = Get-WmiObject Win32_Volume -computername $server
    foreach ($vol in $volumes){
        if($vol.Label -ne $NULL){
            $strSG = "Logs " + $sg
            if($vol.Label.contains($strSG)){
                $freeSpace = [math]::round($vol.freespace/[long]$vol.Capacity,2)*[int]100
            }
        }
    }
if($freeSpace -lt [int]25) {
    return $TRUE
}
}

if(getFreeSpace "svr-exccp05" "SG6" = $TRUE){
    "Stop processing users."
    }
else{
    "Continue..."
}

Hope it may help someone else.

Thanks

Woter

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close