Powershell script replace sc config - powershell

Good Day,
i look for a same command as
sc config BITS type= own in Powershell.
I think ist with
set-service -name BITS -computername server1234 -idon´t know
I have the problem that my INVOKE-COMMAND don´t work.
But all scripts work only this line don´t work.
Invoke-Command -ComputerName $serverneedhelp -ScriptBlock {Start-Process cmd -ArgumentList "/c 'sc config bits type= own'"}
If anybody can help me, it is were so great.
Thankyou
Greeting
Jan

Run it directly?
Invoke-Command $serverneedhelp { sc config bits type= own }
The other alternative is with wmi: Configure service with Set-Service

in my script work it with
Invoke-Command -ComputerName $serverneedhelp -ScriptBlock {Start-Process cmd -ArgumentList "/c sc config bits type= own"}
But I search a version of Powershell without cmd

I know that this is old thread, but maybe someone would benefit from this.
What worked for me is:
Invoke-Command -ComputerName $serverneedhelp -ScriptBlock {& SC.exe config "bits" type= own}

Related

Unable to set windows service Runas from powershell

I want to set the windows service to run as a particular user(admin). This is my first attempt.
$myArgs = 'config "service22" start= demand obj= "'+$machineName+'\'+$userName+'" password= "'+$userPassword+'"'
Start-Process -FilePath sc.exe -ArgumentList $myArgs
I also tried this by changing the position of the variable many times(all places tried)
$svc = Get-WmiObject win32_service -filter "name='service22'"
$svc.change($null,$null,$null,$null,$null,$null,$null,$null,$null,$userName,$userPassword)
Nothing works with scripts but manually I could set it.
No need to overcomplicate this.
Just:
Invoke-Command -ComputerName computername -ScriptBlock {
Set-Service -Name servicename -Credential (Get-Credential user#domain.com)
}
PS: Set-Service have Credential parameter in powershell v7 (tested), non in Windows Powershell
got the tip from here
just had to add type=own at the end
$myArgs = 'config "service22" start= demand obj= "'+$machineName+'\'+$userName+'" password= "'+$userPassword+'" type=own'
Start-Process -FilePath sc.exe -ArgumentList $myArgs

How do I give Invoke-Command more time to run another script

Im trying to run a local script to run a server script:
Invoke-Command -ComputerName ServerName -Credential MyCredentials {Start-Process "C:\Program Files\test.new.cmd"}
So here comes the problem, it runs the Script, but not for long enough. It generates me only one file and the server script needs around 6 seconds to rule everything.
What I have to change to let it run for longer time?
Start-Process can wait for the command you are running by using the -Wait parameter. So you should use:
Invoke-Command -ComputerName ServerName -Credential MyCredentials {Start-Process "C:\Program Files\test.new.cmd" -Wait }

PowerShell run bat from location on my computer on remote computer

I need to run a BAT file on some servers
Because of the amount of many servers I avoid copying the file on any server
I have the file on my computer and I try to access it remotely
$CMDCOMMAND = "\\Mycomp\c$\file.bat"
Invoke-Command -ComputerName $Hostname -Credential $Cred -ScriptBlock {
Start-Process cmd.exe "/c $CMDCOMMAND
}
Invoke-Command -Computer $Hostname -Credential $Cred -FilePath $CMDCOMMAND
should work as #Ansgar Wiechers mentioned.
I doubt you have set $CMDCOMMAND = "\\Mycomp\c$\file.bat" and tried. It should be local path when you pass it to -FilePath
$CMDCOMMAND = "c:\file.bat"
Invoke-Command does not work with a variable defined outside the ScriptBlock. You need to use the ArgumentList parameter. Something like this should work, although I have not tested it:
$CMDCOMMAND = "\\Mycomp\c$\file.bat"
Invoke-Command -ComputerName $Hostname -Credential $Cred -ScriptBlock {
param($ARGLIST) Start-Process cmd.exe "/c $ARGLIST"
} -ArgumentList $CMDCOMMAND

Executing batch file in the remote machines using powershell as a background job

All I am trying to do is to execute a batch file in remote machines as a job.I have batch file located in all machines inside C:\Users\temp folder.Here is my code
$batFile = "test.bat"
foreach($m in $machine)
{
Invoke-Command -ComputerName $m -ScriptBlock{param($batFile) & cmd.exe /c "C:\Users\temp\$batFile"} -Authentication negotiate -Credential $cred -ArgumentList $batFile -AsJob
}
But I keep getting
The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script
block, or a CommandInfo object
I tried using $using:batFile inside ScriptBlock as well with no success. Can anyone suggest me what I might be doing wrong? I am using powershell version 4.
do a trace on that,
Invoke-Command -ComputerName $m -ScriptBlock{param($batFile) Trace-Command NativeCommandParameterBinder -Expression { & cmd.exe /c "C:\Users\temp\$batFile"}} -Authentication negotiate -Credential $cred -ArgumentList $batFile -AsJob
and Try using Invoke-Expression as a workaround instead of &
invoke-Expression "cmd.exe /c 'C:\Users\temp\$batFile'"
Regards,
Kvprasoon

Error invoking command to install a Msi through Powershell

I am trying to use PowerShell's Invoke-Command but I am encountering an error that I have no idea of what it is!
Would be great to have some help on this. I am sure that it must be something really simple..
invoke-command -scriptblock{ $executable = "wmic"; & "$executable product call install true","-computername name" ,'path to the msi' }
Thank you!
You could just try using msiexec:
$scriptblock = {Start-Process msiexec.exe -Argumentlist "/i $PathToMSI","/qn"}
invoke-command -scriptblock $scriptblock -computername $name
I´m not sure if you even can install msi´s via wmi, never seen it before. Other than that you mixed up the syntax of your invoke-command a bit ;)