• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 855
  • Last Modified:

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
  • 3
1 Solution
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

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
QlemoBatchelor and DeveloperCommented:
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.
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
AjarnJonesyAuthor Commented:
I've requested that this question be deleted for the following reason:

need to change it quite a bit.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now