Powershell,使用特殊符号删除 win 服务器域配置文件
Powershell, win server domain profiles deletion with special symbols
我真的很不擅长编写脚本,但无论如何我需要创建一个脚本来删除具有特殊命名约定的特殊域帐户。我们正在使用 power shell v3。我被困在过滤配置文件区域。我有很多带有鸟类编号的配置文件,例如:bb1231X、ba1231z、bb1231rw。所以我只想删除包含 BB****X 的配置文件,并仔细检查标记为 7 个符号,7 个符号应该是 X,开头应该是 BB。
而且不知道怎么写这个复核。
任何帮助,将不胜感激。
当前脚本:
Function Get-System-Drive-Clean {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$computerName
)
PROCESS {
foreach ($computer in $computerName) {
Write-Verbose "Housekeeping on $computer"
Write-Verbose "Mapping drive \$computer\c$"
$drive = New-PSDrive -Name $computer.replace(".","-") -PSProvider FileSystem -Root \$computer\C$
write-Verbose "Checking windows version"
#Cheking windows version
$version = (Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem ).version
Write-Verbose "Windows version $version"
#Profile Deleting area.
if ($version -ge 6) {
write-Verbose "Getting profiles from WMI (Win 2k8 and above)..."
$profiles = Get-WmiObject Win32_UserProfile -ComputerName $computer -Filter "LocalPath like 'C:%R'"
if ($profiles -ne $null) {
$profiles | foreach {
Write-Verbose ("Deleting profile: " + $_.LocalPath)
#$_.Delete()
#| Where {(!$_.Special) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-5))}
}
}
}
}
}
}
}
Regular expressions(或简称 regex
)是您的朋友,PowerShell 对他们有原生支持!
您可以使用 -match
运算符进行正则表达式匹配:
PS C:\> 'BB8972X' -match '^BB.{4}X$'
True
PS C:\> 'BA9042W' -match '^BB.{4}X$'
False
我在上面的示例中使用的模式 (^BB.{4}X$
) 的工作原理如下:
^
:脱字符表示 "start of string position"
BB
: 这就是两个B
字符
.{4}
:在正则表达式中,.
表示 "any character"。 {4}
是一个量词,意思是"exactly 4 of the preceding character",所以任意字符的4
X
: 字母 X
$
:意思是"end of string position"
因此,如果您有许多具有这些名称的目录并且只想要名称类似于 BB****X
的目录,您可以这样做:
$BBXDirs = Get-ChildItem -Directory |Where-Object {$_.Name -match '^BB.{4}X$'}
我真的很不擅长编写脚本,但无论如何我需要创建一个脚本来删除具有特殊命名约定的特殊域帐户。我们正在使用 power shell v3。我被困在过滤配置文件区域。我有很多带有鸟类编号的配置文件,例如:bb1231X、ba1231z、bb1231rw。所以我只想删除包含 BB****X 的配置文件,并仔细检查标记为 7 个符号,7 个符号应该是 X,开头应该是 BB。
而且不知道怎么写这个复核。 任何帮助,将不胜感激。
当前脚本:
Function Get-System-Drive-Clean {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$computerName
)
PROCESS {
foreach ($computer in $computerName) {
Write-Verbose "Housekeeping on $computer"
Write-Verbose "Mapping drive \$computer\c$"
$drive = New-PSDrive -Name $computer.replace(".","-") -PSProvider FileSystem -Root \$computer\C$
write-Verbose "Checking windows version"
#Cheking windows version
$version = (Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem ).version
Write-Verbose "Windows version $version"
#Profile Deleting area.
if ($version -ge 6) {
write-Verbose "Getting profiles from WMI (Win 2k8 and above)..."
$profiles = Get-WmiObject Win32_UserProfile -ComputerName $computer -Filter "LocalPath like 'C:%R'"
if ($profiles -ne $null) {
$profiles | foreach {
Write-Verbose ("Deleting profile: " + $_.LocalPath)
#$_.Delete()
#| Where {(!$_.Special) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-5))}
}
}
}
}
}
}
}
Regular expressions(或简称 regex
)是您的朋友,PowerShell 对他们有原生支持!
您可以使用 -match
运算符进行正则表达式匹配:
PS C:\> 'BB8972X' -match '^BB.{4}X$'
True
PS C:\> 'BA9042W' -match '^BB.{4}X$'
False
我在上面的示例中使用的模式 (^BB.{4}X$
) 的工作原理如下:
^
:脱字符表示 "start of string position"BB
: 这就是两个B
字符.{4}
:在正则表达式中,.
表示 "any character"。{4}
是一个量词,意思是"exactly 4 of the preceding character",所以任意字符的4X
: 字母 X$
:意思是"end of string position"
因此,如果您有许多具有这些名称的目录并且只想要名称类似于 BB****X
的目录,您可以这样做:
$BBXDirs = Get-ChildItem -Directory |Where-Object {$_.Name -match '^BB.{4}X$'}