locked
Optimize Powershell Script which sends a notification based on Network Location (should match Corporate Network) RRS feed

  • Question

  • This is what the script is supposed to do

    -----------------------------------------

     

    • Check whether client VPN is active

    o If yes: check whether the domain / a DC can be reached (e.g. "ping rX.company.net", should come from a COMPANY VPN)

     Show notification according to whatever criteria

    o If no: check whether the domain / a DC can be reached

     Yes: Computer is in the COMPANY corporate network  Send / display notification

     No: Computer is neither connected via VPN nor COMPANY corporate network  No notification

    -------------------------------------------------------------------------------------------

     

    #$ActiveDomainVPN = Get-NetConnectionProfile  -Name "company.net" -InterfaceAlias "COMPANY VPN wCA" | Where {$_.NetworkCategory -match 'DomainAuthenticated'}
    
    #$ActiveDomain = Get-NetConnectionProfile -Name "company.net" -InterfaceAlias "ethernet" | Where {$_.NetworkCategory -match 'DomainAuthenticated'}
    
    #$IP = Get-NetIPAddress | where {$_.IPAddress -match '10.*.*.*'}
    
     
    
    #match criteria based on condition and display message
    
    $ActiveDomainVPN = Get-NetConnectionProfile  -Name "company.net" -InterfaceAlias "COMPANY VPN wCA" -NetworkCategory "DomainAuthenticated"
    
    $ActiveDomain = Get-NetConnectionProfile -Name "company.net" -InterfaceAlias "ethernet" -NetworkCategory "DomainAuthenticated"
    
    $IP = Get-NetIPAddress -IPAddress "10.*.*.*"
    
     
    
    #display message if Active VPN and Domain network can be reached
    
    if ($ActiveDomainVPN -eq $IP)
    
    {
    
    [System.Windows.Forms.MessageBox]::Show("Main text here Main Text here." ,'Closing time!',0,48)
    
    Break
    
    }
    
    #display message if Domain network can be reached
    
    elseif ($ActiveDomain -eq $IP)
    
    {
    
    [System.Windows.Forms.MessageBox]::Show("Main text here Main Text here." ,'Closing time!',0,48)
    
    Break
    
    }
    
    #Dont display message if above criteria is not met
    
    else
    
    {
    
    Exit
    
    }

    Suggestions on code improvement will be welcomed 



    • Edited by Jzuh Wednesday, May 27, 2020 10:29 PM
    Wednesday, May 27, 2020 10:24 PM

Answers

  • This will help you find your mistakes:

    $ActiveDomainVPN = Get-NetConnectionProfile  -Name company.net -InterfaceAlias 'COMPANY VPN wCA' -NetworkCategory DomainAuthenticated
    $ActiveDomain = Get-NetConnectionProfile -Name company.net -InterfaceAlias 'ethernet' -NetworkCategory DomainAuthenticated
    $IP = Get-NetIPAddress -IPAddress '10.*.*.*'
    
    if ($ActiveDomainVPN -eq $IP){
        [System.Windows.Forms.MessageBox]::Show("Main text here Main Text here." ,'Closing time!',0,48)
    }elseif($ActiveDomain -eq $IP){
        [System.Windows.Forms.MessageBox]::Show("Main text here Main Text here." ,'Closing time!',0,48)
    }else{
        #Exit
    }

    I recommend learning PowerShell before trying more guesses.


    \_(ツ)_/

    • Marked as answer by Jzuh Thursday, May 28, 2020 7:02 AM
    Wednesday, May 27, 2020 11:53 PM

All replies

  • First start by formatting your code correctly.  The following link will help:


    \_(ツ)_/

    Wednesday, May 27, 2020 11:45 PM
  • This will help you find your mistakes:

    $ActiveDomainVPN = Get-NetConnectionProfile  -Name company.net -InterfaceAlias 'COMPANY VPN wCA' -NetworkCategory DomainAuthenticated
    $ActiveDomain = Get-NetConnectionProfile -Name company.net -InterfaceAlias 'ethernet' -NetworkCategory DomainAuthenticated
    $IP = Get-NetIPAddress -IPAddress '10.*.*.*'
    
    if ($ActiveDomainVPN -eq $IP){
        [System.Windows.Forms.MessageBox]::Show("Main text here Main Text here." ,'Closing time!',0,48)
    }elseif($ActiveDomain -eq $IP){
        [System.Windows.Forms.MessageBox]::Show("Main text here Main Text here." ,'Closing time!',0,48)
    }else{
        #Exit
    }

    I recommend learning PowerShell before trying more guesses.


    \_(ツ)_/

    • Marked as answer by Jzuh Thursday, May 28, 2020 7:02 AM
    Wednesday, May 27, 2020 11:53 PM
  • There are also a full set of video courses here:  They are mostly based on the free book linked above.

    Free Training Videos – Learn Windows PowerShell


    \_(ツ)_/

    Wednesday, May 27, 2020 11:55 PM
  • Taking a look at the training resources you sent
    Thursday, May 28, 2020 7:02 AM