PowerShell如何访问共享目录?

timeofsunrise
  • 1.5k

image.png

回复
阅读 977
1 个回答

已经映射的和访问本地是一样的,没有映射的先用 New-PSDrive 创建一个映射再访问。

# 生成 PSCredential
Function GenerateCred($user, $pwd){
    $sPwd = ConvertTo-SecureString $pwd -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential ($user, $sPwd) 
    Return $cred
}

$user = "xxx" #访问共享的用户名
$pwd = "xxx" #访问共享的密码
$from = "\\192.168.0.1\xxx" #共享目录地址
$cred = GenerateCred $user $pwd

#创建临时映射
New-PSDrive -Name "Z" -PSProvider "FileSystem" -Root $from -Credential $cred
#访问
$Filelist = Get-Childitem "Z:\"  –Recurse
宣传栏