?
Solved

problem executing sc command in powershell script

Posted on 2012-08-13
7
Medium Priority
?
3,559 Views
Last Modified: 2012-08-15
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
0
Comment
Question by:bibi92
  • 4
  • 2
7 Comments
 
LVL 13

Expert Comment

by:Xaelian
ID: 38290074
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
 

Author Comment

by:bibi92
ID: 38291056
Thanks but same error.
0
 
LVL 13

Expert Comment

by:Xaelian
ID: 38291270
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
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 
LVL 13

Accepted Solution

by:
Xaelian earned 2000 total points
ID: 38291553
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
 
LVL 12

Expert Comment

by:Carlo-Giuliani
ID: 38292653
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
 

Author Comment

by:bibi92
ID: 38292836
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
 
LVL 13

Expert Comment

by:Xaelian
ID: 38292861
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

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question