-6

I am trying to run a PowerShell command to get the total disk space of all drives for all our remote servers. When I run the command I am getting the error below. I have a text file which has names of the servers and I have also confirmed that WinRM is configured and is running.

$Servers = Get-Content "C:\users\anorris\desktop\DR\servers1.txt"
foreach ($s in $Servers) {
  Invoke-Command -ComputerName $s {Get-PSDrive}
}

Error:

[ahv-a2acortst02] Connecting to remote server failed with the following error
message : Access is denied.
For more information, see the about_Remote_Troubleshooting Help topic.
        + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
        + FullyQualifiedErrorId : PSSessionStateBroken
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • The error message looks straight forward. You don't, or the process doesn't, have rights to access the drives. Have you looked into that? – Sean Rhone Feb 05 '16 at 14:55
  • The error specifically mentions the [`about_Remote_Troubleshooting`](https://technet.microsoft.com/en-us/library/hh847850.aspx) help topic. Did you look into that? What were your findings? – Ansgar Wiechers Feb 05 '16 at 23:27

1 Answers1

0

Agreed that the message 'Access is denied' is a dead giveaway that you don't have access.

I would create a credential variable and make sure it is a credential that has rights to the remote system. $Creds = Get-Credential then change your code to the following (I added the -scriptblock and the bolded text

$Servers = Get-Content "C:\users\anorris\desktop\DR\servers1.txt" foreach ($s in $Servers) { Invoke-Command -ComputerName $s -ScriptBlock {Get-PSDrive} -Credential $creds }