Mar*_*son 7 powershell powershell-2.0
我有一个Credential对象数组,我想测试这些凭据是否有权将文件写入文件共享.
我打算做点什么
$myPath = "\\path\to\my\share\test.txt"
foreach ($cred in $credentialList)
{
"Testing" | Out-File -FilePath $myPath -Credential $cred
}
Run Code Online (Sandbox Code Playgroud)
但后来我发现Out-File不Credential作为参数.解决这个问题的最佳方法是什么?
你可以使用New-PSDrive:
$myPath = "\\path\to\my\share"
foreach ($cred in $credentialList)
{
New-PSDrive Test -PSProvider FileSystem -Root $myPath -Credential $Cred
"Testing" | Out-File -FilePath Test:\test.txt
Remove-PSDrive Test
}
Run Code Online (Sandbox Code Playgroud)
这里是一个asituation,其中一个旧的exe(net.exe)似乎比powershell更好......我猜你可以尝试使用提供的凭据映射网络驱动器然后测试将文件写入该驱动器:
$cred=get-credential
$pass=$cred.GetNetworkCredential().Password
net use q: \\servername\share $pass /user:$cred.username
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10351 次 |
| 最近记录: |