Powershell script to copy publish files from build machine to posting server - powershell

I am designing TFS build definition to build solution on a build machine and publish files to posting server. I need to copy those publish files directly from build machine to posting server (which is a shared drive that i need to login using credentials).
Till now, we are posting the build results to a shared drive. I have used powershell commands to do this. From there, we are manually updating the files in posting server. Now i am trying to post directly to posting server rather than involving the manual process.
I need a powershell script to
Login to posting server
Delete the specific files in the posting server
Copy publish files from build machine to posting server
Logoff from posting server after successful copy
I have got the script to everything except including user credentials to login to the destinationpath. I am trying to find some arguments to include login username and password for this destination path, but could not found anything appropriate.
Can this be done using MS Build Arguments instead of script?

Copy a file to a remote computer:
$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul"
Copy-Item "D:\Folder001\test.log" -Destination "C:\Folder001_Copy\" -ToSession $Session
Once you type a user name, you will be prompted for a password.

Related

Powershell: Remotely Unzip from file-share using invoke-command

I am trying to unzip a file remotely. The file-share the ZIP is on uses a different credential
This is the command I ran.
Invoke-Command -Computername $compname -ScriptBlock
{
Expand-Archive -Path "\\share\somefolder\myfile.zip" -DestinationPath "\\share\somefolder"
}
I get an error stating username/password incorrect for accessing the share.
The problem is, the account I use to access $compname is not the same account I use to access the share the zip is on. I don't have a common account that can access both.
Incidentally, running the expand-archive by taking RDP to $compname works because it uses the saved credentials from the credential manager. Invoke-command on the other hand seems to be trying to access the share with the credentials used to connect to the remote computer.
Things I have tried
Mapping a network drive beforehand, but that doesn't get
recognised as a valid path when accessed via invoke-command.
Mapping a network drive on the fly inside the invoke-command
option, and then using that mapped drive with expand-archive. This
showed the most promise as I could get it working via
enter-pssession, but of course this won't work in a script am trying to automate and when
I created a new session via new-pssession and passing that to
invoke-command, it again failed to work.
Does anyone have suggestions on a possible approach? I might revisit option #2 to see if I have made a mistake somewhere.

Running a PowerShell script remotely

So I have a script sitting on a server that I can remote into using Enter-PSSession and run the script from my local environment just fine. What I do is...
Enter-PSSession hostname
cd c:\temp\myscript.ps1
or
$s = New-PSSession -ComputerName hostname
Invoke-Command -Session $s -ScriptBlock {C:\temp\myscript.ps1}
these run the script just fine. The problem I am having is the data is supposed to get sent to a database. If I run the .ps1 on the hostname by RDPing directly into it, it works. If I RDP into the server and go back to my local environment where I Enter-PSSession using the code above, it submits to the database just fine. But when I close out of the RDP session and just try running the script again locally, it doesn't go to the database.
Maybe it is something stupid I am missing but any help would be greatly appreciated.
The important bits are in the myscripts.ps1, so it's impossible to say from what you have provided.
I suggest you check that script file for where the credentials are stored. When you run it locally, I assume you run it from that working directory.
Try using $psexecscript as the root of the config file's path. That's whatever path the script is in, assuming the credentials are in the same or sub- directory.
Is it possible that the user lacks permission to write in your DB or the credentials to write in that DB are not provided in the script?
If after verifying the credential issue, it still doesn't work, you might want to register your script as a task. You can set it as a manual task. Then, whenever you need it, you (remotely) run that task. In that way your credentials are stored as well and you can safely use the 'run as' feature.

Install application on remote server using local installer

I have an installation file (*.msi) which installs a service. I would like to install this service on a remote server.
Doing that via Remote Desktop is not a problem, but I cannot find a solution for doing that automatically using a batch file or a PowerShell command that would install application remotely using the local file.
What is the problem:
I have a Bamboo CI server that results in the installation file. I'd like to install the application on a remote server using the script, without copying the installation file (similar way to deploying application using the Ms Web Deploy).
Is it possible to do this in that way?
So far I have tried installutil.exe (works locally, but I cannot configure it to install it remotely), ServiceConsole, but it somehow doesn't install correctly application using create command and I can't overwrite the application.
i do that before
first download msi or exe command with invoke-webrequest like
invoke-webrequest http://mysiteapp -outfile c:\myapp.msi
then
you can install app with msiexec or some app can install like this
myapp.msi /quite
if you have telnet from server you can make script and then run...
if you have not access telnet you should use invoke-command or psexec
this link can help you
The executable to use when installing .msi files is msiexec.exe; you can call that remotely using PowerShell's Invoke-Command cmdlet, and pass it the remote path to the .msi file as a parameter (look up the msiexec.exe syntax for details).
The trick will be getting access to the .msi file on the local server from the remote PowerShell session. You will almost certainly need to create the session using CredSSP authentication, so that the remote PowerShell session created by Invoke-Command will be able to make a connection back to your local server and access the .msi file.
I hope this sets you on the right path.

How to copy the files from local system to the remote server by using powershell script

I'm running the command which i have shown you the image and its giving that error
Please suggest me on this
Actually am trying to copy the build from local system to the remote server by using powershell scirpt
and i want to copy the files from on remote server to the other remote server by using local system
please provide me solutions for this and what are the requirements for this
You can use the copy-item
copy-item -path "Your\path\to\folderorfile" -destination "\\servernameorip\dest\location\"
if you have different credentials on the destination server, you can either trick windows by creating a local account using same name and password on your mcopy with different credentials

Powershell, remote script access denied to network resources

I am trying to execute powershell script remotely using invoke-command. The script relies on a configuration file which is available over the local network. The script is called in a following way:
Invoke-Command -ComputerName 192.168.137.181 -FilePath c:\scripts\script.ps1 -ArgumentList \\192.168.137.1\share\config.xml
The configuration as you can see is an xml file and it's loaded using:
$xml = New-Object XML
$xml.Load(args[0])
When the script is called locally on the machine then it runs witout any problems and reads the configuration file. However when I run it from different machine using invoke command I get
"Access to the path '\\192.168.137.1\share\config.xml' is denied"
exception, which is thrown when executing Load method.
The file is accessible to everyone with read and write permissions.
Both, machine on which the scrip should be run (.181) and the machine on which it is run physically have the same credentials, thus I do not pass them in invoke-command cmdlet.
The share machine (.1) has different credential, but this was never an issue when calling the script locally from .181.
Can you please point me in the right direction? I stuck on this step and can't find solution by myself. I tried downloading the xml string using WebClient#DownloadString method and passing credentials for the share machine but it did not help.
Thanks in advance
It is probably a double hop issue. You have to use CredSSP to delegate your credentials to the remote computer.
Try the solution mentioned here: http://blogs.msdn.com/b/clustering/archive/2009/06/25/9803001.aspx