Reputation: 181
I need to watch a folder on another server so when a new file appears in that folder it is copied to another folder.
But I don't know how to do this with credentials. My script give-me errors when I try to watch the folder.
$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
}
Errors:
New-Object : Exception calling ".ctor" with "2" argument(s): "The directory name 10.162.106.30\d$\IPE\CRMA\Por Tratar is invalid." At C:\PowerShell\Click\Click2Call.ps1:23 char:18 + $fsw = New-Object <<<< IO.FileSystemWatcher $folder, $filter -Property @{ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Get-Event : Event with source identifier 'FileCreated' does not exist. At 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 : Cannot bind argument to parameter 'InputObject' because it is null. At 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
Upvotes: 0
Views: 469
Reputation: 2342
I believe you were missing the \\
before the IP address.
$folder = '\\10.162.106.30\d$\IPE\CRMA\Por Tratar'
Upvotes: 0