problem executing sc command in powershell script

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
bibi92Asked:
Who is Participating?
 
XaelianConnect With a Mentor 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'
0
 
XaelianCommented:
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
0
 
bibi92Author Commented:
Thanks but same error.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
XaelianCommented:
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'
0
 
Carlo-GiulianiCommented:
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.
0
 
bibi92Author 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
0
 
XaelianCommented:
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 :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.