Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

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
Avatar of Xaelian
Xaelian
Flag of Belgium image

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
Avatar of bibi92

ASKER

Thanks but same error.
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'
ASKER CERTIFIED SOLUTION
Avatar of Xaelian
Xaelian
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of bibi92

ASKER

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
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 :)