Category: Uncategorized

So, The Wemos D1 Mini pro is the board I use to program the NONOS SDK right now currently. The downside is it only exposes one UART interface (UART0) and with that comes some minor issues.
1: If you connect an FTDI chip to this port the TX line from the FTDI is always HIGH and will keep the RX Pin on the ESP8266 HIGH and will stop new firmware from being loaded until you physically disconnect the FTDI chip.

2: The Line is not dedicated so sending Debug in on this line may interfere with other external devices connected to this line.

So the first issue is solved by calling system_uart_swap() thus swapping the RX/TX line with D7 and D8 for the RTS CTS lines freeing the TX/RX labeled pins on the Wemos

The 2nd one can use GPIO2 and use UART1 to transmit the data.

LOCAL void ICACHE_FLASH_ATTR uart_config(uint8 uart_no, void * callback(uint8))
{
    os_printf("uart_config:%d", uart_no);
    if (uart_no == UART0)
    {
        /* UART0 */
        ETS_UART_INTR_ATTACH(uart0_rx_intr_handler,  callback);
        PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
        PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
        //Enable RxD pin
        PIN_PULLUP_EN(PERIPHS_IO_MUX_U0RXD_U);
        PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD);        
        //PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS); //We don't use RTS
    }
    else
    {
        PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U);
        PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); //D4 used for Debug out.
        PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U);
    }

    uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));

    WRITE_PERI_REG(UART_CONF0(uart_no), CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT));

    //clear rx and tx fifo,not ready
    SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
    CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);

    WRITE_PERI_REG(UART_CONF1(uart_no), ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S));

    //clear all interrupt
    WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
    //enable rx_interrupt
    SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA);
}

void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br, void * callback)
{
    // rom use 74880 baut_rate, here reinitialize
    UartDev.baut_rate = uart0_br;
    uart_config(UART0, callback);

    UartDev.baut_rate = uart1_br;
    uart_config(UART1, callback);

    ETS_UART_INTR_ENABLE();

    // install uart1 putc callback
    //system_uart_swap(); //http://smallbits.marshall-tribe.net/blog/2016/11/13/esp8266-quiet-uart
    //PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
    //os_install_putc1((void *)uart1_write_char);
}

void ICACHE_FLASH_ATTR sdk_init_done_cb(void) 
{ 
    os_printf("[%s] initializing ESP8266!\n", __func__);
    while(true)
    {
        os_printf("test");
        uart0_tx_buffer("page 1\xff\xff\xff", 9); //This is our Prod Port
        uart1_tx_buffer("page 2\xff\xff\xff", 9); //This is our Debug Port
        delay_second();
        delay_second();
    }
}

void ICACHE_FLASH_ATTR SetupUART(void)
{
    //Careful when the callback is triggered that it does not kick off any premature events like sending network packets before the network is connected, thus crashing your app
    uart_init(BIT_RATE_115200, BIT_RATE_115200, &UartReceive); //This only seems to be kicked off via the COM-USB onboard, Not the TX/RX port
    system_set_os_print(1); //Turns os_PrintF Log Printing On or Off
    //We use UART swap because the RXpin is always High from external outputs causing issues when loading new firmware.
    //Keep in mind uart swap only swaps the RTS and CTS of the same UART to the RX and TX of the same UART, NOT UART1 to UART0
    //"UART0 swap. Use MTCK as UART0 Rx, MTDO as UART0 Tx, so ROM log will not output from this new UART0. MTDO (U0RTS) and MTCK (U0CTS) also need to be used as UART0 in hardware
    system_uart_swap(); //http://smallbits.marshall-tribe.net/blog/2016/11/13/esp8266-quiet-uart - Makes D7 RX (MTCK) and D8 TX (MTDO)
    os_install_putc1((void *)uart1_write_char); //Redirect OS_PRINTF to UART1, our debug port
}

void ICACHE_FLASH_ATTR user_init()
{
    SetupUART();

    system_init_done_cb(sdk_init_done_cb);

    wifi_set_opmode(0);
    wifi_set_sleep_type( NONE_SLEEP_T );

    ETS_GPIO_INTR_DISABLE();// Disable gpio interrupts
    gpio_init();    

    SetAllGPIOPinsAsOutput();

    /* Need to fix this to display value    */
    uint32 VDDADCByte[4] = {0};
    spi_flash_read(0x3fc06b, (uint32 *)&VDDADCByte, 1); //Read pads the other 3 bytes with FF

    os_printf("\r\n\r\nStarting ESP8266 OTA!\r\nSDK version:%s\r\nLoaded from: %02x\r\nVdd33_Const: %02x\r\n", system_get_sdk_version(), system_get_userbin_addr(), (VDDADCByte[0] & 0xff));

    //Turn off LED, We cannot touch this pin as this is our debug pin D4/GPIO2
    //PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
    //gpio_output_set((1 << 2), 0, 0, 0);

    //Start os task
    system_init_done_cb(sdk_init_done_cb);
    //system_os_task(loop, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen); //Task to Signal for later
}

First download NUGet from NuGet.org.
Install it for your cordinating version of visual studio

Once installed Launch VS then launch NuGet’s console from within

Run the following line for Framework 4.0 and Above
PM> Install-Package ASTreeView

If your project is targeted to .Net Framework 2.0 or 3.5, please install the legacy package using this line below instead.
PM> Install-Package ASTreeView.Legacy

