1. 批量远程修复DNS注册:
* foreach ($pc in $pcs) {.\psexec \\$pc -s "c:\windows\system32\ipconfig.exe" /registerdns}
2. 日历权限管理:
* Get-MailboxFolderPermission -Identity "m@m.com:\calendar"
* Remove-MailboxFolderPermission -Identity "hk.a@m.com:\calendar" -User "u, co"
* Set-MailboxFolderPermission -Identity ayla@contoso.com:\Calendar -User ed@contoso.com -AccessRights Editor -SharingPermissionFlags Delegate -SendNotificationToUser $true
3. 获取AAD用户信息:
Get-AzureADUser -all $true | ?{$_.displayname -like "o, M"}
4. 设置MSOL里用户的使用地 (UsageLocation):
* get-msoluser -UserPrincipalName m@a.com | fl usagelocation,country
UsageLocation : CN
Country : China
* get-msoluser -All | ft userprincipalname,usagelocation,country >> .\temp.txt
> $users = Get-Content .\temp.txt
> foreach ($u in $users) {
Get-MsolUser -UserPrincipalName $u | set-msoluser -UsageLocation "CN"
}
5。设置邮箱FA权限:
* Add-MailboxPermission -Identity "cc" -User "o, k" -AccessRights FullAccess -InheritanceType All
6. 管理邮箱的SendAs权限:
* Remove-RecipientPermission -Identity APC@d.com -AccessRights sendas -Trustee S-1-5-21-...
7. 查看目录Owner:
* GET-ACL
* (Get-ACL "AD:$((Get-ADUser username).distinguishedname)").access
8. 获取清单,利用Powershell逐个处理:
* $Users = Import-Csv "FileSystem::\\server\share\P\User.csv" | foreach { Get-*** | fl }
9. 获取设备上次启动时间:
- (gcim Win32_OperatingSystem).LastBootUpTime
- 变通一下,获取自上次重启后的运行时间:$uptime = (get-date) - (gcim Win32_OperatingSystem).LastBootUpTime
10. Win10、 Server2016/19 安装中文语言包
- 先从微软网站获取离线语言包安装文件
- 运行 DISM /online /add-package /packagepath=d:\temp\lp.cab 进行安装
- 系统设置里调整语言、地区等属性。
- 重启
11. Setspn -Q spnName --查询指定的SPN(需要知道SPN 名)
PS C:\> setspn -Q HTTP/WIN-PT4FFE #HTTP/WIN-PT4FFE是SPN名
Checking domain DC=cn-prod,DC=aaa,DC=com
CN=rs_uat,OU=...,DC=com
http/WIN-PT4FFE.cn-prod.aaa.com
http/WIN-PT4FFE
http/WIN-PT4FFE.cn-prod.aaa.com\PBIRS
12. Setspn -L targetAccountName --查看对应账号下注册的SPN(需要知道账号名)
PS C:\users\desktop> setspn -L rs_uat # rs_uat 是服务账号名
Registered ServicePrincipalNames for CN=rs_uat,OU=...,DC=aaa,DC=com:
http/WIN-PT4FFE
http/WIN-PT4FFE.cn-prod.aaa.com
http/WIN-PT4FFE.cn-prod.aaa.com\PBIRS
13. 远程启用Windows 功能, 以Telnet客户端为例
CMD: psexec \\computername dism /online /Enable-Feature /FeatureName:TelnetClient
PS: enter-pssession computername, 然后运行:Install-WindowsFeature -name "Telnet-Client"
14. 安装 Powershell 模块时, 报错:
WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'.
解决:
先运行命令: [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
15. 使用随机数作为PS命令所生成文件的文件名
dir | out-file ((random(100)).tostring()+".txt")
16. 365 项目要求将用户的UPN 统一成和电子邮件地址一样
PS C:\ > $sam = get-aduser -filter * -SearchBase "ou=test user,dc=com" -properties mail
ii. 检查用户帐号的mail 属性和 UPN 属性
PS C:\ > foreach ($v in $sam) { $v.mail}
PS C:\> foreach ($v in $sam) { $v.userprincipalname}
iii. 修改UPN
PS C:\ > foreach ($v in $sam) { set-aduser $v -replace @{userPrincipalName=$v.mail}}
17. 获取 Intune managed device 信息 ,比如下面的 ID,设备名和系统版本。
Get-IntuneManagedDevice | Get-MSGraphAllPages | ?{$_.operatingsystem -eq "Windows"} | ft id,devicename,osversion > .\id.csv
18. 启用远程电脑上的Powershell的另一种方法
日常运维中,通过enter-pssession 对远程电脑进行操作时,会遇到WinRM相关的报错。结果发现执行 “psexec \\远程电脑名 powershell” 命令后,也相当于是在远程电脑上进行Powershell操作。
19. 启用远程电脑的RDP支持
利用Enter-Pssession 连接到远程电脑的PS会话,然后运行“Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0”
20. 编辑SPO(Sharepoint Online)用户Profile里的扩展属性。
PS> Install-Module -Name "PnP.PowerShell" -RequiredVersion 1.12.0 -Force -AllowClobber
#先安装一个 PnP 模块,这个模块的安装可能会需要用到NuGet(可以运行 Install-PackageProvider -Name nuget -MinimumVersion 2.8.5.201 -force 来安装nuGet)
PS> Connect-PnPOnline -Url https://mkcorp.sharepoint.com -UseWebLogin
PS> set-PnPUserProfileProperty -Account user@xxx.com -PropertyName "employeetype" -Value "FTE"
要注意的是这里的Propertyname,可以从SPO 管理门户的用户配置文件属性界面里去找,也可以用(get-PnPUserProfileProperty -Account user@xxx.com).UserProfileProperties 里去找。
PS:弄了好几天才找到这个查询和修改SPO的用户扩展属性的方法,无意间发现原来是这么容易。网上好多文档说只能通过Graph API去改,差点信了。
然后就可以批量修改用户的属性了。
PS D:\> dir
目录: D:\ #先分别拿到各个属性值。
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2022/12/2 16:02 5549 Birth.txt
-a---- 2022/12/2 16:02 5471 Hire.txt
-a---- 2022/12/2 16:02 4022 ID.txt
-a---- 2022/12/2 16:01 11360 User.txt
PS D:\> $user =Get-Content .\User.txt
PS D:\> $id = Get-Content .\ID.txt
PS D:\> $hire = Get-Content .\Hire.txt
PS D:\> $birth = Get-Content .\Birth.txt
PS D:\> for ($i=0; $i -lt $user.Length; $i++) #循环一下就可以批量改掉了。
{
Set-PnPUserProfileProperty -Account $user[$i] -PropertyName "SPS-Birthday" -Value $birth[$i]
$show = (Get-PnPUserProfileProperty -Account $user[$i]).UserProfileProperties
$show.UserName + "' birthday is: " + $show."SPS-Birthday"
Set-PnPUserProfileProperty -Account $user[$i] -PropertyName "SPS-HireDate" -Value $hire[$i]
$show = (Get-PnPUserProfileProperty -Account $user[$i]).UserProfileProperties
$show.UserName + "' Hire Day is: " + $show."SPS-HireDate"
Set-PnPUserProfileProperty -Account $user[$i] -PropertyName "Id" -Value $id[$i]
$show = (Get-PnPUserProfileProperty -Account $user[$i]).UserProfileProperties
$show.UserName + "' ID is : " + $show."Id"
}
执行结果如下:
A@p.com' birthday is: 8/2 12:00:00 AM
A@p.com' Hire Day is: 10/10 12:00:00 AM
A@p.com' ID is: 601
B@p.com' birthday is: 10/2 12:00:00 AM
B@p.com' Hire Day is: 10/20 12:00:00 AM
B@p.com' ID is: 47
。。。
21. PSGallery 被当成 Untrusted repository的处理
PS C:\>Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
PS C:\>Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
PS C:\>Set-PSRepository -Name PSGallery -InstallationPolicy trusted
22. Enter-pssession后,无法通过install-windowsupdate 安装的补丁的问题(添加计划任务来解决)
PS C:\>Register-ScheduledJob -Name GetUpdates -ScriptBlock {install-windowsupdate -AcceptAll -IgnoreReboot} -runnow
23. Install windowsupdate remotely
PS C:\>install-module PSwindowsupdate
PS C:\>get-windowsupdate #check available updates
Then, refer to Item 22 above to install patches.
24. Fix system by DISM with SFC
C:\> dism /online /cleanup-image /restorehealth
C:\> sfc /scannow
25. 把PFX文件转换成 PEM格式
PS D:\> convert-pfxtopem #第一次运行,报错。
convert-pfxtopem : 无法将“convert-pfxtopem”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
PS D:\> install-module pspki #需要安装这个 PSPKI 模块
不受信任的存储库
你正在从不受信任的存储库安装模块。如果你信任该存储库,请通过运行 Set-PSRepository cmdlet 更改其 InstallationPolicy 值。是否确实要从“PSGallery”安装模块?
是(&Y) 全是(&A) 否(&N) 全否(&L) 暂停(&S)
Y
PS D:\> import-module pspki #然后再导入一下这个模块
PS D:\> convert-pfxtopem #再运行,就可以了。
Capacity MaxCapacity Length
-------- ----------- ------
6828 2147483647 4082
26. 彻底删除 MSOnlineUser
PS D:\> remove-msoluser -UserPrincipalName e.c@xxx.com
确认
是否继续执行此操作?
是(&Y) 否(&N) 暂停(&S)
Y
PS D:\> remove-msoluser -UserPrincipalName e.c@xxx.com -RemoveFromRecycleBin
确认
是否继续执行此操作?
是(&Y) 否(&N) 暂停(&S)
Y
27. 删除SPO User
PS D:\> Connect-SPOService -URL https://p-admin.sharepoint.com
PS D:\> Get-SPOUser -Site https://p.sharepoint.com -LoginName n.u@p.com
Display Name Login Name Groups User Type
------------ ---------- ------ ---------
u, n n.u@p.com {} Member
PS D:\> remove-SPOUser -Site https://p.sharepoint.com -LoginName n.u@mkcorp.com
PS D:\> Get-SPOUser -Site https://p.sharepoint.com -LoginName n.u@mkcorp.com
>> TerminatingError(Get-SPOUser):“User cannot be found.” #再找就已经找不到了。
28. 删除 PS Module的一个方法
Get-module * -ListAvailable | ft name,version,path
然后到对应的 Path里删除模块
29. 清除M365用户的MFA设置
$noMFAConfig = @()
$upn = <用户的UPN>
Set-MSOLUser -UserPrincipalName $upn -StrongAuthenticationMethods $noMFAConfig
30. 导出Exchange 服务器上的SMTP流量。
Get-MessageTrackingLog -Server <servername.domain> -start "03/18/2024" -source "SMTP" -eventid "HARECEIVE" -ResultSize unlimited | select-object timestamp,sender,recipients,messagesubject |export-csv -path d:\msg.csv -delimiter "|"
更进一步(后来再执行上面的命令时,发现导出的收件人全部变成了“SystemString[]”):
Get-MessageTrackingLog -Server <servername.domain> -start "03/18/2024" -source "SMTP" -eventid "HARECEIVE" -ResultSize unlimited | select-object timestamp,sender,{$_.Recipients},messagesubject |export-csv -path d:\msg.csv -delimiter "|"
//用 {$_.recipients} 代替直接使用的 recipients.
31. 让用户B有A用户邮箱的full access权限,且不让A账号自动映射到B的Outlook客户端
Add-MailboxPermission -Identity <Mailbox-UserA> -User <Mailbox-UserB> -AccessRights FullAccess -AutoMapping $false
32.