Invariant operators for quadratic Hamiltonians