Once ran add the reference to the project, The path to the DLL looked like this

C:\Users[username]\Documents\Visual Studio 2015\Projects\ASPTreeDragAndDrop\packages\ASTreeView.1.6.0.4\lib\net40

Then after that add the utilities to the toolbox.

Now you can start adding your tree and design your Drag and Drop Functionality.

So once in a while, I get a stingy program that refuses to remove itself from my Add Or Remove Program’s list.

So what I end up doing is jumping into the registry and removing the Key from the uninstall list. In my case, I ended up having to remove a couple of keys under that list to fully rip it out of AppWiz.

And the final result.

Our department is pretty large and users tend to change user folder permissions out of our standard. This script I threw together to cycle through a list of folders that match the users ‘samAccountName’ in active directory. BEWARE This will delete folders and data of users that do not exist.

Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host

$Domain = "MyDomain"

$RootFSPath = "G:\users3"

Function GetFolderACL([string]$User, [bool]$Recursive) 
{
    $filePath = "$RootFSPath\$User"
    #Get-Acl -Path $filePath | Format-List
    $filePathacl = Get-Acl -Path $filePath
    if ($Recursive -eq $True)
    {
        $folders = Get-ChildItem $filePath -Recurse #-Directory
        foreach ($folder in $folders)
        {
            #Get-Acl -Path $folder.FullName | Format-List
            foreach ($access in $filePathacl.Access) 
            {
                if ($access.IdentityReference.Value -eq "$Domain\$user" -and $access.FileSystemRights -eq "Modify, Synchronize") 
                {
                    continue
                }
                if ($access.IdentityReference.Value -eq "NT AUTHORITY\SYSTEM" -and $access.FileSystemRights -eq "FullControl") 
                {
                    continue
                }
                if ($access.IdentityReference.Value -eq "BUILTIN\Administrators" -and $access.FileSystemRights -eq "FullControl") 
                {
                    continue
                }
                if ($access.IdentityReference.Value -eq "$Domain\Domain Admins" -and $access.FileSystemRights -eq "FullControl") 
                {
                    continue
                }
                Write-Host $access.IdentityReference $access.FileSystemRights
            }
        }
    }
}

