Jenkins and PowerShell - powershell

I have a linux machine with Jenkins installed on it. It executes variety of different jobs on other linux machines.
Now - I would like to connect to a Windows machine and execute a PS script. Is it possible to do that from the Linux Jenkins by installing PS plugin (note that I do not want to execute a PS script on a local linux machine but execute all that on remote hosts)? If not, what is the proper way of doing that?

Yes you can run jobs on slave windows machine with Linux master.
Add windows machine as a new node(slave), give it a distinct label. The following link can help you in adding new slaves (https://wiki.jenkins-ci.org/display/JENKINS/Step+by+step+guide+to+set+up+master+and+slave+machines)
Go to job where you want to run PS script on windows machine and check the option "Restrict where this project can be run". Enter name of your windows machine label here.
To execute all jobs on remote machines, you can give all of them a separate label and use the step 2 to only use them for your jobs.
To execute job on local host you can enter "master" in the restrict run editbox.

Related

Is it possible to run Copy-VMFile powershell command as non-admin?

I have a host machine with Hyper-V enabled, and I have a Hyper-V guest running on this machine. Becauze I can't drag&drop files to the VM, I wrote a script to do this for me, which is integrated in TotalCommander, and which uses Copy-VMFile.
My problem is that the PS script works only if I run it as admin, otherwise I get the error "You do not have the required permission to complete this task."
Why do I need admin to copy from the host machine to the guest OS? And is there a way, some policy which would allow me to circumvent this? It is annoying to enter a password every time I want to copy some files.

Remote-SSH not finding the agent

I'm trying to get Remote-SSH working with ssh-agent, from Windows 7 desktop to CentOs 7 server.
I'm using ssh, ssh-add and ssh-agent from the Git for windows package. From the cmd prompt, this is all working fine, I can "ssh-add -l" and see the keys, and I can run "ssh " and it runs with no problems and without asking for password:
C:\Users\gnb>"c:\Program Files\Git\usr\bin\ssh-add.exe" -l
4096 SHA256:zg2IR6OlPwCGP8SzcbriXIQjth5zuDc9rbO6uaNPmcU gnb#VDI028-MEL (RSA)
C:\Users\gnb>ssh vdi ls
Desktop
tsclient
wkspace
C:\Users\gnb>
From within VS-Code, I can't get this to work. Running the exact same ssh-add command from within the VS-Code Terminal does not seem to find the agent:
C:\Users\gnb>"c:\Program Files\Git\usr\bin\ssh-add.exe" -l
Could not open a connection to your authentication agent.
The actual remote functionality in VS-Code more or less works, but keeps stopping to ask for passwords.
What's missing here? Why can the vs-code environment not connect to the ssh-agent?
OK, I've had a few more hours playing around with this and I think I understand what's going on.
It only works from cmd if it is the same cmd window that was used to start the agent. Opening a new cmd window then ssh gets the same msg as starting vs-code from the menu - can't find the agent. It appears that the start-ssh-agent script that comes with git/ssh on Win7 is setting some environment variables or some such that the ssh client needs. This does make sense, Unix ssh-agent acts the same way, but I'm clearly not used to thinking about windows apps in those terms.
It also seems the start-ssg-agent script will set the environment variables to point to an existing agent if one is running, else will create a new agent. So a 2 line batch file
start-ssh-agent
code
will reliably start up VS Code with the ssh-agent. If you name the identity file in the ssh config, vs-code will add they key to the agent when required. Otherwise you need to manually add the key to the agent, or fall back to entering the password all the time.
Suspect the better solution is to be on Win10 and use ssh-agent as a service, which should mean VS-code should find the agent when run from a menu. But I can't test that.

winrm or requests is not installed: No module named winrm

I have a small simple setup consisting of Jenkins & Ansible 2.7.8 running on Ubuntu 18.04.2 LTS (192.168.0.202):
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/jon/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
Jenkins ver. 2.150.3
I then have a Windows VM (192.168.0.203) that has a Powershell script stored on it and an Ansible playbook configured to connect to the Windows VM and run the Powershell script.
When I run the Ansible-Playbook directly from the command line it works fine, connects to the Windows machine and runs the script. All good.
I am having real trouble though implementing Ansible into Jenkins to run the playbook. When I run the playbook through Jenkins, I get the following error:
Building in workspace /var/lib/jenkins/workspace/Ansible-RunPS-1.0
[Ansible-RunPS-1.0] $ /usr/bin/ansible-playbook //etc/ansible/runPS.yml -f 5
PLAY [Runs remote PS script] ***************************************************
TASK [Gathering Facts] *********************************************************
[0;31mfatal: [192.168.0.203]: FAILED! => {"msg": "winrm or requests is not installed: No module named winrm"}[0m
To me, it seems to be running the playbook but then failing as it cannot find the winrm module. Could it be something to do with the account that Jenkins uses can't somehow find the winrm module, yet if I run the same command under my account, it finds it ok ?
Happy to post other configs etc. if that would help but thought I'd try and keep it as simple as possible to begin with.
As is the way with these things, you spend days trying to fix it, then as soon as you ask for help you manage to fix it.
I'd love to be able to say it was just a case of installing this or running that but in truth, I really don't know what got it going, it suddenly burst into life after a reboot, can't believe that's all it was but you never know!

how to run a bat or PS1 file while VM's booting

I have a script to assign static IP address to HyperV VM's whenever it found 169.x.x.x series IP,and the script start work when we logged in to the machine as Administrator.
Is it possible to run the script while machine booting ? So that, we don't need to log in to the VM's to run the script ?
You don't give us any information about your deployment process. But you could essentially use the SetupComplete.cmd. Run your PowerShell script from there.
See Add a Custom Script to Windows Setup.

Execute remote PowerShell script from VSOnline fails - Access Denied

I am facing a very strange issue trying to run a Remote PowerShell script from the new build system of Visual Studio Online.
Context:
I have a Build server that I host on my own VM. I have also created a second VM where I want the PowerShell script to be run on.
When I run a build from the Build server, I want to create a task that run a PowerShell script on a remote machine. Sounds easy.
Both machines have everything configured to run the Remote PowerShell. (I think)
Problem:
If I manually run the script from the build server, the script is properly executed on my remote machine.
But, if I run the script by starting a new build, it doesn't work, I always get an error message
New-PSSession : [xxx.cloudapp.net] Connecting to remote server xxx.cloudapp.net failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
Have you faced this issue before with VSOnline?
This can happen if there isn't a trust relationship between the remote machine and the build machine. E.g. you could look at the hosts.equiv file on the remote machine and make sure it trusts the build machine.
If that doesn't help then more info might be needed, like how you're trying to sign in, what build agent account you're running as.
Guy