$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