Hebrew send to google spreadsheet from PowerShell Hebrew correctly - powershell

Please help.
In console look hebrew correct but in spreadsheet is not correctenter image description here
$import = New-Object System.Collections.ArrayList($null)
$import.Add( #("route_id"))
$import.Add( #( 'אבא אבן 1 הרצליה' )) | Out-NULL
try {
$Response = Set-GSheetData -accessToken $accessToken -rangeA1 "A1:X$($import.Count)" -sheetName "23-07-21" -spreadSheetID $SpreadsheetID -values $import -Verbose
$Response | ConvertTo-Json
} catch {
$err = $_.Exception
$err | Select-Object -Property *
"Response: "
$err.Response
}

Hebrew might not be supported in Sheets, you would need to install a font. You can try with this one
https://fonts.google.com/noto/specimen/Noto+Sans+Hebrew?query=hebrew
Update
After testing using the Rest API, it works, so the issue might be related on how the PowerShell process hebrew characters.
I've found some information on how to process hebrew characters in the Windows Powershell
[Console]::OutputEncoding = [Text.Encoding]::GetEncoding(1255)
Make sure the console is outputting the right characters and the POST a request to Google Sheets

It's bug in the psmodule. Author must specify encoding UTF8.
https://github.com/umn-microsoft-automation/UMN-Google/pull/50

Related

Outputting a file with an Azure Function

I'm trying to experiment with Azure Functions. Basically my use case is calling the function with a GUID as GET Parameter, having the function download the WIX toolkit DLL and an MSI file, updating a parameter in the MSI file, and the returning that file to the caller of the function (as download prompt for example).
I'm mostly there, just need some help getting the download prompt/send to happen, my code so far:
$urlWix = "http://domain/wix.dll"
$outputWix = "$Env:TEMP\wix.dll"
Invoke-WebRequest -Uri $urlWix -OutFile $outputWix
try{Add-Type -Path $outputWix}catch{$Null}
$urlMSI = "http://domain/file.msi"
$outputFile = "$Env:TEMP\file.msi"
Invoke-WebRequest -Uri $urlMSI -OutFile $outputFile
$oDatabase = New-Object Microsoft.Deployment.WindowsInstaller.Database($outputFile,[Microsoft.Deployment.WindowsInstaller.DatabaseOpenMode]::Direct);
$sSQLQuery = "SELECT * FROM Property WHERE Property= 'MYPROPERTY'"
[Microsoft.Deployment.WindowsInstaller.View]$oView = $oDatabase.OpenView($sSQLQuery)
$oView.Execute()
$oRecord = $oView.Fetch()
$oRecord.SetString("Value","MyCustomValue")
$oView.Modify([Microsoft.Deployment.WindowsInstaller.ViewModifyMode]::Update,$oRecord)
$oView.Close();
$oDatabase.Dispose();
$file = get-item $outputFile
write-output $file
Unfortunately due to content type issues this is not possible in powershell. You can do this via a C#, F#, or Node (isRaw) function. The problem is that you need to specify headers via the JSON response format, which would convert any non-text data into a base64 string.
If you want to sent a text file via powershell it is possible:
$response = ConvertTo-JSON #{
Body="your file data";
Headers=#{
# unfortunately it seems functions does not support 'filename=...'
'Content-Disposition'='attachment';
# you would use application/octet-stream, but because it's converted to JSON you lose binary content
'Content-Type'='text/plain';
};
}
Out-File -Encoding Ascii -FilePath $res -inputObject $response

Manipulating a CSV with Powershell

