Eccezione da HRESULT: 0x800A03EC PowerShell

Dopo aver eseguito questo comando PowerShell che incolla una formula dentro una cella di un file Excel:

$excelObject = New-Object -ComObject Excel.Application $targetWorkbook = $excelObject.Workbooks.Open($myFile) $firstSheet = $targetWorkbook.Worksheets.Item(1) $firstSheet.Cells.Item(6,2).Formula = "=SE(VAL.ERRORE(MEDIA(..."

appariva il seguente errore:

Eccezione da HRESULT: 0x800A03EC In riga:1 car:1 + $firstSheet_N.Cells.Item(6,2).Formula = "=SE(VAL.ERRORE(MEDIA(..." + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMExcep tion

Dopo qualche ricerca e qualche intuizione, ho ricondotto l’errore alle impostazioni internazionali di Excel.

Ho modificato l’ultima riga nel modo seguente:

$firstSheet.Cells.Item($r,2).FormulaLocal = "=SE(VAL.ERRORE(MEDIA(..."

Come si vede, al posto di .Formula ho usato .FormulaLocal che fa funzionare il passaggio di stringhe con le impostazioni internazionali locali (inclusi i separatori,…).

Ho rilanciato lo script e ha funzionato tutto correttamente.

Mappare disco di rete in PowerShell

Oltre al semplice NET USE, in PowerShell è possibile usare il ComObject “WScript.Network” per gestire la mappatura di un disco di rete (map network drive).

Questo semplice script serve per mappare un disco di rete e per rimuovere la mappatura:

Write-Host "] Mapping Network Drive . . ." # Credentials $serverName = 'TESTPC01' $userName = 'TESTPC01\Administrator' $serverPwdSec = Read-Host -assecurestring "] Server password" # Ask for the user password $serverPwd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($serverPwdSec)) $network = New-Object -ComObject WScript.Network try {     # Previous method: NET USE T: \\$serverName\folderTest /USER:$userName $serverPwd     $network.MapNetworkDrive("T:", "\\$serverName\folderTest", $false, "$userName", "$serverPwd") # $network.MapNetworkDrive("T:", "\\$serverName\folderTest") # Without credential     Write-Host "] Network Drive mapped!`n" -BackgroundColor Green -ForegroundColor Black; } catch {     Write-Host "[An error occurred in drive mapping!]" -BackgroundColor Red -ForegroundColor Black;     Write-Host "[Error message:" $_.Exception.Message "`n" -BackgroundColor Red -ForegroundColor Black; }
Write-Host "] Remove Network Drive . . ." try {     $network.RemoveNetworkDrive("T:")     # Previous method: NET USE T: /delete } catch {     Write-Host "[An error occurred in drive unmapping!]`n" -BackgroundColor Red -ForegroundColor Black; }

Inviare email con Outlook in PowerShell

Questa funzione in Powershell permette di inviare e-mail tramite Outlook (che deve essere in esecuzione).

E’ basata sullo script presente su: http://www.andyparkhill.co.uk/2010/08/send-outlook-email-via-powershell.html a cui ho effettuato delle modifiche per l’invio dell’email a più destinatari e per l’invio degli allegati.

function Send-Email (     [String[]]$recipientEmail = $(Throw "At least one recipient email is required!"),     [String]$subject = $(Throw "An email subject header is required!"),     [String]$body,     [String]$attachment ) {     $outlook = New-Object -comObject Outlook.Application     $mail = $outlook.CreateItem(0)     $recipientEmail | ForEach-Object { $mail.Recipients.Add($_) } | Out-Null     $mail.Subject = $subject     $mail.Body = $body     # For HTML encoded emails     # $mail.HTMLBody = "<HTML><HEAD>Text<B>BOLD</B>  <span style='color:#E36C0A'>Color Text</span></HEAD></HTML>"     if ($attachment.Length -ne 0) {         $mail.Attachments.Add($attachment) # | Out-Null     }     $mail.Send()     # Write-Host "] Email sent!" -BackgroundColor Green -ForegroundColor Black; }
# Main Script Body Write-Host "] Start sending mail via Outlook . . ." [String[]]$recipientEmail = 'mail_1@domain.test','mail_2@domain.test','mail_N@domain.test' $subject = "This is a test" $body = "Test test test" # Optional $attachment = "C:\Temp\test.txt" # Optional if (Test-Path $attachment) {     Send-Email -recipientEmail $recipientEmail -subject $subject -body $body -attachment $attachment } else {     Send-Email -recipientEmail $recipientEmail -subject $subject -body $body }
# End of Script Body