PowerShell 運維菜鳥系列-01-批量為n臺服務器導入PFX證書(2017年除夕奉獻)
作為PowerShell菜鳥,我用PowerShell已有n年了,不怎麽使用,但積累不少實用的PowerShell腳本。
將在後續的工作中一一給大家分享。
項目背景:
在測試中使用一個公網證書,每3個月需要更新一次,重新申請、重新頒發、重新導入、重新分配。
但重新導入的時候,如果十臺、二十臺導入很繁瑣,同時也花時間。
本次測試花了我2天時間,從2018年2月14日星期三到2018年2月15日星期四。
本博文寫於2018年02月15日 20:00(2018年除夕之夜)。邊看春節聯歡晚會,邊寫博文。
1. 準備證書文件
2. 準備CSV-For-All Computers
3. 準備PowerShell-For-All Computers
#param( [string] $importfile = $(Read-Host -prompt "Please enter a file name"))
#$importedComputers = Import-CSV $importfile
$importedComputers = Import-CSV .\ComputerNames.csv
foreach ($importedComputer in $importedComputers)
{
Enter-PSSession -ComputerName $importedComputer.nComputerName
$pfxpath = 'C:\Cert\i-x-Cloud.com.pfx'
$password01 = '1'
Add-Type -AssemblyName System.Security
$cred01 = ConvertTo-SecureString "1" -AsPlainText -Force
$Username = 'i-x-Cloud\administrator'
$Password = 'Aa123456'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName $importedComputer.nComputerName -ScriptBlock { Import-PfxCertificate -FilePath 'C:\Cert\i-x-Cloud.com.pfx' Cert:\LocalMachine\my -Password (ConvertTo-SecureString -String '1' -AsPlainText -Force)} -credential $Cred
}
註意:
A.$importedComputers = Import-CSV .\ComputerNames.csv 讀取本地位置的csv中的計算機名到腳本中。
B.$importedComputer.nComputerName 讀取CSV中nComputerName中列。
C.$pfxpath = 'C:\Cert\i-x-Cloud.com.pfx' 讀取本地PFX證書
D. -Password (ConvertTo-SecureString -String '1' -AsPlainText -Force) 讀取PFX的證書的密碼
E. Invoke-Command -ComputerName $importedComputer.nComputerName 在遠程服務器上執行PowerShell腳本
4. 運行PowerShell
執行
5. 針對菜鳥,有幾個難點:
A. 如何實現將PFX證書導入到任何Windows Server 2012 R2加域服務器。
B. 如何實現將PFX證書的密碼傳入到遠程服務器中執行。
C. 將PFX證書放在本地C盤之中,如C:\Cert\i-x-Cloud.com.pfx,目的讀取方便。放在\\011-DC01.i-x-Cloud.com\Cert\i-x-Cloud.com.pfx,會存在一個問題,通過網絡讀取,需要解決用戶名和密碼的問題。未考慮如何去解決,後續如果有機會,再去解決。如果你解決了,可以奉獻給我,我更新到本博文中,方便大家。
如果有問題,請聯系QQ群:
企業統一溝通平臺實戰 65235615
本講師線下視頻課程:(咨詢:3313395633(QQ))
課程一:
《大企業Skype for Business Server 2015/Lync Server 2013部署實戰》
課程二:
《大企業雲桌面部署實戰》
課程三:
《跟菜鳥學Cisco UC部署實戰》
本講師其它視頻課程:
套餐:
***************************************************************
《私有雲部署實戰》-5個雲計算產品(含:Mircrosoft,Linux)(套餐)
http://edu.51cto.com/pack/view/id-516.html
***************************************************************
《私有雲-部署實戰-1-微軟-第1代-SSP20》
《私有雲-部署實戰-2-微軟-第2代-SC2012R2》
《私有雲-部署實戰-3-微軟-第3代-WAP》
《私有雲-部署實戰-4-Linux-Cloudstack 4.3》
《私有雲-部署實戰-5-Linux-OpenStack-juno》
***************************************************************
***************************************************************
《私有雲-基本功能-PK-微軟-And-Linux》
***************************************************************
http://edu.51cto.com/course/course_id-4875.html
***************************************************************
***************************************************************
從零開始一步一步學習Lync Server 2013(套餐)
http://edu.51cto.com/pack/view/id-65.html
***************************************************************
包括:
第 01 階段-規劃
第 02 階段-自動-管理-虛擬機
第 03 階段-準備-基礎-架構
第 04 階段-部署-基礎-環境
第 05 階段-部署-企業語音
第 06 階段-部署-呼叫管理功能
***************************************************************
《在多林環境中部署 Lync Server 2013》
http://edu.51cto.com/course/course_id-5896.html
***************************************************************
***************************************************************
《Skype for Business Server 2015-企業內部-部署》
http://edu.51cto.com/course/course_id-4158.html
《Skype for Business Server 2015-企業外部-部署》
http://edu.51cto.com/course/course_id-5890.html
***************************************************************
PowerShell 運維菜鳥系列-01-批量為n臺服務器導入PFX證書(2017年除夕奉獻)