At the moment I am working on a script to automate a process in IE to add Computer Names and their MACs so we can image them. The page has two fields one for MAC and one for a computer name then a add new button. I had to come to a pretty sloppy solution for avoiding a popup from the page by just quitting out of the com object after submitting.
I don't have much experience with Powershell yet and none with working with CSVs so I'm having a bit of trouble making this work. My goal is to have the script read two entries from a row fill out the correct field then submit it then move to the next row and repeat.
Right now what it does is fills out the fields with undefined in both fields, then submits and repeats.
EDIT: I have edited my code slightly just so it confirms what is trying to read.This is what the results look like. I believe #WalterMitty is on to something that something is wrong with $ie.document.getElementsByName lines, I just tried $ie.document.getElementById but that didn't fill out any fields. It seems it has no problem reading the CSV, but it does have a problem entering the information it reads into the fields properly.
This is an example of what the CSV would look like.
NewComputerName,NewMACAddress
ComputerName1,111122223333
ComputerName2,112233446677
ComputerName3,AAAABBBBCCCC
ComputerName4,AABBCCDDEEFF
This is what my code currently looks like.
cls
$URL = ""
$iterator = 1;
$csv = Get-Content C:\example1.csv
foreach($row in $csv)
{
#starts IE
$ie = new-object -ComObject "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($URL)
while($ie.Busy -eq $true) { start-sleep -Milliseconds 100 }
($ie.document.getElementsByName("mc_id") |select -first 1).value = $_.NewComputerName;
($ie.document.getElementsByName("mac_id") |select -first 1).value = $_.NewMACAddress;
$ie.document.forms | Select -First 1| % { $_.submit() };
$ie.quit()
$iterator++
write-host "$iterator new ID(s) added"
write-host $row.NewComputerName - $row.NewMACAddress
}
$URL = ""
$iterator = 1
# use Import-Csv for CSV files
$csv = Import-Csv "C:\example1.csv" -Delimiter ","
foreach($row in $csv) {
Write-Host "$iterator new ID(s) added"
#starts IE
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Visible = $true
$ie.Navigate($URL)
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 100 }
# $_ is not defined in foreach blocks, you have to use $row here
($ie.Document.getElementsByName("mc_id") | Select-Object -First 1).Value = $row.NewComputerName
($ie.Document.getElementsByName("mac_id") | Select-Object -First 1).Value = $row.NewMACAddress
$ie.Document.Forms | Select-Object -First 1 | ForEach-Object { $_.submit() }
$ie.Quit()
$iterator++
}
I'm having similar issues but I just found this may help you guys - I don't have 50 reputation to comment sorry :/....
I messed around with the -Path of the Import-CSV command but I just couldn’t make it work. Apparently this has nothing to do with the path of the CSV file. The Warlock posted this on his blog:
Long story short, the error came from having trailing blank columns in
my CSV. Import-Csv uses the first row in the CSV as names for the
columns (unless you specify otherwise) and when you have blank columns
(or at least multiple blank columns) it causes this error as it
doesn’t have a valid name for them.
Instead of changing the file, I changed my import command to include the headers as per Dale's comment and it worked perfectly:
$data = import-csv "C:\Sharepoint.csv" -header("Department","AD Group","Members","Notes")
The Warlock and Dale saved me lots of time, please stop by the Warlock’s blog and give them a big Thanks
Consider using Import-Csv and Invoke-WebRequest in combination, e.g. like this:
import-csv .\example.csv | %{ iwr http://someurl.local -body #{mc_id=$_.NewComputerName; mac_id=$_.NewMACAddress} -Method POST }
It will read the csv file, iterate over the records and create a application/x-www-form-urlencoded POST request with the values from each record.
When you use iwr (Invoke-WebRequest) and pass a hash table as the "body" it will act as if it is a form being submitted. The POST method will submit the form values as application/x-www-form-urlencode. Without the POST method it would submit the form as if it was a GET, i.e. pass the values in the url.
If you need authentication, session support etc. then read the documentation for Invoke-WebRequest.
Using IE to automate web requests is brittle and error prone.

Encoding not working when email send by Powershell

Im sending a testemail via powershell like
$messageParameters = #{
Subject = "Email Tool"
Body = Get-Content "C:\body.txt" | out-string
From = "Info <info#xy.de>"
To = "Me <me#xy.de>"
SmtpServer = "mail.xy.de"
Encoding = New-Object System.Text.UTF8Encoding
}
send-mailmessage #messageParameters -BodyAsHtml
everything is working find except the encoding.
if i don't use encoding some characters are send as ??
and if i use it, what i actually want to do, than i get this Ä Ö Ü
but it should be ä ö ü and not this above.
If i don't send the mail as HTML it works.
How can i send the mail with the right encoding AND as html ?
I believe the problem is that your text-file is getting jumbled when you are reading it into a variable as non-utf8.
I would try getting the text file as UTF-8 and keeping the Encoding line.
Body = Get-Content "C:\body.txt" -Encoding UTF8 | Out-String
EDIT: Added Out-String per Dwza.