Function GetFolderACLRecursive([string]$filePath, [string]$User,[bool]$Recursive) 
{

    if ($Recursive -eq $True)
    {
        $folders = Get-ChildItem $filePath -Recurse -Directory
        foreach ($folder in $folders)
        {
            GetFolderACLRecursive $folder.PSPath $true
        }
    }

    [bool]$UserPerm = $false
    [bool]$SystemPerm = $false
    [bool]$AdminPerm = $false
    [bool]$DomainAdminPerm = $false

    $Searcher = [ADSISearcher]"(sAMAccountName=$folder)"
    $Results = $Searcher.FindOne()

    If ($Results -eq $Null) 
    {
        if ($filePath -ne "$RootFSPath\")
        {
            #try your best not to wak the parent folder due to Hr's typeo's ;)
            Write-Host "$folder does not exist in AD, $filePath can be deleted... Deleting"
            Remove-Item –path $filePath –recurse -force
            $UserPerm = $true
        }
    }
    else #If they do exit check the ACLS
    {
        $filePathacl = Get-Acl -Path $filePath
        foreach ($access in $filePathacl.Access) 
        {
            if ($access.IdentityReference.Value -eq "$Domain\$User" -and $access.FileSystemRights -eq "Modify, Synchronize") 
            {
                $UserPerm = $true
                continue
            }
            if ($access.IdentityReference.Value -eq "NT AUTHORITY\SYSTEM" -and $access.FileSystemRights -eq "FullControl") 
            {
                $SystemPerm = $true
                continue
            }
            if ($access.IdentityReference.Value -eq "BUILTIN\Administrators" -and $access.FileSystemRights -eq "FullControl") 
            {
                $AdminPerm = $true
                continue
            }
            if ($access.IdentityReference.Value -eq "$Domain\Domain Admins" -and $access.FileSystemRights -eq "FullControl") 
            {
                $DomainAdminPerm = $true
                continue
            }

            Write-Host $filePath.PadRight(15) $access.IdentityReference $access.FileSystemRights 
        }

        if ($SystemPerm -eq $false)
        {
            Write-Host "Missing System Permission to $filePath"
        }

        if ($AdminPerm -eq $false)
        {
            Write-Host "Missing Admin Permission to $filePath"
        }

        if ($DomainAdminPerm -eq $false)
        {
            Write-Host "Missing DominAdmin Permission to $filePath"
        }

        if (($UserPerm -eq $false) -or ($SystemPerm -eq $false) -or ($AdminPerm -eq $false) -or ($DomainAdminPerm = $false))
        {
            return $false
        }
    }
}

Function SetFolderACL([string]$User, [bool]$Recursive, [bool]$EnableInheritance, [bool]$DisableInheritance) 
{  
    $filePath = "$RootFSPath\$User"
    $filePathacl = Get-Acl -Path $filePath

    if ($EnableInheritance -eq $True) #This should not happen if we are looking to disable Inheritance, We want to remove it from the parent folder last below in this code.
    {
        foreach ($access in $filePathacl.Access) 
        {
            #if ($access.IdentityReference.Value -eq $user) {
                #$acl.RemoveAccessRule($access) | Out-Null
                $filePathacl.RemoveAccessRule($access)
            #}
        }

        $filePathacl.SetAccessRuleProtection($false,$false)
        Set-Acl -Path $filePath -AclObject $filePathacl
    }

    if ($Recursive -eq $True)
    {
        $folders = Get-ChildItem $filePath -Recurse #-Directory
        foreach ($folder in $folders) 
        {
            $acl = Get-Acl -Path $folder.FullName

            Write-Host $folder.FullName

            if ($EnableInheritance -eq $True)
            {
                $acl.SetAccessRuleProtection($false,$false)
                Set-Acl -Path $folder.FullName -AclObject $acl
            }
            if ($DisableInheritance -eq $True)
            {
                $acl.SetAccessRuleProtection($true,$true)
                Set-Acl -Path $folder.FullName -AclObject $acl
            }

            foreach ($access in $acl.Access) {
                #if ($access.IdentityReference.Value -eq $user) {
                    #$acl.RemoveAccessRule($access) | Out-Null
                    $acl.RemoveAccessRule($access)
                #}
            }

            Set-Acl -Path $folder.FullName -AclObject $acl

        }
    }

    if ($DisableInheritance -eq $True) #This should not happen if we are looking to disable Inheritance, We want to remove it from the parent folder last below in this code.
    {
        $filePathacl = Get-Acl -Path $filePath   
        $filePathacl.SetAccessRuleProtection($true,$true)

        foreach ($access in $filePathacl.Access) 
        {
            #if ($access.IdentityReference.Value -eq $user) {
                #$acl.RemoveAccessRule($access) | Out-Null
                $filePathacl.RemoveAccessRule($access)
            #}
        }
        Set-Acl -Path $filePath -AclObject $filePathacl
    }

    $acl = Get-Acl -Path $filePath
    $permission = "$Domain\$user", "Modify", "ContainerInherit, ObjectInherit", "None", "Allow"
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
    $acl.SetAccessRule($accessRule)
    $acl | Set-Acl $filepath
} 

Function SetStrightFolderACL([string]$User, [bool]$Recursive, [bool]$EnableInheritance, [bool]$DisableInheritance) 
{
    Write-Host $folder.FullName
    $filePath = "$RootFSPath\$User"
    $filePathacl = Get-Acl -Path $filePath
    $acl = Get-Acl -Path $filePath
    $permission = "$Domain\$user", "Modify", "ContainerInherit, ObjectInherit", "None", "Allow"
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
    $acl.SetAccessRule($accessRule)
    $acl | Set-Acl $filepath 
}

$folders = Get-ChildItem "$RootFSPath" #-Recurse #-Directory
foreach ($folder in $folders) 
{
    $acl = Get-Acl -Path $folder.FullName
    #Write-Host (GetFolderACLRecursive "$RootFSPath\$folder" $folder $false)
    $result = GetFolderACLRecursive "$RootFSPath\$folder" $folder $false
    if ($result -eq $false)
    {
        $Searcher = [ADSISearcher]"(sAMAccountName=$folder)"
        $Results = $Searcher.FindOne()
        If ($Results -eq $Null) 
        {
            #Write-Host "Users does not exist in AD"
        }
        Else 
        {
            #Write-Host "User found in AD"
            SetFolderACL $folder.Name $true $false $True #Remove Inhairtance
            SetFolderACL $folder.Name $true $true $false #Enable Inhairtance
        }        
    }
    #Write-Host $folder.FullName
    #Write-Host $folder.Name
    #GetFolderACL $folder.Name $false
    #SetStrightFolderACL $folder.Name $true $true $false #Enable Inhairtance
    #SetFolderACL $user $true $true $false #Enable Inhairtance
}

exit

#GetFolderACL $user $true
SetFolderACL $user $true $false $True #Remove Inhairtance
SetFolderACL $user $true $true $false #Enable Inhairtance

exit

SetFolderACL $user $true $false $True #Remove Inhairtance

exit

SetFolderACL $user $true $true $false #Enable Inhairtance

exit

Windows Firewall can be a minorly tricky subject when it comes to the configuration at a level with scalability. After connecting with Microsoft it appears the three main options are

1: Group Policy <– Imagine your GPEdit to reflect 1000+ lines of custom configurations for each server in your environment. 2: Desired State Configuration <– Can this be automated easily without a hassle and a lot of technical programming knowledge 3: WMI <– Slow as is everything else in WMI 4: Manual Setup <– ICK! 4: Don’t use it <– Not an option for us

DSC works by generating a MOF file that the client machine read’s to the kick itself into it’s desired state. The client-side digests the file via SMB/HTTP/HTTPS and then ensure’s its configuration is up to date. An interval can be set in the parameters of SetConfiguration but what we really want out of this is can we dynamically generate these .MOF files on the fly to then push to our servers. Let’s take a look at the PowerShell code example and the .MOF it produces to see what it looks like.

You will need a copy of the PowerShell modules below to copy into your Modules folder on your local test machine.

xNetworking

#Unpack xNetworking to the following folders
#C:\Program Files\WindowsPowerShell\Modules
#C:\Program Files (x86)\WindowsPowerShell\Modules
#https://gallery.technet.microsoft.com/scriptcenter/xNetworking-Module-818b3583
Configuration DSCFirewallRule
{
    param
    (
        [string[]] $NodeName = 'localhost'
    )

    Import-DSCResource -ModuleName xNetworking

    Node $NodeName
    {
        xFirewall Firewall1 
        {
            Access      = 'Block'
            Name        = 'NotePadFirewallRule'
            DisplayName = 'Firewall Rule for Notepad.exe'
            Ensure      = 'Present'
            Profile     = ('Domain', 'Private')
            Direction   = 'OutBound'
            RemotePort  = ('8080', '8081')
            LocalPort   = ('9080', '9081')
            Protocol    = 'TCP'
            Description = 'Firewall Rule for Notepad.exe'
            Service = 'WinRM'
        }

        xFirewall Firewall2 
        {
            Access      = 'Allow'
            Name        = 'NotePad++FirewallRule'
            DisplayName = 'Firewall Rule for Notepad++.exe'
            Ensure      = 'Present'
            Profile     = ('Domain', 'Private')
            Direction   = 'OutBound'
            RemotePort  = ('8082', '8084')
            LocalPort   = ('9086', '9085')
            Protocol    = 'TCP'
            Description = 'Firewall Rule for Notepad++.exe'
            Service = 'WinRM'
        }

    }

}

DSCFirewallRule
Start-DscConfiguration -Path .\DSCFirewallRule -Wait -Force -Verbose 

Now, using the code above, it will generate a .MOF file to then use on the destination machines.

/*
@TargetNode='localhost'
@GeneratedBy=Mr. Hall
@GenerationDate=06/12/2018 16:55:22
@GenerationHost=myComputer
*/

instance of MSFT_xFirewall as $MSFT_xFirewall1ref
{
Description = "Firewall Rule for Notepad.exe";
 Direction = "Outbound";
 DisplayName = "Firewall Rule for Notepad.exe";
 ResourceID = "[xFirewall]Firewall1";
 RemotePort = {
    "8080",
    "8081"
};
 Name = "NotePadFirewallRule";
 Ensure = "Present";
 Protocol = "TCP";
 SourceInfo = "C:\\PowerShell\\DSC.ps1::13::9::xFirewall";
 Service = "WinRM";
 LocalPort = {
    "9080",
    "9081"
};
 ModuleVersion = "2.1.1";
 ModuleName = "xNetworking";
 Profile = {
    "Domain",
    "Private"
};
 Access = "Block";

};

instance of MSFT_xFirewall as $MSFT_xFirewall2ref
{
Description = "Firewall Rule for Notepad++.exe";
 Direction = "Outbound";
 DisplayName = "Firewall Rule for Notepad++.exe";
 ResourceID = "[xFirewall]Firewall2";
 RemotePort = {
    "8082",
    "8084"
};
 Name = "NotePad++FirewallRule";
 Ensure = "Present";
 Protocol = "TCP";
 SourceInfo = "C:\\PowerShell\\DSC.ps1::28::9::xFirewall";
 Service = "WinRM";
 LocalPort = {
    "9086",
    "9085"
};
 ModuleVersion = "2.1.1";
 ModuleName = "xNetworking";
 Profile = {
    "Domain",
    "Private"
};
 Access = "Allow";

};

instance of OMI_ConfigurationDocument
{
 Version="1.0.0";
 Author="Mr. Hall";
 GenerationDate="06/12/2018 16:55:22";
 GenerationHost="MyComputer";
};

Now, this is great news. The .MOF file seems predictably easy to regenerate and giving the pattern above, I may be able to write a WebServer to generate out the following pattern and have it pushed to a remote machine, I’ll give it a shot later this week and post the results. Thing’s are looking pretty promising.

Some of the hurdles that had to be made were
1: Adding Remote/Local Address to XFirewall
2: Creating a WebPage to then track and craft powershell scripts to then generate the MOF file.
3: Tracking the files that are GUID.mof format. We used the Guid in AD computer objects to then track this. A great source to reading up on this issue is here

Great src on this topic: https://blogs.technet.microsoft.com/heyscriptingguy/2016/01/22/conceptualize-desired-state-configuration-part-5/

So this sniblet is a little something I wrote a few years back that I still use today. In our environment, we have a Public folder that is available to users on the exchange server. Within the Public folder drilling down is a list of Contact cards that are maintained by our secretary. This code will pull that information and apply faces from the HTTPS source matching the employee’s information. In the end, you basically get a nice clean list with employee images attached to see who you are communicating with.

Imports Microsoft.Office.Interop.Outlook
Imports System.Runtime.InteropServices

Public Class Form1

    Private Sub LoadContacts()
        Dim Myresult = MessageBox.Show("Before Running this Program Please clear your local copy of STAFF contacts then press OK", "Are you Ready?", MessageBoxButtons.OKCancel)
        If Not Myresult = vbOK Then End

        Dim oApp = New Microsoft.Office.Interop.Outlook.Application()
        Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon(Nothing, Nothing, False, True)
        Dim PrivateFolder As Microsoft.Office.Interop.Outlook.MAPIFolder = oNS.GetDefaultFolder(OlDefaultFolders.olFolderContacts)
        Dim PublicContacts As Microsoft.Office.Interop.Outlook.MAPIFolder = oNS.GetDefaultFolder(OlDefaultFolders.olPublicFoldersAllPublicFolders).Folders("Staffing").Folders("staff")

        Dim Contacts As Microsoft.Office.Interop.Outlook.MAPIFolder
        Try
            Contacts = PrivateFolder.Folders("staffWpics")
        Catch ex As COMException
            'it does not exist
            Contacts = PublicContacts.CopyTo(PrivateFolder)
            Contacts.Name = "isstaffWpics"
            Debug.WriteLine(Contacts.ShowAsOutlookAB)
            Contacts.ShowAsOutlookAB = True
        End Try

        Dim MyWebRequest As New System.Net.WebClient
        MyWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials

        System.IO.Directory.CreateDirectory("C:\Pics")


        Dim DidIFindPicture As Boolean = False
        For Each MyContact As Microsoft.Office.Interop.Outlook.ContactItem In Contacts.Items
            DidIFindPicture = False
            Dim EID As String
            Dim Email As String
            Try
                MyWebRequest.DownloadFile("https://Faces.Local/EmployeeFace.aspx?FULLNAME=" & MyContact.LastName & ",%20" & MyContact.FirstName, "C:\Pics\" & MyContact.LastName & ",%20" & MyContact.FirstName & ".jpg")
                DidIFindPicture = True
            Catch ex As System.Exception
                Try
                    EID = MyContact.Email1Address.Substring(MyContact.Email1Address.LastIndexOf("=") + 1).PadLeft(5, "0")
                    MyWebRequest.DownloadFile("https://Faces.Local/EmployeeFace.aspx?EID=" & EID, "C:\Pics\" & MyContact.LastName & ",%20" & MyContact.FirstName & ".jpg")
                    DidIFindPicture = True
                Catch exa As System.Exception
                    Try
                        Dim UnCleanedEmail As String = MyContact.Email1DisplayName.Substring(MyContact.Email1DisplayName.LastIndexOf("(") + 1)
                        Email = UnCleanedEmail.Substring(0, UnCleanedEmail.Length - 1)
                        MyWebRequest.DownloadFile("https://Faces.Local/EmployeeFace.aspx?EID=" & Email, "C:\Pics\" & MyContact.LastName & ",%20" & MyContact.FirstName & ".jpg")
                        DidIFindPicture = True
                    Catch exaa As System.Exception
                        Debug.WriteLine("No Picture Found for: " & MyContact.FirstName & " " & MyContact.LastName & " | " & EID & " - " & Email)
                        For Each MyProp As ItemProperty In MyContact.ItemProperties
                            Debug.WriteLine(MyProp.Name & " - " & MyProp.Value.ToString)
                        Next
                    End Try

                End Try
            End Try

            If DidIFindPicture Then
                MyContact.AddPicture("C:\Pics\" & MyContact.LastName & ",%20" & MyContact.FirstName & ".jpg")
                MyContact.Save()
            End If

            For Each Attachment As Microsoft.Office.Interop.Outlook.Attachment In MyContact.Attachments
                Debug.WriteLine(Attachment.DisplayName)
                If Attachment.DisplayName = "ContactPicture.jpg" Then
                    'MyContact.AddPicture("c:\120604_0000.jpg")
                    'MyContact.Save()
                End If
            Next
        Next
        MsgBox("All Pictures Imported as isstaffWpics")
        End
        For Each MyContact As Microsoft.Office.Interop.Outlook.ContactItem In PublicContacts.Items
            'Contacts.Items.Add(
        Next

        End


        For Each MyFolder As Microsoft.Office.Interop.Outlook.MAPIFolder In PrivateFolder.Folders
            Debug.WriteLine(MyFolder.Name)
        Next

        End

        For Each MYContact As Microsoft.Office.Interop.Outlook.ContactItem In PublicContacts.Items
            Debug.WriteLine(MYContact.FirstName)
        Next
        End
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            LoadContacts()
    End Sub

    Private Sub Fail()
        Dim oApp = New Microsoft.Office.Interop.Outlook.Application()
        Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon(Nothing, Nothing, False, True)

        Dim oDLs As Microsoft.Office.Interop.Outlook.AddressLists
        Dim oGal As Microsoft.Office.Interop.Outlook.AddressList

        For Each MyFolder As MAPIFolder In oNS.Folders
            Debug.WriteLine(MyFolder.Name)
            If MyFolder.Name.StartsWith("Public Folders - ") Then
                For Each MySubFolder As MAPIFolder In MyFolder.Folders
                    If MySubFolder.Name = "All Public Folders" Then
                        Debug.WriteLine(MySubFolder.Name)
                        For Each MySubFolder2 As MAPIFolder In MySubFolder.Folders
                            Debug.WriteLine(MySubFolder2.Name)
                            If MySubFolder2.Name = "Information Systems" Then
                                For Each MySubFolder3 As MAPIFolder In MySubFolder2.Folders
                                    If MySubFolder3.Name = "isstaff" Then
                                        Dim outlookname As Microsoft.Office.Interop.Outlook.NameSpace
                                        outlookname = MySubFolder3.Items(0)
                                        Debug.WriteLine(outlookname.AddressLists(0).Name)
                                        oGal = MySubFolder3.Items.Item(0)
                                        Exit Sub
                                    End If
                                Next
                            End If
                        Next
                    End If
                Next
            End If
        Next
        Exit Sub

        For Each AL As Microsoft.Office.Interop.Outlook.AddressList In oDLs
            Debug.WriteLine(AL.Name)
        Next
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Global Address List")
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Contacts")
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Suggested Contacts")
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("staff") 'Right click and show this folder as item in outlook
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Public Folders")

        Dim oEntries As Microsoft.Office.Interop.Outlook.AddressEntries = oGal.AddressEntries
        For Each ADDREntry As Microsoft.Office.Interop.Outlook.AddressEntry In oEntries
            Debug.WriteLine(ADDREntry.Name)

            Dim MyEx = ADDREntry.GetContact
            Debug.WriteLine(MyEx.HasPicture)
            For Each Attachment As Microsoft.Office.Interop.Outlook.Attachment In MyEx.Attachments
                Debug.WriteLine(Attachment.DisplayName)
                If Attachment.DisplayName = "ContactPicture.jpg" Or ADDREntry.Name = "Hall, Nicholas (NickHall@Domain.Com)" Then
                    'Attachment.SaveAsFile("C:\Temp.jpg")
                    PictureBox1.Image = Image.FromFile("C:\Temp.jpg")
                    MyEx.RemovePicture()
                    'MyEx.AddPicture("c:\120604_0000.jpg")
                    MyEx.Save()
                End If
            Next

            If MyEx.Attachments.Count = 0 Then
                'MyEx.AddPicture("c:\120604_0000.jpg")
                'MyEx.Save()
            End If

        Next
    End Sub

    Private Sub GoodCode()
        Dim oApp = New Microsoft.Office.Interop.Outlook.Application()
        Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon(Nothing, Nothing, False, True)
        Dim oDLs As Microsoft.Office.Interop.Outlook.AddressLists = oNS.AddressLists
        For Each AL As Microsoft.Office.Interop.Outlook.AddressList In oDLs
            Debug.WriteLine(AL.Name)
        Next
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Global Address List")
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Contacts")
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Suggested Contacts")
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("isstaff") 'Right click and show this folder as item in outlook
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Public Folders")
        Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Public Folders\All Public Folders\Staffing")
        Dim oEntries As Microsoft.Office.Interop.Outlook.AddressEntries = oGal.AddressEntries
        For Each ADDREntry As Microsoft.Office.Interop.Outlook.AddressEntry In oEntries
            Debug.WriteLine(ADDREntry.Name)

            Dim MyEx = ADDREntry.GetContact
            Debug.WriteLine(MyEx.HasPicture)
            For Each Attachment As Microsoft.Office.Interop.Outlook.Attachment In MyEx.Attachments
                Debug.WriteLine(Attachment.DisplayName)
                If Attachment.DisplayName = "ContactPicture.jpg" Or ADDREntry.Name = "Hall, Nicholas (NickHall@Domain.Com)" Then
                    'Attachment.SaveAsFile("C:\Temp.jpg")
                    PictureBox1.Image = Image.FromFile("C:\Temp.jpg")
                    MyEx.RemovePicture()
                    'MyEx.AddPicture("c:\120604_0000.jpg")
                    MyEx.Save()
                End If
            Next

            If MyEx.Attachments.Count = 0 Then
                'MyEx.AddPicture("c:\120604_0000.jpg")
                'MyEx.Save()
            End If

        Next


        Exit Sub
        Dim sDL As String = "Information Systems Desktop Support"
        Dim oDL As Microsoft.Office.Interop.Outlook.AddressEntry = oEntries(sDL)
        Dim oEntry As Microsoft.Office.Interop.Outlook.AddressEntry
        Dim I As Integer
        For I = 1 To oEntries.Count - 1
            oEntry = oEntries(I)
            Debug.WriteLine(oEntry.Address)
            Dim MyEx = oEntry.GetExchangeUser
            MyEx.GetPicture()
        Next
    End Sub

    Private Sub WORKS()
        Dim oApp = New Microsoft.Office.Interop.Outlook.Application()
        Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon(Nothing, Nothing, False, True)
        Dim oDLs As Microsoft.Office.Interop.Outlook.AddressLists = oNS.AddressLists
        For Each AL As Microsoft.Office.Interop.Outlook.AddressList In oDLs
            Debug.WriteLine(AL.Name)
        Next
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Global Address List")
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Contacts")
        'Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("Suggested Contacts")
        Dim oGal As Microsoft.Office.Interop.Outlook.AddressList = oDLs("staff") 'Right click and show this folder as item in outlook
        Dim oEntries As Microsoft.Office.Interop.Outlook.AddressEntries = oGal.AddressEntries
        For Each ADDREntry As Microsoft.Office.Interop.Outlook.AddressEntry In oEntries
            Debug.WriteLine(ADDREntry.Name)

            Dim MyEx = ADDREntry.GetContact
            Debug.WriteLine(MyEx.HasPicture)
            For Each Attachment As Microsoft.Office.Interop.Outlook.Attachment In MyEx.Attachments
                Debug.WriteLine(Attachment.DisplayName)
                If Attachment.DisplayName = "ContactPicture.jpg" Then
                    Attachment.SaveAsFile("C:\Temp.jpg")
                    PictureBox1.Image = Image.FromFile("C:\Temp.jpg")
                    MyEx.AddPicture("c:\120604_0000.jpg")
                End If
            Next

        Next

    End Sub
    Private Sub Junk()
        Dim MyContacts As ContactItem
        Dim MyEntry As Microsoft.Office.Interop.Outlook.AddressEntries

        Dim MyMembers As System.Reflection.MemberInfo() = MyContacts.GetType().FindMembers(Reflection.MemberTypes.All, Reflection.BindingFlags.Instance, Nothing, Nothing)
        For Each MyMem As System.Reflection.MemberInfo In MyMembers
            Debug.WriteLine(MyMem.Name)
        Next
    End Sub
End Class

So, In the last few days I am wrapping up some UART code on the esp8266, and as you can tell, it’s been quite a ride. I’ve been working with these Nextion HMI touch screen’s and I gotta say these thing’s are pretty awesome. I love the idea and it’s easy to work with. I’ll post some update’s once I get some program made but so far it’s pretty easy. Now only if I can get this ESP8266 to talk to this thing, everything would be rolling a bit smoother. Anyways to all the other UC / Arudino junkies, Highly recommended. 🙂


//https://www.mikrocontroller.net/attachment/263828/The-ESP8266-Book-August-2015.pdf
#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "ip_addr.h"
#include "mem.h"
#include "user_interface.h"
#include "lwip/stats.h"
#include "espconn.h"

#include "c_types.h" ////ONE WIRE

#include "../library/uart.h" //Copy these from your Driver Lib to your local folder
#include "../library/gpio16.h" //Copy these from your Driver Lib to your local folder

#include "../library/common.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

#define user_procTaskPrio        0
#define user_procTaskQueueLen    1
os_event_t user_procTaskQueue[user_procTaskQueueLen];

void uart0_tx_buffer(uint8 *buf, uint16 len);

char rxbuff[31];
char rxindex = 0;

void UartReceive(char ByteReceived)
{
   os_printf("[%s] Got UART Callback!\r\n", __func__);
   os_printf("Byte Recevied: %c\r\n", ByteReceived);

   rxbuff[rxindex++] = ByteReceived;
   if (rxindex == sizeof(rxbuff)) 
   {
      rxindex = 0;
   }
   else
   {
      os_printf("%d != %d\r\n", rxindex, sizeof(rxbuff));
   }
   os_printf("Buffer: %s\r\n", rxbuff);
}

void ICACHE_FLASH_ATTR sdk_init_done_cb(void) 
{ 
   os_printf("[%s] initializing ESP8266!\n", __func__);
   while(true)
   {
      uart0_tx_buffer("test", 4);
      delay_second();
      delay_second();
   }
}

void ICACHE_FLASH_ATTR user_init()
{
   uart_init(BIT_RATE_115200, BIT_RATE_115200, &UartReceive); //This only seems to be kicked off via the COM-USB onboard, Not the TX/RX port
   //system_uart_swap(); //http://smallbits.marshall-tribe.net/blog/2016/11/13/esp8266-quiet-uart - Makes D7 RX and D8 TX
   system_init_done_cb(sdk_init_done_cb);

   wifi_set_opmode(0);
   wifi_set_sleep_type( NONE_SLEEP_T );

   ETS_GPIO_INTR_DISABLE();// Disable gpio interrupts
   gpio_init();   

   SetAllGPIOPinsAsOutput();

   /* Need to fix this to display value   */
   uint32 VDDADCByte[4] = {0};
   spi_flash_read(0x3fc06b, (uint32 *)&VDDADCByte, 1); //Read pads the other 3 bytes with FF

   os_printf("\r\n\r\nStarting ESP8266 OTA!\r\nSDK version:%s\r\nLoaded from: %02x\r\nVdd33_Const: %02x\r\n", system_get_sdk_version(), system_get_userbin_addr(), (VDDADCByte[0] & 0xff));
   //Turn off LED
   PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);
   gpio_output_set((1 << 2), 0, 0, 0);

   //Start os task
   system_init_done_cb(sdk_init_done_cb);
   //system_os_task(loop, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen); //Task to Signal for later
}

LOCAL void ICACHE_FLASH_ATTR uart_config(uint8 uart_no, void * callback(uint8))
{
   os_printf("uart_config:%d", uart_no);
   if (uart_no == UART1)
   {
      PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
   }
   else
   {
      /* UART0 */
      ETS_UART_INTR_ATTACH(uart0_rx_intr_handler,  callback);
      PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
      PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
      //Enable RxD pin
      //PIN_PULLUP_EN(PERIPHS_IO_MUX_U0RXD_U);
      //PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3);      
      //PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_U0RTS);
   }

   uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));

   if (uart_no == UART1)  
   {
      //UART 1 always 8 N 1
      WRITE_PERI_REG(UART_CONF0(uart_no), CALC_UARTMODE(EIGHT_BITS, NONE_BITS, ONE_STOP_BIT));
   }
   else
   {  
      //WRITE_PERI_REG(UART_CONF0(uart_no), CALC_UARTMODE(UartDev.data_bits, UartDev.parity, UartDev.stop_bits));
      WRITE_PERI_REG(UART_CONF0(uart_no), CALC_UARTMODE(UartDev.data_bits, NONE_BITS, UartDev.stop_bits)); //Override default settings
   }

   //clear rx and tx fifo,not ready
   SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
   CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);

   //set rx fifo trigger
   //  WRITE_PERI_REG(UART_CONF1(uart_no),
   //                 ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
   //                 ((96 & UART_TXFIFO_EMPTY_THRHD) << UART_TXFIFO_EMPTY_THRHD_S) |
   //                 UART_RX_FLOW_EN);
   if (uart_no == UART0)
   {
      //set rx fifo trigger to receive 0x01 byte for each call
      WRITE_PERI_REG(UART_CONF1(uart_no), ((0x01 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) | ((0x01 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) | UART_RX_FLOW_EN | (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S | UART_RX_TOUT_EN);
      //WRITE_PERI_REG(UART_CONF1(uart_no), ((0x01 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) | ((0x01 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) | UART_RX_FLOW_EN);
      //SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA | UART_FRM_ERR_INT_ENA); //This worked
      //Clear pending interrupts
      WRITE_PERI_REG(UART_INT_CLR(UART0), 0xffff);
      //enable rx_interrupt
      SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_RXFIFO_FULL_INT_ENA);
   }
   else
   {
      WRITE_PERI_REG(UART_CONF1(uart_no), ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S));
   }

   //clear all interrupt
   WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
   //enable rx_interrupt
   SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA);
}

One of the next steps I am taking is making the ESP8266 aware of other ESP8266 devices and being able to talk to them.
There is a couple way’s I can do this via UDP either with Broadcast or Multicast and I am still planning to figure out what would work best.
One thing I noticed is that as I enable IGMP I am also unable to continue sending RAW udp packets to the multitask group. Well after about an hour of Diag the issue I found that when my socket recieves a packet it update’s the remote PORT and address to reply back specifically to that client and does not revert the address to continue pointing back at the Multicast group 238.255.255.250 thus net new packets continue to send to the last used address.
I created the following code below to copy the address and restore it after the UDP packet is sent out, resolving the issue.

if (espconn_get_connection_info(udpconn,&premot,0) == ESPCONN_OK)
   {

      if(udpconn->link_cnt) {
         os_printf("EspConn %u HTTPUDP connections:\r\n", udpconn->link_cnt);
         int   i = 0;
         while(i < udpconn->link_cnt) {
            os_printf("%d) " IPSTR ":%u %s\r\n", i,  IP2STR(premot[i].remote_ip), premot[i].remote_port, msg_espconn_state[premot[i].state] );
            i++;
         };
      }

      //os_printf("Received device find message\n\r"); 
      //os_sprintf(DeviceBuffer, "%s" MACSTR " " IPSTR, pusrdata, MAC2STR(hwaddr), IP2STR(&ipconfig.ip));
      //os_printf("%s\n", DeviceBuffer);

      remot_info premotbackup;
      premotbackup.remote_port  = udpconn->proto.udp->remote_port; 
      premotbackup.remote_ip[0] = udpconn->proto.udp->remote_ip[0];
      premotbackup.remote_ip[1] = udpconn->proto.udp->remote_ip[1];
      premotbackup.remote_ip[2] = udpconn->proto.udp->remote_ip[2];
      premotbackup.remote_ip[3] = udpconn->proto.udp->remote_ip[3];

      unsigned short strlength = os_strlen(DeviceBuffer);
      udpconn->proto.udp->remote_port = premot->remote_port;
      udpconn->proto.udp->remote_ip[0] = premot->remote_ip[0];
      udpconn->proto.udp->remote_ip[1] = premot->remote_ip[1];
      udpconn->proto.udp->remote_ip[2] = premot->remote_ip[2];
      udpconn->proto.udp->remote_ip[3] = premot->remote_ip[3];

      espconn_sendto(udpconn, SSDPResponse, os_strlen(SSDPResponse)); //Send Back Answer

      udpconn->proto.udp->remote_port  = premotbackup.remote_port;
      udpconn->proto.udp->remote_ip[0] = premotbackup.remote_ip[0];
      udpconn->proto.udp->remote_ip[1] = premotbackup.remote_ip[1];
      udpconn->proto.udp->remote_ip[2] = premotbackup.remote_ip[2];
      udpconn->proto.udp->remote_ip[3] = premotbackup.remote_ip[3];

      //os_printf("SSDPResponse sent: %s\n\r", SSDPResponse); 
      //os_printf("SSDPResponse sent to %s\n\r", IP2STR(&premot->remote_ip));  //Cashes
   }

So as many of us have either heard or experienced ransomware in some way or another. I wanted to ensure that our databases .bak files were not getting encrypted by some 3rd party virus and if they were alert us so we don’t start poisoning our backup systems if we were not going to catch it in time to discover we’ve been compromised. Below is a .PS1 powershell script that I use to report on the status of our ‘.BAK’ files. If a virus were to encrypt the files PRTG would alert us on its next check interval.

$Dir = get-childitem "C:\DatabaseBackups\" -recurse
# $Dir |get-member
$List = $Dir | where {$_.extension -eq ".bak"}
#$List | format-table name
#$List | format-table fullname

foreach ($myitem in $List) {
    #Write-Host $myitem.fullname
    $bytes = Get-Content $myitem.fullname -Encoding byte -TotalCount 4
    #[System.Text.Encoding]::ASCII.GetString($bytes)

    Write-Host
    "<result>"
    "<channel>" + $myitem.fullname + "</channel>"
    "<value>" 
    if ([System.Text.Encoding]::ASCII.GetString($bytes) -eq "TAPE")
    {
        "Passed"
    }
    else
    {
        "Failed"
    }"</value>"
    "</result>"
}

Exit 0