Я хочу сохранить каждый 'write-verbose' и 'write-warning' в текстовом файле / файле журнала после завершения сценария.
Я пробовал out-file в цикле и outцикла с переменной в начале, как элементы, предложенные здесь. Как вывести вывод цикла foreach в файл в PowerShell?
edit: было бы удобно также захватить выходные данные из Measure-Command.
Но этоне работает для моего сценария.Любая помощь будет оценена.
#initialisation
CLS
$ErrorActionPreference = 'Stop'
$VerbosePreference = "continue"
#Settings
$SubFolder = ".\_Orphan"
$FileListFile = ".\DirtoMove.csv"
#Retrieve List with folders to move from current folder.
Try { [Array]$FilesToMove = Get-Content $FileListFile }
Catch {Write-Warning "could not load $($FileListFile)"; Start-Sleep -S 3 ; Exit}
#If subfolder does not exist then create it.
If (!(Test-Path $SubFolder)) {
Try { New-Item $SubFolder -ItemType Directory | Out-Null}
Catch {Write-Warning "Could not create subfolder $($SubFolder)"; Start-Sleep -S 3 ; Exit}
}
Measure-Command{
#Try to moving the folders from the list to the the specified subfolder.
Foreach ($File in $FilesToMove) {
#If folder does not exist then skip.
If (!(Test-Path $File)) {
Write-Verbose "File $($File) Does not exist, skipping";
Continue
}
# Check if folder already exist in the sub folder.
If (Test-Path (Join-Path -Path $SubFolder -ChildPath $File)){
Write-Verbose "File $($File) exists already in the subfolder, skipping";
Continue
}
#try moving the folder.
Try {
$File | Move-Item -Destination $SubFolder;
Write-Verbose "File $($File) succesfully moved." ;
}
Catch {Write-Warning "Could not move file $($File), Skipping" ; Continue}
}
}
Write-Verbose "Script finished, waiting for 5 seconds before closing."
Start-Sleep -Seconds 5