如何从本地复制到远程位置,在PowerShell中的凭据?凭据、位置、PowerShell

2023-09-05 04:19:06 作者:时光会咬人

我是一个新的初学者到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"