We help IT Professionals succeed at work.

problem executing sc command in powershell script

bibi92
bibi92 asked
on
Hello,

I try to execute this following command in ps script :

$ComputerName = (get-WmiObject win32_computersystem).Name
$server = "\\" + $computername
$U = $env:USERNAME
sc $server  config sshd type= own
sc $server config sshd obj= $U password= $env:password

This error is returned

Set-Content : A positional parameter cannot be found that accepts argument 'sshd'.
At line:1 char:3
+ sc <<<<  $server config sshd obj= $U password= $env:password
    + CategoryInfo          : InvalidArgument: (:) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetContentCommand

How can I resolve this problem?

Thanks

bibi
Comment
Watch Question

Commented:
Try:

$ComputerName= (get-WmiObject win32_computersystem).Name 
$server= "\\" + $ComputerName
$U= $env:USERNAME
sc $server config sshd type= ownsc $server config sshd obj= $U password= $env:password

Author

Commented:
Thanks but same error.

Commented:
Hi,

Use the following:

$ComputerName= (get-WmiObject win32_computersystem).Name
$server= "\\" + $ComputerName
$U= $env:USERNAME
sc.exe $server config sshd type= own obj= '$U' password= '$env:password'
Commented:
This worked for me. Pay attention. $env:USERNAME won't work if you execute it in a domain.

it will give sc.exe $server config sshd type= own obj= USERNAME password= '$env:password'

But that's only for a local user.

For a domain user he will ask for

sc.exe $server config sshd type= own obj= DOMAIN\USERNAME password= '$env:password'
To add to the response from Xaelian, your problem is that "sc" is an alias for the Set-Content cmdlet.  By using sc.exe, you will be executing the correct command.

Author

Commented:
Hello,

Thanks, I try this :
sc.exe $server config sshd type= own obj= DOMAIN\USERNAME password= '$env:password'
But the sc doesn't start due to a logon failure.
There is a problem with $env:password.
How can I catch the $env:password from this following bash command before sc command :
bash --login -c "/bin/ssh-host-config -y -c ntsec -u $U -w $env:password"

Thanks

bibi

Commented:
Well can you echo the $env:password? In your bash file?

Because password are handled really secure in every OS. If you can echo it, you can put it into a variable. If you can't echo it, you can't use it.

It would be very insecure if everyone can get the password so easy :)