顯示具有 Powershell 標籤的文章。 顯示所有文章
顯示具有 Powershell 標籤的文章。 顯示所有文章

2023/10/17

POWERSHELL 結果匯出成csv 國字變亂碼問號

在用powershell把一些安全性log匯出成csv時,國字 都會變成問號,這時只要把最後匯出成csv的指令,後面在加個用utf8編碼的指令就可以正常顯示了。

原指令

 Export-Csv d:\eventlog.csv

修正後

 Export-Csv  d:\eventlog.csv -NoTypeInformation -encoding utf8


2023/02/15

powershell get 資料後以表格寄出

 有時後要用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.

2022/10/19

發信通知AD帳號 密碼快到期的人

 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

2021/02/18

網域電腦 不要退網域改電腦名稱

 之前網域內的電腦要改名稱,都是先退網域,改名稱,在重加網域,要重開機兩次,有點麻煩。

其實是有指令在DC上遠端變更client電腦名稱,指令是

Rename-Computer  -ComputerName  OldName  -NewName NewName  -DomainCredential  Domain\Username -Force

下完這個指令就會跳出要你輸入密碼的視窗,輸入後就會跳出成功,電腦重開後就完成更名。

在Client端電腦不會出現任何訊息,不會影響到user的操作,很方便。



2019/03/21

powershell 執行 smtp.send 發送郵件失敗"因為這個系統上已停用指令碼執行,所以無法載入..........................LinkID=135170。"

最近要透過powershell來發信,卻一直出現錯誤,一開始看到錯誤訊息有unauthorizedaccess,以為是要連到mail server的那段驗證出了問題,才會產生錯誤。




但後來測試過帳密沒錯,經查才發現,原來是因為powershell執行腳本的功能預設被鎖住了,要打開才行,只要用系統管理者開啟powershell並執行"Set-ExecutionPolicy -ExecutionPolicy RemoteSigned"。然後在執行"Get-ExecutionPolicy",確認結果是remotesigned就行了。

2018/10/17

Powershell 匯出 csv 無資料或資料有問題

運用Powershell撈資料,然後接用用export-csv要把資料匯出成csv檔時,發現資料有問題,就都是一些重覆的數字加文字而已,跟直接在powershell跑出來的資料完全不一樣。
會有這個問題,是因為powershell在撈資料時,如果語法裡有加"format-xxx"把匯出的資料做格式的調整,就會造成匯出csv檔時資料有問題,要改成"select-object"匯出csv才不會有問題。
可以參考這個網站 
https://blogs.msdn.microsoft.com/powershell/2007/03/07/why-cant-i-pipe-format-table-to-export-csv-and-get-something-useful/