When we use 狗屎 (dog's excrement) to describe a thing, it is definitely negative. Sometimes it can also appear as a bad word.
Also, 踩到狗屎 is obviously a bad thing.
But, 狗屎运 (good fortune) is used positively.
What causes this difference?
3 Answers
狗屎运 is an accidental luck and some good things you don't expect suddenly come to you. It's fine to use it in informal situations, but it's still a vulgar term. Sometimes, it can be taken in a funny way, but you should be cautious when you use it in a formal situation.
I wouldn't call it commendatory -- It is more like a sarcastic way of describing the accidental luck. I would never say it as I congratulate my friends for winning the championship because I sincerely believe they deserve it. However, if I hear a classmate who does not have very good grades gets into Harvard, I would call this 狗屎运.
I think others have explained well, I don't have much to add on, but to provide a few examples:
兒子,你走了狗屎運. Jack,你走了狗屎運 (Joking with families and close friends)
小張走了狗屎運, 中了頭獎. (Envy, jealousy over a third person's luck)
他是什麼料. 成功就靠那點狗屎運. (Contempt)
在中国旧社会时期,农村化肥少,大都是用粪便发酵后做肥料的。但是人的粪便往往不足,所以就有了一种现象,就是早上起来捡狗屎。那时狗屎也能卖钱的,而捡了很多狗屎的人就能卖到比较多的钱。
but without a reference.