powershell - 如何使用凭据测试写入文件共享路径?

标签 powershell powershell-2.0

我有一个数组 Credential对象,我想测试这些凭据是否具有将文件写入文件共享的权限。

我要做类似的事情

$myPath = "\\path\to\my\share\test.txt"
foreach ($cred in $credentialList)
{
    "Testing" | Out-File -FilePath $myPath -Credential $cred
}

但后来我发现 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
}

关于powershell - 如何使用凭据测试写入文件共享路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21335134/

相关文章:

powershell - 删除所有 .ica 文件

powershell - 如果 PowerShell 脚本出现任何错误,请发送电子邮件并终止该脚本

powershell - Powershell文件名检查

powershell - 使用 Power Shell 重命名文件名中包含括号的文件

c# - AssemblyInfo 版本的 Nuspec 版本

xml - 用 2 "values"解析 XML

azure 管道-根据触发的分支设置变量

powershell - Powershell脚本到电子邮件的最新修改日期

sql-server - 是否可以在 powershell 中使用服务器凭据创建 Microsoft SMO SQL 连接?

windows - 我可以以编程方式访问 RSA 软 token 密码吗