【发布时间】:2014-03-19 03:08:26
【问题描述】:
我正在尝试将文件从一台服务器复制到另一台服务器,这样它就不会要求我输入我的登录信息,我也不必在代码中直接存储密码。问题是,即使我提供了 -Credential 参数,它仍然会提供凭据请求 GUI。这就是我将密码存储在文件中的方式:
read-host -assecurestring | convertfrom-securestring | out-file C:\secString.txt
那么代码如下所示:
function GetSecureLogin() {
$username = "usa02xxxswg2\20xbackup"
$password = get-content C:\secString.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
}
function CopyFileToFolder ([string]$Source, [string]$destination){
Copy-Item "$Source" -Destination "$destination" -Credential $cred #this is where the login info box is coming up but shouldn't be
}
######################start here#####################
$cred = ""
GetSecureLogin
$tempSource = "filename.txt"
$ToLocation = "$ToLoc"
CopyFileToFolder $tempSource $ToLoc
有什么想法吗?我对 PowerShell 比较陌生,所以我可能会做一些愚蠢的事情。我不确定如何判断 $cred 是否可以恢复到我的 Copy 功能,或者它是否超出范围。
我查看了这些示例,但我无法决定是否需要做一些不同的事情,因为它们指定了服务器信息,我试图避免这种情况,因为我只想像以前一样使用 UNC 路径以前做的。无需登录即可正常复制,因为我使用 Active Directory 登录来保存任务。现在我们想使用本地保存的登录名(脚本运行的地方),然后它在尝试访问其他服务器时需要不同的本地登录名。
copy-item with alt cred, powershell without prompt for cred, copy without cred, copy with alt cred, cred without password prompt
【问题讨论】:
标签: copy credentials powershell-3.0