locked
IP address display RRS feed

  • Question

  • Hello,

    in my company we set up a script to retrieve the IP address of a user using a 3G key to connect to our network VPN. except that the script should also return an IP even if the 3G key is not introduced. Unfortunately this does not work if we do not have a key.
    would it be possible to have your help.

    thank you

    Friday, October 5, 2018 1:37 PM

All replies

  • Add-Type -AssemblyName presentationframework
    Add-Type -AssemblyName PresentationCore
    Add-Type -AssemblyName WindowsBase
    #[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
    
    
    #Chargement de la vue
    $XamlLoader = (New-Object System.Xml.XmlDocument)
    $XamlLoader.Load((get-location).path + "\layout.xaml")
    $XamlMainWindow = $XamlLoader
    $Reader = (New-Object System.Xml.XmlNodeReader $XamlMainWindow)
    $Form = [Windows.Markup.XamlReader]::Load($Reader)
    
    #Nom de l'ordinateur
    $computerName = $Form.findname("computerName")
    $computerName.Content = $env:computername
    
    #Adresses IP
    $ip1 = $Form.findname("ip1")
    $ip2 = $Form.findname("ip2")
    $ip = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where {$_.IPAddress -ne $null})
    $ip1.Content = $ip[1].IPAddress[0]
    $ip2.Content = $ip[0].IPAddress[0]
    
    #Passerelle
    $gateway = $Form.findname('gateway')
    $gateway.Content = $(($ip | select-object -first 1).DefaultIpGateway[0])
    
    #Adresses MAC
    $mac1 = $Form.findname('mac1')
    # $mac2Txt = $Form.findname('mac2')
    $mac=(Get-WmiObject win32_networkadapterconfiguration | select macaddress | where {$_.macaddress -ne $null})
    #SLE/Debug $mac1 = $mac[0].macaddress
    # $mac2 = $mac[1].macaddress
    #SLE/Debug $mac1.Content = $mac1
    $mac1.Content = $mac[0].macaddress
    # $mac2Txt.Text = $mac2
    
    #Domaine
    $domain = $Form.findname('domain')
    $domain.Content = $env:USERDOMAIN
    
    #Serveur de connexion
    $coServer = $form.findname('coServer')
    $coServer.Content = $env:logonserver
    
    #Version système d'exploitation
    $osVersionTxt = $Form.findname('os')
    $os = Get-WmiObject -class Win32_OperatingSystem
    $osName=$os.Caption
    $osSp=$os.ServicePackMajorVersion
    #SLE/Debug $os.Content = $osName + ' Service Pack ' + $osSp
    $osVersionTxt.Content = $osName + ' Service Pack ' + $osSp
    
    #Numéro série PC
    $pcSerial = $Form.findname('pcSerial')
    $pcSerial.Content = (gwmi win32_bios).serialnumber
    
    #Version internet explorer
    $ieVersion = $Form.findname('ieVersion')
    $ieVersion.Content = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Internet Explorer').svcVersion
    
    #Utilisateur connecté localement
    $user = $Form.findname('user')
    $user.Content = $env:UserName
    
    #Dernier boot
    $lastBoot = $Form.findname('lastBoot')
    $date=Get-WmiObject win32_operatingsystem | select csname, @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
    $lastBoot.Content = $date.lastbootuptime
    
    #Mémoire physique
    $memory = $Form.findname('memory')
    $bytes = (Get-WmiObject -class "cim_physicalmemory" | Measure-Object -Property Capacity -Sum).Sum
    $mb = $bytes / 1024 / 1024
    $mb = 
    $memory.Content = $mb.tostring() + ' MB'
    
    #Espace libre disques durs
    $hdd = $Form.findname('hdd')
    $disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object FreeSpace
    $freeSpace = [Math]::Round($Disk.Freespace / 1GB, 2)
    $hddFormat = (GET-WMIOBJECT -query "select * from win32_logicaldisk where drivetype =3" | select *).filesystem
    $hddName = (GET-WMIOBJECT -query "select * from win32_logicaldisk where drivetype =3" | select *).caption
    $hdd.Content = $hddName + '\ ' + $freeSpace + ' GB ' + $hddFormat
    
    #Numéro de la hotline
    $hotline = $Form.findname('hotline')
    $hotline.Content = '01 69 93 11 76'
    
    #Modèle du PC
    $model = $Form.findname('model')
    $infosPC = Get-CimInstance -ClassName Win32_ComputerSystem
    $model.content = $infosPC.Model
    
    $Form.ShowDialog() | out-null 
    
    

    Friday, October 5, 2018 1:38 PM
  • the part that interests us is this one:

    # IP addresses
    $ ip1 = $ Form.findname ("ip1")
    $ ip2 = $ Form.findname ("ip2")
    $ ip = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where {$ _. IPAddress -ne $ null})
    $ ip1.Content = $ ip [1] .IPAddress [0]
    $ ip2.Content = $ ip [0] .IPAddress [0]

    Friday, October 5, 2018 1:39 PM
  • A lot of information but a very vague question.

    Win32_NetowrkAdapterConfiguratuion returns all adapters as a collection.  Run it at a prompt and you will see this.

    Get-WmiObject Win32_NetworkAdapterConfiguration

    To get all adapters with that are confihured for networking do this:

    Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True'

    With more than one result you will need to specify the adapter Index.

    Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'Index=12'


    \_(ツ)_/

    Friday, October 5, 2018 1:50 PM
  • Hello,

    in my company we set up a script to retrieve the IP address of a user using a 3G key to connect to our network VPN. except that the script should also return an IP even if the 3G key is not introduced. Unfortunately this does not work if we do not have a key.
    would it be possible to have your help.

    Add-Type -AssemblyName presentationframework
    Add-Type -AssemblyName PresentationCore
    Add-Type -AssemblyName WindowsBase
    #[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
    
    
    #Chargement de la vue
    $XamlLoader = (New-Object System.Xml.XmlDocument)
    $XamlLoader.Load((get-location).path + "\layout.xaml")
    $XamlMainWindow = $XamlLoader
    $Reader = (New-Object System.Xml.XmlNodeReader $XamlMainWindow)
    $Form = [Windows.Markup.XamlReader]::Load($Reader)
    
    #Nom de l'ordinateur
    $computerName = $Form.findname("computerName")
    $computerName.Content = $env:computername
    
    #Adresses IP
    $ip1 = $Form.findname("ip1")
    $ip2 = $Form.findname("ip2")
    $ip = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where {$_.IPAddress -ne $null})
    $ip1.Content = $ip[1].IPAddress[0]
    $ip2.Content = $ip[0].IPAddress[0]
    
    #Passerelle
    $gateway = $Form.findname('gateway')
    $gateway.Content = $(($ip | select-object -first 1).DefaultIpGateway[0])
    
    #Adresses MAC
    $mac1 = $Form.findname('mac1')
    # $mac2Txt = $Form.findname('mac2')
    $mac=(Get-WmiObject win32_networkadapterconfiguration | select macaddress | where {$_.macaddress -ne $null})
    #SLE/Debug $mac1 = $mac[0].macaddress
    # $mac2 = $mac[1].macaddress
    #SLE/Debug $mac1.Content = $mac1
    $mac1.Content = $mac[0].macaddress
    # $mac2Txt.Text = $mac2
    
    #Domaine
    $domain = $Form.findname('domain')
    $domain.Content = $env:USERDOMAIN
    
    #Serveur de connexion
    $coServer = $form.findname('coServer')
    $coServer.Content = $env:logonserver
    
    #Version système d'exploitation
    $osVersionTxt = $Form.findname('os')
    $os = Get-WmiObject -class Win32_OperatingSystem
    $osName=$os.Caption
    $osSp=$os.ServicePackMajorVersion
    #SLE/Debug $os.Content = $osName + ' Service Pack ' + $osSp
    $osVersionTxt.Content = $osName + ' Service Pack ' + $osSp
    
    #Numéro série PC
    $pcSerial = $Form.findname('pcSerial')
    $pcSerial.Content = (gwmi win32_bios).serialnumber
    
    #Version internet explorer
    $ieVersion = $Form.findname('ieVersion')
    $ieVersion.Content = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Internet Explorer').svcVersion
    
    #Utilisateur connecté localement
    $user = $Form.findname('user')
    $user.Content = $env:UserName
    
    #Dernier boot
    $lastBoot = $Form.findname('lastBoot')
    $date=Get-WmiObject win32_operatingsystem | select csname, @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
    $lastBoot.Content = $date.lastbootuptime
    
    #Mémoire physique
    $memory = $Form.findname('memory')
    $bytes = (Get-WmiObject -class "cim_physicalmemory" | Measure-Object -Property Capacity -Sum).Sum
    $mb = $bytes / 1024 / 1024
    $mb = 
    $memory.Content = $mb.tostring() + ' MB'
    
    #Espace libre disques durs
    $hdd = $Form.findname('hdd')
    $disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object FreeSpace
    $freeSpace = [Math]::Round($Disk.Freespace / 1GB, 2)
    $hddFormat = (GET-WMIOBJECT -query "select * from win32_logicaldisk where drivetype =3" | select *).filesystem
    $hddName = (GET-WMIOBJECT -query "select * from win32_logicaldisk where drivetype =3" | select *).caption
    $hdd.Content = $hddName + '\ ' + $freeSpace + ' GB ' + $hddFormat
    
    #Numéro de la hotline
    $hotline = $Form.findname('hotline')
    $hotline.Content = '01 69 93 11 76'
    
    #Modèle du PC
    $model = $Form.findname('model')
    $infosPC = Get-CimInstance -ClassName Win32_ComputerSystem
    $model.content = $infosPC.Model
    
    $Form.ShowDialog() | out-null 
    
    

    la partie qui nous interesse est celle-ci:
    #Adresses IP
    $ip1 = $Form.findname("ip1")
    $ip2 = $Form.findname("ip2")
    $ip = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where {$_.IPAddress -ne $null})
    $ip1.Content = $ip[1].IPAddress[0]
    $ip2.Content = $ip[0].IPAddress[0]

    • Merged by jrv Friday, October 5, 2018 2:04 PM DUPLICATE
    Friday, October 5, 2018 1:50 PM
  • Please do not repost the same question multiple times.


    \_(ツ)_/

    Friday, October 5, 2018 2:12 PM
  • $ip = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where {$_.IPAddress -ne $null})

    $ip

    Run that command both with the key and without and compare the output.  That should lead you in the right direction. 

    While the script may lead to the desired result most of the time - it's design is quite poor targeting just the first 2 network adapters.  The key present is going to change the indexing off so I suspect the "problem" lies with the ordering - you should query all network adapters with an IP assigned.

    Friday, October 5, 2018 6:37 PM
  • Not entirely sure what you want to do but have a look at Get-NetIPConfiguration and/or Get-NetIPAddress.
    Monday, October 8, 2018 10:07 AM
  • What I want to do is:
    make sure that when running the script, it gives me both IP key and IP ip that it gets through the VPN and if it has no 3G key then just the ip of his machine.

    i tried this: 

    $ip1 = $Form.findname("ip1")
    $ip2 = $Form.findname("ip2")

    $ip = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where {$_.IPAddress -ne $null})
    $ip1.Content = $(($ip | select-object -first 1).ipaddress[0])

    $ip1.Content = $ip[1].IPAddress[0]

    but same problem

    Monday, October 8, 2018 1:59 PM
  • You would need to check for the presence of the key on the system. Either by its interface name or (part of) the IP it gets. Then you can easily populate $IP.Content with whatever its actual IP is or wrap it in an if/else statement.

    For example, this would get the IPv4 address for a wifi adapter:

    Get-NetIPAddress|?{ $_.AddressFamily -eq'IPv4'-and$_.InterfaceAlias -match'wifi'} |selectIPAddress

    Tuesday, October 9, 2018 6:09 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    Best Regards,

    Lee

    Just do it.

    Friday, October 26, 2018 8:47 AM
  • Hi,

    As this thread has been quiet for a while, we will mark it as ‘Answered’ as the information provided should be helpful. If you need further help, please feel free to reply this post directly so we will be notified to follow it up. You can also choose to unmark the answer as you wish.

    Best Regards,

    Lee


    Just do it.

    Monday, November 5, 2018 9:27 AM