Powershell outlook message parsing

I'm wrote this script to look for NDR messages in Outlook. I want to find these messages and parse the body of the message to extract the email address the message bounced back from. Finding the messages with the subject is working fine. The issue I'm having is the body of the NDR emails are garbled with strange characters so I cannot parse them.
Anyone know how I can get around this?
$olFolderInbox = 6
$outlook = new-object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder($olFolderInbox)
$targetfolder = $inbox.Folders | ? { $_.name -eq "Dump" }
$targetfolder.items | % {
if($_.subject -match "Undeliverable"){
write-output $_.subject
write-output $_.SentOnBehalfOfName
write-output $_.body
}
}
sample from
$_.body
格浴㹬格慥㹤਍洼瑥⁡瑨灴攭畱癩∽潃瑮湥⵴祔数•潣瑮湥㵴琢硥⽴瑨汭※档牡敳㵴獵愭捳楩㸢⼼敨摡㰾潢祤ാ㰊㹰戼㰾潦瑮挠汯牯∽〣〰㘰∶猠穩㵥㌢•慦散∽牁慩≬䐾汥癩牥⁹慨⁳慦汩摥琠桴獥⁥敲楣楰湥獴漠⁲牧畯獰㰺是湯㹴⼼㹢⼼㹰਍昼湯⁴潣潬㵲⌢〰〰〰•楳敺∽∲映捡㵥吢桡浯≡㰾㹰愼栠敲㵦洢楡瑬㩯湁牵条
䴮瑩慲牀换浣挮浯㸢湁牵条䴮瑩慲牀换浣挮浯⼼㹡戼㹲਍桔⁥ⵥ慭汩愠摤敲獳礠畯攠瑮牥摥挠畯摬❮⁴敢映畯摮‮汐慥敳挠敨正琠敨爠捥灩敩瑮猧攠洭楡摡牤獥⁳œں⁤牴⁹潴爠獥湥⁤桴⁥”®«Aجُ⹥䤠⁦桴⁥ ¶³¶¾´潣瑮湩敵ⱳ瀠敬獡⁥‌ک¬—¼ٌ⁴‌¨ا£–ك›D“µھ—⹫戼㹲਍⼼㹰਍瀼㰾⁡牨晥∽
慭汩潴䄺“³اأطرں¨کO䁮’Rµ·⹭潣≭䄾摮敲匮煥極䁮’Rµ·⹭潣㱭گè㰾ہخാ吊敨攠洭楡摡牤獥⁳‌¨⁵œ£صûاأ⁤潣汵湤琧戠⁥ءتœ±⹤倠敬獡⁥µµ’ج桴⁥اأé¹کKœ£❴⁳ⵥ慭汩愠摤敲獳愠摮琠祲琠敲敳摮琠敨洠獥慳敧‮晉琠敨瀠潲汢浥挠湯楴畮獥‬汰慥敳挠湯慴瑣礠畯⁲”«ى
ل”¨ڑr㰮ہخാ㰊瀯ാ㰊㹰愼栠敲㵦›¥ک#¬–㩯±âکE‘]⹶獁橩䁡’Rµ·⹭潣≭䄾桢湩癡䄮کM‘Y —»»ن½’«ن´⼼㹡‘ù㹲਍桔⁥ⵥ慭汩愠摤敲獳礠畯攠瑮牥摥挠畯摬❮⁴敢映畯摮‮汐慥敳挠敨正琠敨爠捥灩敩瑮猧攠洭楡摡牤獥⁳œں⁤牴⁹潴爠獥湥⁤桴⁥”®«Aجُ⹥䤠⁦桴⁥
¶³¶¾´潣瑮湩敵ⱳ瀠敬獡⁥‌ک¬—¼ٌ⁴‌¨ا£–ك›D“µھ—⹫戼㹲਍⼼㹰਍⼼潦瑮ാ㰊牢㰾ہخ㰾ہخ㰾ہخ㰾ہخ㰾ہخാ㰊潦瑮挠汯牯∽㠣㠰㠰∰猠穩㵥¢ç•‘Wة¢∽慔潨慭㸢‍ڈ㰾㹢—èجُ³±¬›قوè‏ئصêô‘\کNœ«س³êôم³›ؤœ§ھ›
¹¬‹êô㩳⼼㹢⼼㹰਍瀼䜾œ£ °¬‹œ§⁧”²°d °›’R⹣潣㱭‍„ാ㰊㹰湁牵条䴮¬“‘` —»»ن½’«ن´‘ù㹲਍⌠氦㭴⌠⸵⸱‱浳灴㔻〵㔠ㄮㄮ删卅䱏䕖⹒䑁⹒敒楣买瑯潆湵㭤ا‎¬کس³®ڈ“³ëü㭴⌠䵓偔㰣‍„ാ㰊㹰湁牤⹥敓畱湩牀换浣挮浯戼㹲਍⌠氦㭴⌠⸵⸱‱浳灴㔻〵㔠ㄮㄮ删卅䱏䕖
⹒䑁⹒敒楣买瑯潆湵㭤ا‎¬کس³®ڈ“³ëü㭴⌠䵓偔㰣‍„ാ㰊㹰扁楨慮⹶獁橩䁡’Rµ·⹭潣㱭ہخാ ☣瑬※㔣ㄮㄮ猠瑭㭰㔵،ë⸵⸱‱䕒体噌剅䄮„q¹خ’ج‍¹‌‡䙴®ڈ“³،ù³±⁴ءتœ±♤瑧※匣呍⍐⼼㹰਍瀼伾楲楧慮敭獳条⁥”«“©
°㩳⼼㹰਍瀼敲刾捥楥敶㩤س³نû硭⸳扲⹣潣ㄨ⸰〲⸸㌱⸶⤵戠⁹塓䍓㑍㄰昮⹧扲⹣潣൭ ㄨ⸰〲⸸㤱⸲ㄴ
楷桴䴠قونû‌¤¬گ½³…ش⁐敓癲牥椠⁤㐱㌮ㄮ㐷ㄮ※敗Ɽ㐠䨠湵㈠㄰ഴ 㜱㐺㨳㤲ⴠ㐰〰਍ⵘ牉湯潐瑲䄭¬—⵩灓浡䘭مéصûاأ㩤¬_î´൥堊䤭نû‚آêô⵴湁楴匭慰⵭敒畳瑬›ڑM䉙䑁⌦㌴医ㅪ䕎䤶癥䝮獤㉢桊䅢䩄±’䈴شت䉅¶ç䩷਍ⵘ牉湯潐瑲䄭㩖䔠…]µئ‌œ㭳㵩½¹‌¥㭴⸴㠹㤬㔷ㄬ㤳ㄲㄸ〲
☰畱瑯㬻ഠ †㵤焦潵㭴“µ❦猿慣❮〲ⰸㄲ☷畱瑯㬻㵡焦潵㭴㠱〳㐲㠹☳畱瑯഻堊匭剂㩓㔠㘮਍敒散癩摥›êَن´®_ڑm³±œ³⠠䕈佌攠慳⸲慭歲瑥硡獥⹳灩浨⹸潣⥭⠠㙛⸸㌲⸲㌱⸵㜱崵ഩ 戠⁹浶㍸爮换挮浯眠瑩⁨ط¦…ش⽐䱔⽓䍒ⴴ䡓㭁〠‴畊〲㐱ㄠ㨷㌴ㄺ‵〭〴ര堊䤭نû‚آêô⵴噁›㵅‌Œ—Iâ´—
â☽畱瑯㐻㤮ⰸ㜹ⰵ㌱㈹㐱㈵〰焦潵㭴،ù਍†搠☽畱瑯瀻晤㼧’صœں¢ى㠰㈬㜱½¹‌¥㭴گô☽畱瑯ㄻ〶¤¨㠷焦潵㭴਍敒散癩摥›êَن´®_ڑm³±œ³⠠䕈佌挠灲敳浣楡桬ぴ⸲佃偒剏呁⹅佌䅃⥌⠠㉛㐰ㄮ㠲㔮⸲崰ഩ 戠⁹獥㉡洮ظہ敫慴數獳椮桰硭挮浯眠瑩⁨ط¦…ش⽐䱔⽓䕁ㅓ㠲匭䅈،°㔰䨠湵㈠㄰ഴ ㈰
㐺㨱㠵☠㐣㬳㔰〰਍敒散癩摥›êَن´‍u“·”²ن½ھ›·َぢ‱ㄨ⸰〴㈮㌵㌮⤰戠⁹剃卐䍅䅍䱉呈㈰䌮„y×ô䅒䕔䰮䍏䱁਍⠠〱㐮⸰ㄱ⸲㔱⤴眠瑩⁨—ٌ ®â´¾°⁴䵓‚½³ °”´⁲ؤ¦ㄠ⸴⸳㜱⸴㬱圠摥‬‴畊〲㐱਍ㄠ㨷ㄴ㔺،ا〭〴ര琊牨慥ⵤ湩敤㩸䄠♣㐣㬳偁䙡つ䵨䭏䕋半焹丹䍃‹C䜲…و㵑ഽ吊
牨慥ⵤ潔楰㩣䴠⁁䝈吠慲敤删捥灡〠⼶㐰㈯㄰ഴ䘊潲㩭☠瑬䴻ؤµë¸䅴”µ«A›µؤµë¸‘b”µ«A’«ن´ëü㭴਍潔ഺ䌊㩃☠瑬吻慲敤敒慣䑰ک#µ…›µؤµë¸‘b”µ«A’«ن´ëü㭴਍畓橢捥㩴䴠⁁䝈吠慲敤删捥灡〠⼶㐰㈯㄰ഴ䐊瑡㩥ˆL“‬‴畊〲㐱ㄠ㨷〴㐺‰〭〴ര䴊獥慳敧䤭㩄☠瑬䔻㘶㌹ぃ
㌸㌲㐳㜲䈴䘸䕃㈲㥃䐶㉂䄳䀸牐摯捵楴湯䰮’زخغëü㭴਍䥍䕍嘭牥楳湯›⸱ര䌊湯整瑮吭灹㩥›£›NکNخ؟¬ڑ›³چ“഻ऊ潢湵慤祲☽畱瑯ⴻⴭ㴭也硥側牡彴〰弰〰㔰た䌱㡆㄰⹃䄱〵㑂〵焦潵㭴਍ⵘ慍汩牥›—ٌ ®â´¾°⁴䑃⁏潦⁲饓³²J⁳〲〰਍潃瑮湥⵴汃獡㩳®_œ®ح¦œ«صû¬—’ھ‘[«
Aھ—›³ھ—‘a”ھ਍浉潰瑲湡散›³±›حخغ਍牐潩楲祴›³±›حخغ਍ⵘ楍敭䱏㩅‚‹نû®‡ة¢⁤祂䴠قونû‌¤¬گ䴠浩佥䕌嘠⸶〰㌮㤷⸰㤴㌱਍敒畴湲倭瑡㩨䴠牡敫䅴”µ«A›µؤµë¸‘b”µ«A’«ن´਍⼼牰㹥਍⼼潦瑮ാ㰊戯摯㹹਍⼼瑨汭
It's likely a bug in Outlook 2013. See:
http://answers.microsoft.com/en-us/office/forum/office_2013_release-outlook/when-forwarding-or-replying-to-emails-the-text/61280c0d-0fd6-48cf-9318-184ee3e83da9
In my case I got the strange gibberish when trying to view the body of a non delivery report from an Outlook 2013 addin (not such which version of Exchange we're using). But when the addin is disabled, the body of the non-delivery report shows up just fine.

Expressions are only allowed as the first element of a pipeline

I'm new at writing in powershell but this is what I'm trying to accomplish.
I want to compare the dates of the two excel files to determine if one is newer than the other.
I want to convert a file from csv to xls on a computer that doesn't have excel. Only if the statement above is true, the initial xls file was copied already.
I want to copy the newly converted xls file to another location
If the file is already open it will fail to copy so I want to send out an email alert on success or failure of this operation.
Here is the script that I'm having issues with. The error is "Expressions are only allowed as the first element of a pipeline." I know it's to do with the email operation but I'm at a loss as to how to write this out manually with all those variables included. There are probably more errors but I'm not seeing them now. Thanks for any help, I appreciate it!
$CSV = "C:filename.csv"
$LocalXLS = "C:\filename.xls"
$RemoteXLS = "D:\filename.xls"
$LocalDate = (Get-Item $LocalXLS).LASTWRITETIME
$RemoteDate = (Get-Item $RemoteXLS).LASTWRITETIME
$convert = "D:\CSV Converter\csvcnv.exe"
if ($LocalDate -eq $RemoteDate) {break}
else {
& $convert $CSV $LocalXLS
$FromAddress = "email#address.com"
$ToAddress = "email#address.com"
$MessageSubject = "vague subject"
$SendingServer = "mail.mail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SendEmailSuccess = $MessageBody = "The copy completed successfully!" | New-Object System.Net.Mail.SMTPClient mail.mail.com $SMTPMessage
$RenamedXLS = {$_.BaseName+(Get-Date -f yyyy-MM-dd)+$_.Extension}
Rename-Item -path $RemoteXLS -newname $RenamedXLS -force -erroraction silentlycontinue
If (!$error)
{ $SendEmailSuccess | copy-item $LocalXLS -destination $RemoteXLS -force }
Else
{$MessageBody = "The copy failed, please make sure the file is closed." | $SMTPClient.Send($SMTPMessage)}
}
You get this error when you are trying to execute an independent block of code from within a pipeline chain.
Just as a different example, imagine this code using jQuery:
$("div").not(".main").console.log(this)
Each dot (.) will chain the array into the next function. In the above function this breaks with console because it's not meant to have any values piped in. If we want to break from our chaining to execute some code (perhaps on objects in the chain - we can do so with each like this:
$("div").not(".main").each(function() {console.log(this)})
The solution is powershell is identical. If you want to run a script against each item in your chain individually, you can use ForEach-Object or it's alias (%).
Imagine you have the following function in Powershell:
$settings | ?{$_.Key -eq 'Environment' } | $_.Value = "Prod"
The last line cannot be executed because it is a script, but we can fix that with ForEach like this:
$settings | ?{$_.Key -eq 'Environment' } | %{ $_.Value = "Prod" }
This error basically happens when you use an expression on the receiving side of the pipeline when it cannot receive the objects from the pipeline.
You would get the error if you do something like this:
$a="test" | $a
or even this:
"test" | $a
I don't know why are trying to pipe everywhere. I would recommend you to learn basics about Powershell pipelining. You are approaching it wrong. Also, I think you can refer to the link below to see how to send mail, should be straight forward without the complications that you have added with the pipes : http://www.searchmarked.com/windows/how-to-send-an-email-using-a-windows-powershell-script.php