Hello,
I’m facing an issue when I’m testing path in IF statement. When I run the script from PowerShell all works as expected. When I add in Script template I’m facing with error all the time, whether I tried. The script basically, goes to all target servers, and zip IIS logs. In the beginning, tries to find C:\inepub\logs\logfiles, If doesn’t exist, it should exit the script … and there is an issue as I am getting all the time error output.
I tied with Exut, return, try to use Get-ChildItem…but, the output error went the same.
Get-ChildItem : Cannot find path 'C:\inetpub\logs\L' because it does not exist.
At C:\Octopus\Work\20180416141949-394579-228\Script.ps1:39 char:9
+ $dirs = Get-ChildItem -Path $logsPath -Recurse -Force | Where-Object ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\inetpub\logs\L:String) [Get-
ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh
ildItemCommand
The remote script failed with exit code 1
Maybe I made mistake somewhere, but I can’t see it. Could someone help here?
-----------Script ------------------
$7zipLocation = "\\server_share\7-Zip\"
$limitT = (Get-Date).AddDays(-31) # Test
$limitP = (Get-Date).AddDays(-365) # Production
$servername = ${Env:ComputerName}
$logsPath = "C:\inetpub\logs\LogFiles\"
$getName = ${Env:ComputerName}+'\'
$logFile = "backup_log_$servername.txt"
$target = "C:\Backup\"
$dirs = Get-ChildItem -Path $logsPath -Recurse -Force | Where-Object {$_.Attributes -like '*Directory*'}
$attachmentT = "$target$getName$logFile"
$folderToCreate = Join-Path -Path $target -ChildPath $getName
if (!(Test-Path $logsPath)) {
Write-Warning "Directory doesn't exist and will be created"
Exit
}
elseif (!(Test-Path -Path "$folderToCreate" -PathType Container)) {
New-Item -ItemType Directory -Force -Path $folderToCreate
}
elseif (!(Test-Path -Path "$env:ProgramFiles\7-Zip\7z.exe")) {
Copy-Item -Path $7zipLocation -Destination $env:ProgramFiles\7-Zip -Recurse -Force
Write-Host "7z copied to the target server "
}
else {
Write-Host "Backup is started"
Foreach ($dir in $dirs) {
$result_path = $target + $getName + ($dir.LastWriteTime).ToString("yyyy-MM") + "\IIS\"
$newdir = $result_path + $dir.name
$items = Get-ChildItem -Path $logsPath$dir -Recurse -Force -Verbose
Foreach ($item in $items) {
set-alias 7z "$env:ProgramFiles\7-Zip\7z.exe"
7z $arguments $compression -bso0 -bsp0 "$newdir\$item.zip" "$logsPath\$dir\$item" -ssw
}
}
$backup_log = Get-ChildItem -recurse $target\$getName | Sort-Object LastWriteTime | out-File
"$target\$getName\$logFile"
Write-Host "Backup the logs from $servername is completed."
}
Thanks in advance,
Branko