Unable to set windows service Runas from powershell - 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

Related

Install MSI on remote computer using Powershell

I am trying to write a Powershell script which will deploy software on a collection of WS2016 servers. I am a local administrator on all these servers. Here's what I have so far:
$Cred = Get-Credential
$Computer = 'myserver.contoso.com'
$SplunkMSI = '\\mylocalbox\C$\Splunk.msi'
$InstallDir = 'C:\Apps\Splunk\'
$sb = {
param($installer, $dir)
Start-Process -FilePath 'c:\windows\system32\msiexec.exe' -ArgumentList "$installer INSTALLDIR=$dir AGREETOLICENSE=Yes /qn /norestart /L*v C:\temp\splunkInstall.log" -Wait -NoNewWindow
}
Write-Host "Deploying Splunk to host $Computer"
Invoke-Command -Computer $Computer -Credential $Cred -ScriptBlock $sb -ArgumentList $SplunkMSI, $InstallDir -ErrorAction Stop
When I run this script, I get prompted for credentials, and then I see the output of the Write-Host, but then... nothing. I have to manually terminate the script.
I logged onto the remote host, but see no evidence that the MSI was executed or failed to execute.
Anyone see a smoking gun?
ya, it looks like it's looking for $installer and $dir in the script block but they're not specified

Powershell script replace sc config

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}

Using Invoke-Command to run Start-Process in an elevated session

As a precursor to running an installation file on several remote servers, I need to update the Powershell setting MaxMemoryPerShellMB. This requires running a PS session as Administrator on the remote server. I have been trying to run Invoke-Command which then runs a ScriptBlock consisting of a Start-Process command which includes the -Verb RunAs parameter. Nothing seems to work, however.
I have tried with various quoting schemes, single, double, triple, but nothing seems to work.
I've tried running the Start-Process from an Enter-PSSession, with the same results.
Following is the code I'm testing now:
$creds = Get-Credential -Username 'DOMAIN\userID' -Message "Enter Username and Password to access the remote servers."
$ScriptBlock = {
Start-Process -FilePath Powershell.exe -ArgumentList """Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024""" -Verb RunAs -Wait
}
Invoke-Command -ComputerName testsvr01 -Credential $creds -ScriptBlock $ScriptBlock
I should be able to RDP to the remote server and run Get-Item WSMan:\localhost\Shell and have it show the updated value, but the value isn't changed.
When running the code it pauses for a second when the Invoke-Command runs, but other than that, there is no feedback in Powershell.
On the remote server I see the following two Kerberos errors in the System Event log.
0x19 KDC_ERR_PREAUTH_REQUIRED,
0xd KDC_ERR_BADOPTION
Any help is greatly appreciated.
> powershell.exe -?
...
EXAMPLES
...
PowerShell -Command "& {Get-EventLog -LogName security}"
-Command
...
To write a string that runs a Windows PowerShell command, use the format:
"& {<command>}"
where the quotation marks indicate a string and the invoke operator (&)
causes the command to be executed.
So you could try to call Set-Item in the following way:
$ScriptBlock = {
Start-Process -FilePath Powershell.exe -ArgumentList "-Command"," &{ Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024 }" -Verb RunAs -Wait -PassThru
}
$process = Invoke-Command -ComputerName testsvr01 -Credential $creds -ScriptBlock $ScriptBlock
$process.ExitCode
I'm also returning a process object via -PassThru on which you might check the `ExitCode``
Hope that helps

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

powershell execute start-process on remote machine

IDE: PowerShell ISE
I have a script which installs the chrome to a remote machine
#Script : Test01.ps1
Start-Process D:\Chrome\Chrome.exe -wait -verb runas
write-host "Chrome is installed"
and I am executing the above script using :
Invoke-Command -ComputerName MySystem18 -FilePath D:\Test01.ps1 -ArgumentList Process
The above script is working on local system (MySystem03) and remote machine (MySystem18).
But when I am executing this in MySystem18 it is not showing up the acknowledgement or cursor after the installation of chrome, even after successful installation of chrome on MySystem18.
Can you tell me how to fix it.
D:\Chrome\Chrome.exe is a path on your remote computer ? If not, try to share a dir with Chrome.exe named yourdirshared on your mysystem03 and replace by \\mysystem03\yourdirshared\Chrome.exe into your ps1
if always doesnt work, you can try to remove -wait my be...
if always doesnt work, are you try to specify credentil parameter?
$Username = 'username'
$Password = 'yourpassword
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName "MySystem18" -Credential $Cred -ScriptBlock {Start-Process D:\Chrome\Chrome.exe -wait -verb runas; write-host "Chrome is installed" }