在用powershell把一些安全性log匯出成csv時,國字 都會變成問號,這時只要把最後匯出成csv的指令,後面在加個用utf8編碼的指令就可以正常顯示了。
原指令
Export-Csv d:\eventlog.csv
修正後
Export-Csv d:\eventlog.csv -NoTypeInformation -encoding utf8
在用powershell把一些安全性log匯出成csv時,國字 都會變成問號,這時只要把最後匯出成csv的指令,後面在加個用utf8編碼的指令就可以正常顯示了。
原指令
Export-Csv d:\eventlog.csv
修正後
Export-Csv d:\eventlog.csv -NoTypeInformation -encoding utf8
有時後要用powershell去撈一些資料,然後寄出,如果已經用powershell篩出要的資料再寄出,語法不難,網路上一堆。
但假設我撈出了兩欄資料,我希望寄出來,是有表格的型式,這就難了。
在爬了一堆文後,找到一篇很實用的文章
https://evotec.xyz/sending-html-emails-with-powershell-and-zero-html-knowledge-required/
一開始就是先用get-xxx去把要的資料找出來,並且設成一個變數。
二是建立一個迴圈,把撈出來的資料依序帶入到一個表格容器裡面,設成一個變數。
三是用html語法建立一個表格,設定表格的樣式,抬頭名稱,欄位名稱,然後把步驟二的資料放到這個html的表格內,也是要設成一個變數。
四就是最後一步,用powersell發mail的指令發信,信的內容就是把步驟三產生的變數帶入,就完成了。
依照這個架構,就可以利用powershell去產出表格資料寄給需要的人。
Sometimes, I need to fetch data using PowerShell and then send it out. If I have already filtered the desired data using PowerShell, sending it out is not difficult, as there are plenty of examples available online.
However, if I want to send the fetched data in a tabular format, it becomes more challenging.
After searching through various resources, I found a very useful article:
https://evotec.xyz/sending-html-emails-with-powershell-and-zero-html-knowledge-required/
First, I use "Get-xxx" commands to retrieve the desired data and store it in a variable.
Next, I create a loop to iterate through the fetched data and populate it into a table container, which is stored as another variable.
Then, I use HTML syntax to create a table, set its style, define column headers, and incorporate the data from the previous step into the HTML table, which is stored as a variable as well.
Finally, in the last step, I use PowerShell's email sending command to send the email, and the content of the email is populated by inserting the variable generated in the previous step.
By following this framework, I can utilize PowerShell to generate tabular data and send it to the intended recipients.
windows系統內建的提醒實在太小了,就縮在右下角,而且預設兩週前會天天提醒,到剩一週後,就不會在顯示了,使用者早就忘了,所以需要一個比較顯示的提示。
可以參考這個下列這個powershell,就可以執到此功能,裡面程式碼有夠多,但只要先把要搜尋的dc位置,mail server位置,管理者信箱這3個設定值,調成自己環境內的資訊,就可以執行測試了。
預設是測試模式,所以只會寄給管理者,測試ok後再關閉測試模式,通知信就會發給使用者了,不需要修改太多東西就可以用了,讚。
https://gist.github.com/meoso/3488ef8e9c77d2beccfd921f991faa64#file-example-com-password-expiration-notifications-ps1
Notifying Users of Expiring Passwords via Email in Active Directory
The default built-in password reminder in Windows is quite inconspicuous, residing in the bottom right corner. Additionally, it only displays reminders every day up to two weeks before the expiration date. Once there is only one week left, the reminder disappears, and users tend to forget about it. Therefore, a more prominent notification is needed.
You can refer to the following PowerShell script to achieve this functionality. Although the code may seem extensive, you only need to adjust three configuration values: the location of the domain controller (DC), the mail server, and the administrator's email address. Once you set them according to your environment, you can execute a test run.
By default, the script runs in test mode, sending notifications only to the administrator. After confirming that it works correctly, you can disable test mode, and the notifications will be sent to the users. It requires minimal modifications, making it easy to use.
You can find the PowerShell script at the following link:
https://gist.github.com/meoso/3488ef8e9c77d2beccfd921f991faa64#file-example-com-password-expiration-notifications-ps1
之前網域內的電腦要改名稱,都是先退網域,改名稱,在重加網域,要重開機兩次,有點麻煩。
其實是有指令在DC上遠端變更client電腦名稱,指令是
Rename-Computer -ComputerName OldName -NewName NewName -DomainCredential Domain\Username -Force
下完這個指令就會跳出要你輸入密碼的視窗,輸入後就會跳出成功,電腦重開後就完成更名。
在Client端電腦不會出現任何訊息,不會影響到user的操作,很方便。