Score:0

Powershell: creating functions (or aliases) for system commands

mv flag

I am trying to make it easier to run certain things with certain users from powershell.

So first I calculate username and domain needed to run a specific command, which is no problem.

But then I want to be able to run a specific command with the calculated credentials, and this fails. I have already tried the following variants:

# this does not work yet (the command works, but the alias/function doesn't
# function aduc {"runas /user:$currentUserDomain\$adminUserNameTier0 dsa.msc"}
# function aduc {& "runas /user:$currentUserDomain\$adminUserNameTier0 dsa.msc"}
# function aduc {Invoke-Command -NoNewScope -ScriptBlock {runas /user:$currentUserDomain\$adminUserNameTier0 dsa.msc}}
# function aduc {Invoke-Command -NoNewScope -ScriptBlock {& "runas /user:$currentUserDomain\$adminUserNameTier0 dsa.msc"}}

Whenever I call "aduc", I get:

aduc : The term 'runas /user:DOMAIN\USER dsa.msc' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1

(I put DOMAIN\USER in the error message for data protection, of course in reality we are using real, existing values)

Any idea how to get this to work?

Score:0
mv flag

After further experimentation I found this works:

$currentUserDomain = "contoso.com"
$adminUserNameTier0 = "contosodomainadmin"
function aduc {Invoke-Command -NoNewScope -ScriptBlock {runas /netonly /user:$currentUserDomain\$adminUserNameTier0 "mmc dsa.msc"}}

Now simply calling "aduc" will run the dsa ("Active Directory Users and Computers") mcc console as contoso\contosodomainadmin, asking for the password.

Question can be closed.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.