There are degrees of immorality, and in my view, intent is important.
I agree intent is important. In the example given, Spock performs what he knows to be pain against a person's will for the sake of getting what he believes to be information which will save lives.
The question is, was he justified? Was it right for him to hurt another person to save lives?
I'm not sure if you're familiar with a particular political event that happened in mid 2018, but there was a supreme court nominee (chosen by Trump) up for review by congress. His name was Brett Kavanaugh. It was in the news a fair bit. Brett had a fairly well documented history of sexual assault, but an official, in-depth investigation into that history was stifled at the time by the people who were in control of such processes. Ya see, Brett is a republican and as such he represents a pro-life vote on the supreme court. Getting him on the bench would represent a huge victory; only a sitting president can nominate a candidate, they are life-time appointments, and no one (not even a president) can fire them after they are appointed.
For this reason, a slew of professing Christians came out in support of Brett, saying that "boys will be boys", that the alleged incident(s) happened too long ago to still be relevant, that people should be allowed to move on from their past, etc. It all had the ring of legitimacy to it, except that Brett denied everything. He said he had nothing to move on from. The issue became so polarized that a woman who supported Trump (remember, Trump nominated the guy) claimed that there is nothing wrong with a bit of groping anyway. She said this on live, national television with her two teenage daughters standing beside her. She even asked them to verbally commit to her expression of political loyalty by also confirming that they saw nothing wrong with a bit of groping. Mind you, the accusation against him was not groping, but rather attempted rape. Political expediency practically dripped from this situation.
This woman, and many like her, were prepared to defend this man (despite tacit acknowledgment that he was guilty) because of his pro-life vote on the supreme court. In their minds, all the millions and millions of saved babies could not compare to his few sexual assault allegations even if he really was guilty of all of them. They were quite prepared to see any dozen (or perhaps even hundreds) of women assaulted if it meant a reversal of current abortion law.
These people had convinced themselves that a certain amount of immoral behavior, a certain amount of sexual assault, a certain amount of rape was justified
if it meant saving lives in some other area. In their minds, neither they nor Brett tortured anyone; the sexual assault was just an unfortunate bit of baggage they needed to accept for a greater good.
Relating this back to the OP hypothetical, all Spock had to do was mentally assault this woman in the deepest places of her mind (in a way which was clearly unwanted and very painful for her) and they could save who-knows-how-many lives.
This is the problem when it comes to playing around with moral technicalities for the sake of a greater good. When we refer to the hurt we cause to others as just a side-effect for the greater good, we have ceased to genuinely consider the most important intent of all; our own.