简体   繁体   中英

Powershell : Accessing shared drive from remote server

I am trying to access shared drive vai remote server in my Powershell script. My code is below:

$bypass1 = "config"
$bypass2 = "web.config"
$Username = "test\newtest"
$Password = "xxxxxxxxx"
$srv = "xxx.xxx.xxx.xxx"

$securePassword = ConvertTo-SecureString -AsPlainText -Force $Password 
$cred = New-Object System.Management.Automation.PSCredential $Username, $securePassword
$session = New-PSSession -ComputerName $srv -port 22 -Credential $cred
Invoke-Command -Session $session -ScriptBlock {
    $computer = "xxx.xxx.xxx.xxx"
    test-path \\$computer\netlog\php
    Get-ChildItem \\$computer\netlog\
}
Remove-PSSession -Session $session

When I tried to access shared from Remote Desktop Connection on the server it is working but through powershell it is throwing following error.

False
Cannot find path '\\xxx.xxx.xxx.xxx\netlog\' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (\\xxx.xxx.xxx.xxx\netlog\:String) [Get-Ch 
   ildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCom 
   mand
    + PSComputerName        : xxx.xxx.xxx.xxx

I am having Powershell 4 and remote server is windows 2008 R2.

Regards, Vj

Did you try accessing the following path \\\\xxx.xxx.xxx.xxx\\netlog\\ locally? You have the creds to access the server. And also you say the drive is shared. Try accessing the path locally. Not via power-shell. But try the old fashioned way. Using Run . If you are prompted for any credentials to be entered. You may strike off any issues related to permissions. If not... The folder you are trying to access isn't shared at all. Try giving it the required permissions. If this isn't the case please leave a comment.

You might need to enable Multihop Remoting when you access a share from a remote machine and use CredSSP.

The credentials you use to create the remote session are not passed to the share access action by default. I think everyone runs into this problem at least once. :)

http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/04/enabling-multihop-remoting.aspx

http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/14/enable-powershell-quot-second-hop-quot-functionality-with-credssp.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM