MSDeploy Unauthorized Error - command-line

I am calling msdeploy using the command line to deploy a web deploy package (zip) to the same machine that I am running the command from.
Here is the command line:
msdeploy.exe -verb:sync -source:package="<pathToZip>" -dest:auto,computerName="<hostName>",includeAcls="False",authType=Basic,userName="<myUserName>",password="<myPassword>" -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:"<pathToSetParametersXml>" -allowUntrusted -preSync:runCommand="<pathToPreDeployCmd",waitattempts=10,waitinterval=30000 -postSync:runCommand="<pathToPostDeployCmd>",waitattempts=5,waitinterval=30000
I've tried this with the msdeploy.axd url, with msdeploy.axd?site url, and with just the host name of the computer. When I run this command on the local machine where it is deploying to, the command fails with an unauthorized exception despite the user being an admin. I ran the same command on a remote computer and it worked when I just used the host name as the ComputerName.
For some reason the same command works remotely but not locally. I copied and pasted the exact command. I've used both the computer host name as well as localhost locally with no luck.
Any ideas on what the problem might be?

Try setting the destination computername to "localhost".

Related

Azure pipeline remote powershell task hanging

I try the "run powershell on remote machines" task to restart my Tomcat (java) service on the Windows server.
It just keep printing useless info in the console (target machine name)
Here is the detail about the powershell script:
stop Tomcat service (call a .bat file)
move .jar files to right location & replace old files
start Tomcat service (call a .bat file)
& D:\MY\PATH\stop.bat;
......
Copy-Item -Path "D:/s-1.0.jar" -Destination $sqs_path -Force;
......
& D:\MY\PATH\start.bat;
When I run the same command directly in target windows server, the "powershell part" trigger .bat script job, then get back to powershell console successfully .
Later, a new window pops out. The new window is Tomcat server that shows logs of my service.
However, when I do the same job with Azure release pipeline, the Tomcat window did not show up in target machine.
And release job console keep hanging (just print the name of targer machine).
I guess somewhat the output of popout window has be redirected to the console in release pipeline.
In addition, if I cancel the release job. my Tomcat service still working. (just without console to debug)
Or, another thought, can I achieve my goal with other 'task' in release pipeline? (powershell is not a "must")
Any suggestion would be appreciated.
[Update1]
I change the service start commamd to run the .bat in another window.
& D:\MY\PATH\stop.bat;
......
Copy-Item -Path "D:/s-1.0.jar" -Destination $sqs_path -Force;
......
Start-Process cmd.exe -ArgumentList "/C D:\sources\SQS.Dev\start.bat;"
And it turns out "nothing happens" ...
The remote powershell task finish immediately.
The PowerShell on Target Machines task uses WinRM to connect and access the remote target machine.
Normally, WinRM requires the agent machine and the remote target machine have been joined into the same domain or workgroup.
Please check with the following things:
Ensure the agent machine and the remote target machine have been joined into the same domain or workgroup.
Ensure you have followed the steps here to configure the WinRM.
If you have configured the WinRM. Login to the agent machine, ensure you can connect and access the remote target machine when you manually try to call the remote PowerShell. You can try with the following script on the agent machine to test if the WinRM can work well to connect and access the remote target machine.
Param(
# The IP address or FQDN of the remote machine
[string]$computerIp = "{computerIp}"
)
# Username and Password of the admin account on the remote machine
$Username = "{Username}"
$Password = ConvertTo-SecureString "{Password}" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($Username,$password)
# Call the remote PowerShell script
Invoke-Command -ComputerName $computerIp -Credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"powershell.exe /c '{absolute path of the remote PowerShell script}'"}
If it does not work when you try manually calling the remote PowerShell script on the agent machine, the task in the pipeline of course is not able to work. The issue should be on the WinRM, maybe, the WinRM is not configured well.
If it work well when you manually try on the the agent machine, the issue should be on the self-hosted agent. Try to set up a new self-hosted agent with the admin account to see if it can work.
[UPDATE]
As I mentioned above, please try manually calling the remote PowerShell script on the agent machine to see if the commands for 'start Tomcat service' can work.
The PowerShell on Target Machines task is running on the agent machine. If the remote PowerShell script cannot work as expected when you manually try it on the agent machine, it is of course not able to work on the task in pipeline.
At this time, the possible reason of the issue could be the following:
The connection between the machines has some problems.
The PowerShell script has some issues. Maybe you missed some settings for remoting call the script.

Starting Service on remote server failed

I encounter strange behavior when trying to remotely start a service.
on Server A i'm running this line (it's part of bigger script named RunRemoteService.ps1):
Invoke-Command -ComputerName $B_comp -ScriptBlock {Powershell.exe -File "run_service.ps1"} -Credential $cred
And the script run_service.ps1 contains the following line (it's also part of bigger script):
$my_service_name.Start()
Now here's the strange thing, If I run RunRemoteService.ps1 when I have an open remote connection (mstsc) to server B then the script works perfectly and the required service on B is really starting.
However, if I run RunRemoteService.ps1 when there is no mstsc connection with server B then the script failed (service doesn't start).
Why is this happening and how can it be resolved?
EDIT: I explored this issue a bit more and found out that this occurs only when trying to run my specific service.
that means that my service must run from a session of already logged on user (that is why it's working if I mstsc to the server before).
So I guess my new question is - is there a way I can login to remote machine from powershell?
Thanks.
If you cannot use Credential delegation as suggested by #EBGreen. Try using psexec.exe for calling the script instead of PowerShell remoting.
psexec \\server "cmd /c powershell.exe -f c:\script.ps1"
Get psexec from sysinternals.com
Ok, so my question had evolved and modified on the fly so this solution is to the latest issue I had which is - I couldn't remotely start my service if no user is logged on to remote machine.
The solution is a workaround:
I configured auto-login (using sysinternals auto-login tool) on remote server.
I used Restart-Computer cmdlet at the beginning of the test.
Now after the restart is complete and ps-session is restored, user is logged in automatically to server and I can remotely start my service.
Thanks.

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.

PsExec not able to logon to remote machine

I have a bunch of machines to be joined to a domain. They have same name and hence I am using a VBScript to change the host name of these machines (Win7) and then add them to the domain.
The script is present on all the remote target machines and we are trying to run it thorugh psexec from a different machine.
The problem is that PsExec is not able to logon to the target machines. It gives "Access Denied" error. I am using the credentials as... -u "target machine name"\"administrator name" -p
Something like this...
PsExec.exe \%%a -u "target machine"\"administartor name" -p XXXXXXXXX -s cmd "/C cscript.exe \path\to\script.vbs \reboot"
Please help me understand the issue here. I have tested that PsExec works when the remote target machine is on the same domain. Whats the difference between a workgoup and domain.
folks, i found the answer...
PsExec needs the local administrator account on windows to be enabled. Recent Windows(following linux) has made this account default set to disabled(the logic is the same as for 'sudo' in linux: security). Enable this account by the following command(run command prompt as administrator)...
net user administrator /enable:yes

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