export a lease

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of XONE3D . XONE3D . 2 years, 3 months ago.

  • Author
    Posts
  • #18266
    Profile photo of XONE3D .
    XONE3D .
    Participant

    Hello everybody,

    the script was make for a Windows server 2012 and is use to export a lease from a DHCP in a MySQL database

    i would know when is a possibility to convert this script to use him of a server 2008 R2 ?

    {[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
    $connectionString = "server=localhost;uid=user;pwd=user;database=arpwatch;Allow User Variables=true;Pooling=false;"
    $connection = New-Object MySql.Data.MySqlClient.MySqlConnection
    $connection.ConnectionString = $connectionString
    $connection.Open()
     
    Get-DhcpServerv4Lease -ScopeId 192.168.0.0 -ComputerName winserver |
    Select-Object -Property ClientId, IPAddress |
    Export-Csv -NoTypeInformation -Path C:\Users\Administrateur\mysql.csv
    
     
    $sql = @'
        LOAD DATA LOCAL INFILE 'C:\\Users\\Administrateur\\mysql.csv'
        INTO TABLE dhcp
        FIELDS TERMINATED BY ',' 
    	OPTIONALLY ENCLOSED BY '"' 
        LINES TERMINATED BY '\r\n'
        IGNORE 1 LINES;
    '@
      
    $command = New-Object MySql.Data.MySqlClient.MySqlCommand($sql, $connection)
     
    $rowsAffected = $command.ExecuteNonQuery();
      
    $connection.Close()}

    Thanks for your helping

  • #18269
    Profile photo of Don Jones
    Don Jones
    Keymaster

    The DHCP server on 2008R2 doesn't have PowerShell cmdlets, which means you're not going to be able to easily convert it.

    It looks like the DHCP commands run CIM internally, which means in theory you could just do WMI/CIM directly against a 2008R2 server. However, I don't know if DHCP in 2008R2 actually supports WMI/CIM. That may have been added in 2012 specifically to enable PowerShell, and if that's the case, then a conversion would be impossible.

    Generally speaking, PowerShell is a lot less useful for management on 2008R2, because 2008R2 doesn't have the needed commands or the underlying command support layers.

  • #18279
    Profile photo of XONE3D .
    XONE3D .
    Participant

    Thanks for the answer !

    i have a question what mean you with WMI/CIM ? ( i begin with powershell and windows server ) ?

    the only solution I've found is this command :

    netsh dhcp server scope 192.168.0.0 show clients > c:\dhcp.csv

    I have to look how to keep only the adress ip, and mac from the file text now with mysql

You must be logged in to reply to this topic.