start from the prime power factorisation: $\displaystyle n = 1 \times p_2 ^ {k_2} \times p_3 ^ {k_3} ... p_j^{k_j} = 1 \times \prod_{i=2}^{j} p_i^{k_i}$
The above product expression only includes primes above 1 to avoid double counting on the next step.
By definition any part of the above product is a divisor of n. The full set of divisors are formed by varying the powers of each prime factor (other than 1) between 0 and $\displaystyle p_i$, ie the divisors are:
$\displaystyle p_2^{x_2}p_3^{x_3}....p_j^{x_j} ~~~~ 0 \leq x_i \leq k_i $
The total number of divisors that can be constructed by varying those powers is $\displaystyle D = \prod_{i=2}^{j} (k_i + 1)$. The construction includes all possible divisors since the prime factorisation of n is unique. No divisor is constructed twice since the prime factorisation of each divisor is unique. So D is the correct total of unique divisors. NB: the total (D) counts both n and 1 as divisors of n. If you dont consider those "real" divisors then you can subtract 1 or 2 from the total.
Getting the number of square divisors is a simple extension. Define $\displaystyle m_i = \lfloor \frac{k_i}{2} \rfloor$
A number is square iff its prime factorsation has even powers, so the possible square divisors are
$\displaystyle p_2^{2x_2}p_3^{2x_3}....p_j^{2x_j} ~~~~ 0 \leq x_i \leq m_i $
Anything constructed in this way is a divisor of n since $\displaystyle 2x_i \leq k_i$ . Using the same arguments as before the total number of square divisors is $\displaystyle S = \prod_{i=2}^{j} (m_i + 1)$
Now it's just a probability problem. We want P(square then non-square) + P(non-square then square).
$\displaystyle =\frac{S}{D} \cdot \left(1-\frac{S-1}{D-1} \right) + \left(1-\frac{S}{D}\right) \cdot \frac{S}{D-1}$