0
$hostnames = Get-Content -Path C:\new_folder\machines.txt
ForEach ($server in $hostnames) {
    Write-Host $server
    $server1 = $([System.Net.Dns]::GetHostAddresses("$server")).IPAddressToString
    $server1 = $server1.ToString()

    try {
        Set-Item wsman:\localhost\client\trustedhosts -Concatenate -Value $server1 -Force
        Invoke-Command -ComputerName $server1 -Credential $Cred -ScriptBlock { 'cmd.exe /c C:\bat_start_process\process.bat' } -ErrorAction Stop
    } 
    catch {
        Write-Host 'error'
        $formatstring = "{0} : {1}`n{2}`n" +
        "    + CategoryInfo          : {3}`n" +
        "    + FullyQualifiedErrorId : {4}`n"
        $fields = $_.InvocationInfo.MyCommand.Name,
        $_.ErrorDetails.Message,
        $_.InvocationInfo.PositionMessage,
        $_.CategoryInfo.ToString(),
        $_.FullyQualifiedErrorId

        $formatstring -f $fields
    }
} 

Hi, I am trying to connect to a remote machine and invoking a bat file. The idea is using this powershell, i should be able to just invoke the bat and leave it running. But i see some error as below.

 error
 : [*.*.*.*] Connecting to remote server *.*.*.* failed with the following error message : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible ove
r the network, and that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM firewall exception for public profiles limits access to remote computers within the sam
e local subnet. For more information, see the about_Remote_Troubleshooting Help topic.
At C:\startBatfile.ps1:19 char:13
+             Invoke-Command -ComputerName $server1 -Credential $Cred - ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (10.233.165.70:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionStateBroken 
1

1 Answer 1

1

First of all, you have an issue with the connection to the remote host over WinRM.

To solve that, you should configure WinRM on both servers.

# Just run that cmdlet.    
winrm quickconfig -quiet

Then you must have configured TrustedHosts on both servers. I see that you are doing that on your computer, but what about trustedHost on remote server?

# This will set all as trusted
Set-Item wsman:\localhost\client\trustedhosts *
Restart-Service WinRM

And usually required to set an Execution policy, but if you want to run a batch, this is not needed.

And the Second thing this could not work.

Why?

Because invoke-command will not execute an interactive process/script on the remote host, it is possible in few ways, but the most simple one is to use Invoke-CommandAs Module.

Just run PowerShell as Administrator and run this command.

Install-Module -Name Invoke-CommandAs -Force

So you doing it like that...

Invoke-CommandAs -ComputerName $server1 -ScriptBlock { 'cmd.exe /c C:\bat_start_process\process.bat' }-AsUser $Cred -ErrorAction Stop
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.