Issue
Я пытаюсь записать журналы в один файл журнала с помощью модуля PowerShell PSlogging.
Сценарий использует параллельный объект Foreach.
скрипт работает, но иногда скрипт застревает между ними.
Пожалуйста, помогите мне решить это? Есть ли лучший способ добиться этого?
Ядро Powershell 7
код
#Script Version
$sScriptVersion = '1.0'
$sScriptVersion = '1.0'
$sLogName = 'custom.log'
#Log File Info
$sLogPath = "C:\Users\Dixon\Desktop\automation\azure\log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
Start-Log -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
$allServers = $(Get-AzVM -Status)
try {
$allServers | ForEach-Object -parallel {
$sScriptVersion = '1.0'
$sLogName = "parallel-" + $_.Name +".log"
#Log File Info
$sLogPath = "C:\Users\Dixon\Desktop\automation\azure\log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
Import-Module .\modules\pwshAzMod\pwshAzMod.psd1 -Force
$version = "1.0.11"
$vmName = $_.Name
$vmLocation = $_.Location
$vmResourceGroupName = $_.ResourceGroupName
$powerState = $_.PowerState | Out-String
if ($powerState -match "running") {
if ($_.OSProfile.WindowsConfiguration) {
Write-LogInfo -LogPath $sLogFile -Message "Start --- Windows Servers --- $vmResourceGroupName $vmLocation $vmName $version $powerState"
Write-LogInfo -LogPath $sLogFile -Message "Done --- Windows Servers --- $vmResourceGroupName $vmLocation $vmName $version"
}
elseif ($_.OSProfile.LinuxConfiguration) {
$extensionName = "CustomScript"
Write-LogInfo -LogPath $sLogFile -Message "Start --- Linux Servers --- $vmResourceGroupName $vmLocation $vmName $version $powerState"
Write-LogInfo -LogPath $sLogFile -Message "Done --- Linux Servers --- $vmResourceGroupName $vmLocation $vmName $version"
}
else {
Write-LogInfo -LogPath $sLogFile -Message "OS Type not handled"
}
}
else {
if ($_.OSProfile.WindowsConfiguration) {
Write-LogInfo -LogPath $sLogFile -Message "Deallocated --- Windows Servers --- $vmResourceGroupName $vmLocation $vmName $version $powerState"
}
elseif ($_.OSProfile.LinuxConfiguration) {
Write-LogInfo -LogPath $sLogFile -Message "Deallocated --- Linux Servers --- $vmResourceGroupName $vmLocation $vmName $version $powerState"
}
else {
Write-LogInfo -LogPath $sLogFile -Message "Deallocated --- Other Servers --- $vmResourceGroupName $vmLocation $vmName $version $powerState"
}
}
} -ThrottleLimit 5
}
catch {
$errorMessage = $_
$errorMessage
}
finally {
Get-Content $sLogPath/parallel-*.log | Add-Content $sLogFile
Remove-Item –path $sLogPath\* -Filter parallel*
Stop-Log -LogPath $sLogFile
}