Powershell复制文件,将源路径,搜索字符串和目标路径作为参数传递

时间:2018-09-12 20:16:29

标签: powershell automation

param([string]$username,[string]$path,[string]$searchstr,[string]$destination) 
$password = Get-Content "C:\Users\V70070\Desktop\mysecurestring.txt" 
$secstr = New-Object -TypeName System.Security.SecureString $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)} 
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr 

Invoke-Command -ComputerName 10.82.21.154 -ScriptBlock { Get-ChildItem -Path $path -Recurse | Where-Object {$_.Name -like $searchstr} | Copy-Item -Destination $destination} -credential $cred

以上命令保存在“ C:\ Users \ V70070 \ Desktop \ script5.ps1”中,我以

执行powershell脚本

& "C:\Users\V70070\Desktop\script5.ps1" "V70070" "D:\Sett-Trans-New\InputFiles\Transmission\KDV\OasisNITS\Negative_Backup" "*Data*" "D:\Sett-Trans-New\InputFiles\Transmission\KDV\OasisNITS"

我没有看到任何执行错误,也没有复制文件。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您使用的是PS v 2.0吗?

$Using:仅从3.0开始工作。

您可以在Param()中使用Invoke-Command

Invoke-Command -ComputerName 10.82.21.154 -ScriptBlock {
    Param($Path)
    Get-ChildItem -Path $path -Recurse | Where-Object {$_.Name -like $searchstr} | Copy-Item -Destination $destination
} -credential $cred -Argumentlist $Path