40

我正在使用 powershell v2 的 CTP。我编写了一个脚本,需要访问我们 dmz 中的各种网络共享并复制一些文件。但是,我遇到的问题是,显然 powershell 的 cmdlet(例如复制项、测试路径等)不支持备用凭据...

任何人都对如何最好地完成我的任务有建议..?

4

13 回答 13

45

我最近遇到过这种情况,在最新版本的 Powershell 中有一个新的BitsTransfer Module ,它允许使用BITS进行文件传输,并支持使用 -Credential 参数。

以下示例显示了如何使用 BitsTransfer 模块将文件从网络共享复制到本地计算机,使用指定的 PSCredential 对象。

Import-Module bitstransfer
$cred = Get-Credential
$sourcePath = \\server\example\file.txt
$destPath = C:\Local\Destination\
Start-BitsTransfer -Source $sourcePath -Destination $destPath -Credential $cred

另一种处理方法是使用标准的“net use”命令。但是,此命令不支持“securestring”密码,因此在获取凭证对象后,您必须获取密码的解密版本才能传递给“net use”命令。

$cred = Get-Credential
$networkCred = $cred.GetNetworkCredential()
net use \\server\example\ $networkCred.Password /USER:$networkCred.UserName
Copy-Item \\server\example\file.txt C:\Local\Destination\
于 2011-03-23T17:05:53.970 回答
34

由于 PowerShell 不支持通过许多 cmdlet 使用“-Credential”(非常烦人),并且通过 WMI 映射网络驱动器在 PS 中被证明是非常不可靠的,我发现通过 net use 命令预先缓存用户凭据工作得很好:

# cache credentials for our network path
net use \\server\C$ $password /USER:$username

在路径中使用 \\server\C$ 的任何操作似乎都可以使用 *-item cmdlet。

完成后,您还可以删除共享:

net use \\server\C$ /delete
于 2010-02-16T23:13:24.730 回答
23

PowerShell 3.0 现在支持文件系统提供程序上的凭据。要使用备用凭据,只需使用 New-PSDrive cmdlet 上的 Credential 参数

PS > New-PSDrive -Name J -PSProvider FileSystem -Root \\server001\sharename -Credential mydomain\travisj -Persist

在此命令之后,您现在可以访问新创建的驱动器并执行其他操作,包括像普通驱动器一样复制或移动文件。这是完整的解决方案:

$Source = "C:\Downloads\myfile.txt"
$Dest   = "\\10.149.12.162\c$\skumar"
$Username = "administrator"
$Password = ConvertTo-SecureString "Complex_Passw0rd" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)

New-PSDrive -Name J -PSProvider FileSystem -Root $Dest -Credential $mycreds -Persist
Copy-Item -Path $Source -Destination "J:\myfile.txt"
于 2015-06-03T03:28:37.807 回答
9

这是一个老问题,但我只是为未来的发现者更新它。

PowerShell v3 现在支持将 -Credential 参数用于文件系统操作。

希望这可以帮助其他人寻找相同的解决方案。

于 2012-05-03T17:57:26.973 回答
6

我会尝试将驱动器映射到远程系统(使用“网络使用”或 WshNetwork.MapNetworkDrive,这两种方法都支持凭据),然后使用复制项。

于 2009-03-05T10:23:48.203 回答
2

我知道PowerShell 3 现在开箱即用地支持这一点,但是为了记录,如果你被困在 PowerShell 2 上,你基本上必须使用旧net use命令(如其他几个人所建议的那样),或者我的模拟模块专门写了一段时间来解决这个问题。

于 2012-09-25T15:56:44.067 回答
1

这是我在机器上作为 LocalSystem 运行的脚本,但需要域用户的凭据才能访问网络文件位置。它允许您将用户的密码存储在“安全的”加密文件中;只能由编写它的用户阅读。

通过将包含明文密码的文件复制到机器中来设置和更改密码。下次运行脚本时,它会读取密码,对其进行加密,然后删除明文密码。

$plaintext_password_file = 'C:\plaintext.txt' # Stores the password in plain text - only used once, then deleted
$encryted_password_file = 'C:\copy_pass.txt'  # Stores the password in "safe" encrypted form - used for subsequent runs of the script
                                              #   - can only be decrypted by the windows user that wrote it
$file_copy_user = 'OURDOMAIN\A_User'

# Check to see if there is a new plaintext password
if (Test-Path $plaintext_password_file)
{
    # Read in plaintext password, convert to a secure-string, convert to an encrypted-string, and write out, for use later
    get-content $plaintext_password_file | convertto-securestring -asplaintext -force | convertfrom-securestring | out-file $encryted_password_file
    # Now we have encrypted password, remove plain text for safety
    Remove-Item $plaintext_password_file
}


# Read in the encrypted password, convert to a secure-string
$pass = get-content $encryted_password_file | convertto-securestring

# create a credential object for the other user, using username and password stored in secure-string
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $file_copy_user,$pass

# Connect to network file location as the other user and map to drive J:
New-PSDrive -Name J -PSProvider FileSystem -Root "\\network\file_directory" -Credential $credentials

# Copy the file to J:
Copy-Item -Force -Verbose -Path "C:\a_file.txt" -Destination "J:\"

作为一个额外的改进:用户名也可以被加密,而不是硬编码。

于 2017-08-23T22:52:12.473 回答
0

这个问题解决了一个非常相关的问题,可能有助于在 powershell 中使用网络共享

于 2009-03-11T00:33:58.147 回答
0

是一个帖子,有人让它工作。看起来它需要更改注册表。

于 2009-03-04T19:42:44.340 回答
0

让这个死而复生。通过将 .ps1 包装在批处理文件中并执行 Win7、Shift + r.Click RunAs,我解决了类似的凭据问题。如果你愿意,你也可以这样使用 PsExec:

psexec.exe /accepteula /h /u user /p pwd cmd /c "echo. | powershell.exe -File script.ps1"
于 2012-11-23T04:22:46.953 回答
0

较新的 PowerShell 版本可以处理此问题,MS 文档中有一个很好的示例,可以在此处复制具有不同凭据的文件

$Session = New-PSSession -ComputerName "Server02" -Credential "Contoso\User01"
Copy-Item "D:\Folder002\" -Destination "C:\Folder002_Copy\" -ToSession $Session
于 2021-01-27T19:56:16.380 回答
-1

显然 powershell 的 cmdlet(例如复制项、测试路径等)不支持备用凭据...

看起来他们在这里做的,copy-item 肯定包含一个 -Credential 参数。

PS C:\> gcm -syn 复制项
复制项 [-Path] <String[]> [[-Destination] <String>] [-Container] [-Force] [-Filter <String>] [-I
nclude <String[]>] [-Exclude <String[]>] [-Recurse] [-PassThru] [-Credential <PSCredential>] [...]
于 2009-03-04T19:23:12.797 回答
-6

您应该能够将所需的任何凭据传递给 -Credential 参数。所以像:

$cred = 获取凭据

[输入凭据]

Copy-Item -Path $from -Destination $to -Credential $cred

于 2009-03-04T19:23:52.623 回答