我需要在另一台服务器上观看一个文件夹,所以当该文件夹中出现一个新文件时,它会被复制到另一个文件夹。
但我不知道如何使用凭据来做到这一点。当我尝试观看文件夹时,我的脚本给了我错误。
$folder = '10.162.106.30\d$\IPE\CRMA\Por Tratar'
$filter = '*.xls*'
$folder2 = Get-Date -UFormat "%m-%Y"
$destination = '\\10.162.106.30\d$\IPE\CRMA\' + $folder2
$destination2 = 'D:\Import_Jbrandao\Click'
Write-Host "--------------------------------------------------------"
Write-Host "Folder With Whatch:" $folder
Write-Host "Folder Destination:" $destination
Write-Host "--------------------------------------------------------"
$ComputerName = "10.162.106.30"
$UserName = "UserName"
$Password = "********"
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName , $SecurePassword
$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -Credential $Credential
Write-Host "** Whatch Folder **"
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
get-event -SourceIdentifier FileCreated
#Unregister-Event -SourceIdentifier FileCreated
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$nameFile = $Event.SourceEventArgs.Name
Write-Host "** Watch has something **"
Write-Host "Year:" $nameFile.Substring(40,4)
Write-Host "Month:" $nameFile.Substring(45,2)
Write-Host "Day:" $nameFile.Substring(48,2)
$name = "BYSIDE_" + $nameFile.Substring(40,4) + '_' + $nameFile.Substring(45,2) + '_' + $nameFile.Substring(48,6)
$path = $Event.SourceEventArgs.FullPath
Rename-Item -Path $path -NewName $name
Write-Host "** Name file was changed **"
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp"
Write-Host "Try move from " $path\$namefile " To " $destination\$name
Move-Item $folder\$name -Destination $destination\$name -Force -Verbose # Force will overwrite files with same name
Write-Host "Start Program Import C2C"
Write-Host "Stop if have 'ImportIndicadores.exe' running"
Get-Process ImportIndicadores | Stop-Process
# -NoLogo -NonInteractive -File
Set-ExecutionPolicy Unrestricted
Write-Host "Start 'ImportIndicadores.exe'"
Start-Process -FilePath "C:\APPS_Manual\IF Don't Work (SOS)\C2C_Telefonia.appref-ms"
Write-Host "Wait 20 Seconds"
Start-Sleep -Seconds 20
Write-Host "Copy From " $destination\$name " To " $destination2\$name
Copy-Item $destination\$name -Destination $destination2\$name -Force -Verbose
}
错误:
New-Object:使用“2”参数调用“.ctor”的异常:“目录名称10.162.106.30 \ d $ \ IPE \ CRMA \ Por Tratar 无效。“在C:\ PowerShell \ Click \ Click2Call.ps1:23 char:18 + $ fsw = New-Object<<<< IO.FileSystemWatcher $文件夹,$ filter -Property @ { + CategoryInfo:InvalidOperation:(:) [New-Object],MethodInvocationException + FullyQualifiedErrorId:ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Get-Event:源标识符“FileCreated”的事件不存在。 在C:\ PowerShell \ Click \ Click2Call.ps1:27 char:10 + get-event<<<< -SourceIdentifier FileCreated + CategoryInfo:InvalidArgument:(:) [Get-Event],ArgumentException + FullyQualifiedErrorId:INVALID_SOURCE_IDENTIFIER,Microsoft.PowerShell.Commands.GetEventCommand
Register-ObjectEvent:无法将参数绑定到参数'InputObject' 因为它是null。在C:\ PowerShell \ Click \ Click2Call.ps1:30 char:34 + $ onCreated = Register-ObjectEvent<<<< $ fsw Created -SourceIdentifier FileCreated -Action { + CategoryInfo:InvalidData:(:) [Register-ObjectEvent],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RegisterObjectEventCommand
答案 0 :(得分:0)
我相信你错过了IP地址前的\\
。
$folder = '\\10.162.106.30\d$\IPE\CRMA\Por Tratar'