Powershell Script/Psession

First off thanks to Qlemo I am down to one final piece on this. :)  

At this point I m trying to establish a remote session with servers imported from my servers.txt  My servers.txt has the following headers. computername,optype,opname
I import the list and loop through. If the format is server,service,whatever_service I start the service. If the format is server,script,script-path-and-name I then need to establish a remote session with the server and kick off that script on the remote server then write it out to a log.
The code I am using to do this (I haven't figured out the best way to establish the session yet) is
 elseif($computer.optype -eq "script")
    {      
#somehow need to establish a remote session
    write-host  running script $computer.opname on $computer.computername  
      $Status = Invoke-Command -ComputerName $computer.computername -ScriptBlock { . $computer.opname } #here I need to add a start argument so I can call
#whatever remote script something like c:\scripts\service.bat -start
      Write-Output "Script execution status for $($computer.name) is $Status" >> C:\Scripts\RMLog.txt
    }

However using that now with a statement in my servers.txt like  server,script,script-path I receive the following error

[server] Connecting to remote server server failed with the following error message : The client cannot connect to the destination specified in the
request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management
service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to
analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (server:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken
servers.txt
SVCStartFinal.txt
AjarnJonesyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AjarnJonesyAuthor Commented:
I added the following in trying to establish the session however now get a different error.

 elseif($computer.optype -eq "script")
    {


    $session = new-pssession -computerName  $computer.computername
    write-host  running script $computer.opname on $computer.computername  
      Invoke-Command -session $session -ScriptBlock { . $computer.opname }
      Write-Output "Script execution status for $($computer.name) is $session" >> C:\Scripts\RMLog.txt
    }

Error
new-pssession : [c1399] Connecting to remote server c1399 failed with the following error message : Access is denied. For more information, see the
about_Remote_Troubleshooting Help topic.
At C:\scripts\STartSVCFinal.ps1:33 char:16
+     $session = new-pssession -computerName  $computer.computername
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed
running script c:\scripts\svcstop.bat on c1399
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Supply an argument that is not null or empty and then
try the command again.
At C:\scripts\STartSVCFinal.ps1:35 char:26
+     Invoke-Command -session $session -ScriptBlock { . $computer.opname }
+                             ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand


The error message I get now is
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The remote target isn't a server with current OS in the same domain, because those are configured by default to accept remote commands from the same domain. Depending on what your environment is exactly, you'll need to issue
   winrm quickconfig
on the remote machine to set up the PS Remoting stuff. There might be more to set up if you are not in a domain.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AjarnJonesyAuthor Commented:
I did run through the winrm quickconfig.. I have it creating a session now and am just down kicking off the remote script.. Just arguing with the invoke-command now..

I changed the elseif to
   elseif($computer.optype -eq "script")
    {
   
    $session = New-PSSession -ComputerName $computer.computername
write-host  running script $computer.opname on $computer.computername  Status is "$status"
      $Status = Invoke-Command -computername $computer.computername -ScriptBlock { Invoke-Expression - $computer.opname}
   
 
      Write-Output "Script execution status for $($computer.name) is $Status" >> C:\Scripts\RMLog.txt
 
    Get-PSSession | Remove-PSSession
     
    }
0
AjarnJonesyAuthor Commented:
I've requested that this question be deleted for the following reason:

need to change it quite a bit.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.