Link to home
Start Free TrialLog in
Avatar of AjarnJonesy
AjarnJonesyFlag for United States of America

asked on

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
Avatar of AjarnJonesy
AjarnJonesy
Flag of United States of America image

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
     
    }
I've requested that this question be deleted for the following reason:

need to change it quite a bit.