[Solved] Different results when running script manually

Welcome Forums General PowerShell Q&A [Solved] Different results when running script manually

This topic contains 5 replies, has 2 voices, and was last updated by

 
Participant
4 years, 3 months ago.

  • Author
    Posts
  • #17522

    Participant
    Points: 0
    Rank: Member

    I have a Logon.PS1 that runs in my organization across multiple sites.

    One of the many things it does is log the user's IP address, and what office they're in based on that IP address.

    function Get-IPAddress() {
        Get-NetIPAddress | Where-Object -FilterScript { $_.IPAddress.StartsWith("$strIPPrefix") } | Select-Object IPAddress -First 1 | foreach ($temp) { return $_.IPAddress }
    }

    This code, when I run the logon script manually after start time, works great.

    This code, when running automatically at start up, gives an empty answer.

    The GPO for logon scripts references the same PS1, with the same includes and settings (i.e. $strIPPrefix). When I log the values, $strIPPrefix is always set properly.

    Any idea on why it would fail to see the IP address on the machine when logging a user on?

  • #17523

    Member
    Points: 0
    Rank: Member

    There are a few problems with this code as written:

    • You're using a variable named $strIPPrefix which is not a parameter to the function, nor defined anywhere inside it. The variable would have to be set in a scope above the function, which is generally not a good practice. (If it's not set, then you'd be calling StartsWith(""), which always returns True.)
    • Your foreach syntax is wrong; the ($temp) part should be removed. You can also just replace that part of the pipeline and use -ExpandProperty on the call to Select-Object. For example:
      Select-Object -ExpandProperty IPAddress -First 1
    • The Get-NetIPAddress command only exists on Windows 8 or above. If you need this function to work on older operating systems, you'll need to rewrite it to use a different approach.
  • #17524

    Participant
    Points: 0
    Rank: Member

    All good suggestions, but I believe the third point you made is what I was looking for, specifically.

    The IPPrefix variable is set in the calling script, which is always the same, and logging them both shows the IPPrefix variable properly set when the IP address is requested. While I generally avoid global or high level variables, in this case because of how I use the scripts, it's working great. Always good to keep an eye open for better practices, though, so I appreciate you bringing it back to my attention.

    Very strange — I don't even recall what I used $temp for. The "foreach" is to get a string value rather than an object/object array. I want the function to return as simple a value as possible. Oddly enough, though you're correct, and it's absolutely wrong, however PowerShell processes it leaves me with valid results, reliably. I'm guessing with $temp not existing, it's using it to store the object each iteration. But that's just conjecture, since I'm referencing it by $_ instead of $temp. Very odd, and warrants more attention from me, for sure.

    I don't run many Windows 8 machines, so I didn't consider that a possibility. But my development workstation is. I checked the logs, and you're spot on — the issue was trying to use Get-NetIPAddress on Windows 7 boxes.

    Thanks a ton for the help, Mr. Wyatt — much appreciated.

  • #17525

    Member
    Points: 0
    Rank: Member

    No problem! Happy to help. 🙂

    Here's a start of how you can find your IP address on Windows 7 and earlier computers:

    Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IPEnabled = True' |
    Select-Object -ExpandProperty IPAddress
    
  • #17526

    Participant
    Points: 0
    Rank: Member
  • #17527

    Participant
    Points: 0
    Rank: Member

    Hah! You beat me to it! Solid.

    Many thanks again, sir!

The topic ‘[Solved] Different results when running script manually’ is closed to new replies.