0

I have written a PowerShell script which uninstall a program and install a newer version of the program on my servers (Update Programs). Now I want to create another script which run the aforementioned script on the servers. Consider that I have to connect to my servers through using IPs, UserName and password and using domain is not an option.

How is this possible?

PowerShell version is 4

I have tried this code to simply get date:

$User = "administrator"
$PWord = ConvertTo-SecureString -String "Password1234" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord
$session = New-PSSession -ComputerName '10.60.60.100' -Credential $Credential
Invoke-Command -Session $session -ScriptBlock {Get-Date}

and I got this error:

New-PSSession : [10.60.60.100] Connecting to remote server 10.60.60.100 failed with the following error message : The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You can get more information about that by running the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.

TylerH
  • 20,799
  • 66
  • 75
  • 101
A.Kazemi
  • 13
  • 1
  • 6
  • What did you try? – Jeroen Heier Jun 24 '18 at 09:17
  • 1
    @JeroenHeier I have added my last try – A.Kazemi Jun 24 '18 at 10:03
  • For any remoting-related issues, it's always worth checking the troubleshooting guide in the help to make sure you've not missed something obvious: [about_remote_troubleshooting](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_troubleshooting?view=powershell-6) – boxdog Jun 24 '18 at 10:49

1 Answers1

2

This is because you’re not running your command from a trusted host, or because the remote computers wsman service isn’t configured properly. I’d start by running the following command to configure wsman on the remote machine:

wsman quickconfig

If that doesn’t fix the problem, then you need to add your computer to the remote machines trusted hosts. You can do that by running the following:

winrm s winrm/config/client '@{TrustedHosts="RemoteComputer"}'
Bryce McDonald
  • 1,760
  • 12
  • 22
  • Thanks, the trustedhosts was the problem. Now I replace `get-date` with `powershell -file C:\Test.ps1` but the script is running on the local machine and not the remote one. How can I fix this? – A.Kazemi Jun 24 '18 at 11:07
  • I’ve found session management with invoke-command to behave oddly at times. Try just passing the credentials directly into the command. It’ll look like ‘Invoke-Command -Computername $comps -Credential $creds -FilePath C:\Test.ps1’ – Bryce McDonald Jun 24 '18 at 11:51
  • Thanks for your help. The problem is relatively solved. Just 1 problem remained. Now I can unistall the old version and install the new version through running a script on the remote server, but in the last sentences of the code I tried to run the .exe file for running my new program and nothing happening. If I run the .ps1 file its work but in the remote manner aforementioned problem exits. Even I used `-scriptblcok {invoke-item "C:\Test.exe"}` and `-scriptblock {& "C:\Test.exe"}` but no change happened – A.Kazemi Jun 26 '18 at 08:33
  • I don't think you can run executables remotely. It may be wise to transfer the .exe to something like "C:\Temp" on the remote machine, then call the executable remotely. – Bryce McDonald Jun 27 '18 at 14:39