如何从本地复制到远程位置,在PowerShell中的凭据? [英] How can I copy from local to a remote location with credentials in PowerShell?

查看:515
本文介绍了如何从本地复制到远程位置,在PowerShell中的凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的初学者到PowerShell的。

I am a fresh beginner to PowerShell.

我的用户名和密码在远程位置达到一个共享文件夹。

I have username and password to reach a shared folder in a remote location.

我需要以文件 foo.txt的从当前位置复制到 \\ Bar.foo.myCOmpany .COM \中被用于编写的PS1脚本日志 的Powershell 3.0

I need to copy the file foo.txt from the current location to \\Bar.foo.myCOmpany.com\logs within a PS1 script that is written for Powershell v3.0.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

拷贝项目不支持-credential参数,该参数不显示 但它不支持任何Windows PowerShell的核心cmdlet或提供

Copy-Item doesn't support the -credential parameter, This does parameter appear but it is not supported in any Windows PowerShell core cmdlets or providers"

您可以试试下面的函数映射网络驱动器,并调用拷贝

You can try the below function to map a network drive and invoke copy

Function Copy-FooItem {

param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$True)]
        [string]$username,
        [Parameter(Mandatory=$true,ValueFromPipeline=$True)]
        [string]$password
        )

$net = New-Object -com WScript.Network
$drive = "F:"
$path = "\\Bar.foo.myCOmpany.com\logs"
if (test-path $drive) { $net.RemoveNetworkDrive($drive) }
$net.mapnetworkdrive($drive, $path, $true, $username, $password)
copy-item -path ".\foo.txt"  -destination "\\Bar.foo.myCOmpany.com\logs"
$net.RemoveNetworkDrive($drive)

}

下面是如何运行的功能,只需要改变参数,用户名和密码

Here's how you can run the function just change the parameters for username and password

Copy-FooItem -username "powershell-enth\vinith" -password "^5^868ashG"

这篇关于如何从本地复制到远程位置,在PowerShell中的凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