Running powershell script fails on windows server salve - powershell

I have the below code which runs from Jenkins on windows server 2019 slave:
$Username = $args[0]
$Password = $args[1]
$Env = $args[2]
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
echo "******** Start SQL Deploy ********"
Start-Process -FilePath msbuild -ArgumentList '"Database Services\Database Services.sqlproj"','/t:deploy', '/p:Configuration=Release', '/p:TargetConnectionString=$Env', '/p:BlockOnPossibleDataLoss=True', '/p:TargetDatabase="test_fsdb"','-fl','-flp:logfile=msbuild.log' -wait -LoadUserProfile -credential $cred
Get-Content msbuild.log
echo "******** End SQL Deploy ********"
The parameters comes form Jenkinsfile. I'm using applicative user.
The error is:
Start-Process : This command cannot be run due to the error: The service cannot be started, either because it is
disabled or because it has no enabled devices associated with it.
I was able to run it locally, so I wonder if it's kind of permission issue on the salve...

Related

powershell run command powershell to all logging users in server

how I can run this command in powershell to all users in the server?
powerShell -sta -file 'E:\Script-Popup\Popup-Message - Done.ps1'
that on every user who is currently connected to the server, this command will run.
Option 1:
Get connected users:
query user /server:SERVERNAME
For each user run:
$username = "username"
$password = "password"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username,
$securePassword
Start-Process powershell.exe -Credential $credential -ArgumentList "-file
$script"
Option 2:
Configure a logon script using a GPO (the script will run when the users login)
You don't need PowerShell for that (but you can use it with PowerShell)
msg * 'This will be shown to all users as a pop up'
MS Learn - Terminal Server commands: MSG

never ending Powershell script when executed from SQL Server Agent

My PowerShell script is unable to create a PS-Session when executed through SQL Server Agent Job,
On the other hand when I tried to execute the script from powershell editor it is running fine.
I tried to catch output into a text file which shows script is executing fine until-------
echo "Credentials received" >> C:\Users\username\Desktop\ABC.txt
#Set Execution policy for the first run
#Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
#This is better for scheduled jobs
$User = "xyz#abc.com"
$pass = "0100skdhfsdhlfsj7eb0100000044afdcf1458d41449af94347f8d9d962000012464664603660000c000000010000000cb11e38eb73dd5d612111f4461d953e90000000004800000a00000001000000085512ba758402a4c5f4504af6f407ba3180000003bf98f386b5341815358f54166f931802814a6154f86daf8140000009a3c5af0588baa420ed836dc0ac1d1f4d0fb4649"
$PWord = ConvertTo-SecureString -string $pass
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
echo "Credentials received" >> C:\Users\username\Desktop\ABC.txt
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
echo "Session" >> C:\Users\username\Desktop\ABC.txt
Import-PSSession $Session -AllowClobber
echo "Session set up" >> C:\Users\username\Desktop\ABC.txt
Remove-PSSession -Id $Session.Id
exit
When I run the same piece of code from powershell editor, it is getting executed correctly.
I am the expecting same behavior when it is scheduled from SQL Agent.
As encryption is user specific we should create a proxy account at sql agent jobs for a user with whom password is encrypted. And then run the job with particular proxy. It works.
Encryption Technique used:
$password = read-host -prompt "Enter Password"
write-host "$password is: "
$secure = ConvertTo-SecureString $password -force -asPlainText
$bytes = ConvertFrom-SecureString $secure
#Encrypted Password
echo $bytes
Note:- enter code here Above mentioned technique is user and system specific.
Please encrypt the password on the same machine from where you will be running the script in which you will be using the password.

PowerShell Script fails to execute batch file on Remote server

I have a PowerShell script which works fine on windows server 2016 azure VM but fails to execute the same script from my build agent which is also window server 2016 OS azure VM.
No errors get logged in PowerShell due to which i am not able to figure out what is the reasons?
Is there any Prerequisites that i need to validate or install on the server for executing this script?
Below is the script which execute batch file present on another another VM.
$Username = 'ABC'
$Password = 'XYZ'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
try {
Invoke-Command -ComputerName "ServerName" -credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\CI\Demo_CI.bat'"
Write-Host "done"
}
} catch {
Write-Host "error"
}
I believe what you are facing here is a Credential delegation issue, You can try enabling CredSSP in your build agent and the target "ServerName". To know more about credssp , see here, therwise you will have to use psexec in CI.

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" }

Need to connect to remote server and execute ps1 script using jenkins

I created a Jenkins job that needs to connect to a remote machine and execute a ps1 script.
$pw = convertto-securestring -AsPlainText -Force -String "4444"
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "eeeee\eee",$pw
$sess = New-PSSession -ComputerName server1 -Credential $cred
Enter-PSSession $sess
Looks like you may be better served with Invoke-Command than Enter-PSSession.
https://technet.microsoft.com/en-us/library/hh849719.aspx
Something along the lines of:
Invoke-Command -Credential $creds -ComputerName $server -FilePath $scriptname -ArgumentList $arg1, $arg2
Add the remote machine as a slave, then run your script from 'execute windows Batch command' or install Powershell plugin and 'Run powershell script'.
Links:
Add windows slave: https://wiki.jenkins.io/display/JENKINS/Step+by+step+guide+to+set+up+master+and+agent+machines+on+Windows
Powershell plugin: https://wiki.jenkins-ci.org/display/JENKINS/PowerShell+Plugin