今天我被这件事难住了。我有一个脚本,它通过提供计算机名来删除远程用户配置文件,它会从 C:\users 中的文件夹中列出一个列表,以根据他们的 ADUC 名称进行选择。我遇到的问题是我的排除参数,因为它似乎只接受一个参数。我宁愿不使用Get-ChildItem
UNC 路径来避免速度性能下降。
我已经尝试了很多东西,所以我将跳过在这里输入以节省空间。比我聪明的人能帮帮我吗?
#This is the where I would be passing the multiple "users" to exclude.
#Tried ArgumentList instead with no luck either.
[array]$User_List = Invoke-Command -ScriptBlock {
Get-ChildItem -Path "C:\Users" -Exclude Public, Default*, $Using:Exclude |
Sort-Object -Property LastWriteTime -Descending } -Session $PSSession
无能为力,但觉得我错过了一些东西。我只想能够提供要排除的用户列表(文件夹名称)。
Delete-UserProfile -ComputerName LocalHost
#作品Delete-UserProfile -ComputerName LocalHost -Exclude UserOne
#作品Delete-UserProfile -ComputerName LocalHost -Exclude UserOne, Usertwo
#不工作。
这是完整的脚本:
Function Delete-UserProfile{
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(Mandatory=$false,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true)]
[Alias('Comp','CN','NetBIOSName')]
[String[]]$ComputerName,
[Parameter(Mandatory=$false)]
$Exclude = $env:USERNAME )
Begin {
if ($null -eq $ComputerName) {
$ComputerName = Read-Host -Prompt "Enter Computer Name"
$ComputerName = $ComputerName -split ","
}
}
Process{
Foreach($Computer in $ComputerName){
Try{
$PSSession = New-PSSession -ComputerName $Computer -ErrorAction Stop
$CIMSession = New-CimSession -ComputerName $Computer -ErrorAction Stop
[array]$User_List = Invoke-Command -ScriptBlock {
Get-ChildItem -Path "C:\Users" -Exclude Public, Default*, $Using:Exclude |
Sort-Object -Property LastWriteTime -Descending } -Session $PSSession
[array]$userinfo1 = foreach ($user in $User_List.name) {
$userinfo = (net user $user /domain | Select-String "Full Name" -ErrorAction SilentlyContinue) -replace "Full Name ", "" 2>&1 | Out-String -Stream
if ($userinfo.Length -lt 4) { "NO DISPLAY NAME in ADUC" }
elseif($LASTEXITCODE -eq 2) { "ACCOUNT NOT in ADUC" }
elseif($LASTEXITCODE -eq 0) { $userinfo }
else { "Error occured" }
}
$(for($i=0; $i -lt $User_List.Count; $i++){
[pscustomobject]@{
'User Display Name ' = "${i}: $($userinfo1[$i])"
' Name ' = $User_List[$i].name
'Last Modified' = $User_List[$i].LastWriteTime
'Profile Size ' = Try{
$ProfilePath = $User_List[$i].FullName
$Profile_Sum = Invoke-Command -ScriptBlock {
Get-ChildItem -Path $Using:ProfilePath -Recurse |
Where-Object {$_.PSParentPath -match "Documents|Desktop|Music|Videos|Downloads|Links|Pictures|Favorites|Contacts"} |
Measure-Object -Property length -Sum |
Select-Object -ExpandProperty Sum } -Session $PSSession
if($Profile_Sum -lt 1048576){ " {0:N2}" -f ($Profile_Sum / 1KB) + " KB" }
elseif($Profile_Sum -gt 1048576 -and $Profile_Sum -lt 1073741824){ " {0:N2}" -f ($Profile_Sum / 1MB) + " MB" }
elseif($Profile_Sum -gt 1073741824){ " {0:N2}" -f ($Profile_Sum / 1GB) + " GB" } #Profile Size
} Catch { "$($Error[0].Exception.Message.Split('.')[2].Trim())!" }
}
} ) | Out-Host
Write-Warning "Ensure user profiles are no longer active and/or, have profiles be backed-up!"
Write-Host "Press 'Q' to quit. `nPress 'B' to backup profile. `nSeparate #'s with a comma (1, 2, 3)."
$ii = Read-Host -Prompt "Enter Number of user(s) to Delete"
$index = $ii.Trim() -split ","
if([String]::IsNullOrEmpty($index) -eq $true) { "Null string"; Break }
elseif($index.ToLower() -like "q*") {"Q was selected. Stopping script."; Break }
elseif($index.ToLower() -like "b*") {"B was selected. Stopping script. `nRunning PFL-UserBackUp. . . `n"; PFL-UserBackUp -Computer $Computer; Break }
" "
" Following Profiles will be Deleted:"
" ------------------------------------"
foreach($i in $index) { "$($i.trim()): $($userinfo1[$i])" }
" "
$Confirm = Read-Host -Prompt "Are you sure you want to continue? [Y/N]"
if($Confirm.ToLower().TrimStart() -like "n*" -or $Confirm.ToLower() -like "q*"){Break}
if([String]::IsNullOrEmpty($Confirm.Trim()) -eq $true) { "Null string"; Break }
foreach($i in $index) {
[PSCustomObject] @{
"Deleting User(S)" = $userinfo1[$i]
" Name " = $User_List[$i].name
"Deletion Status" = Try{
if($PSCmdlet.ShouldProcess($Computer, "Removing User Profile: $($userinfo1[$i])")){
for($l=0; $l -lt $index.Count;$l++){
Get-CimInstance -ClassName Win32_UserProfile -CimSession $CIMSession |
Where-Object { $_.LocalPath.split('\')[-1] -eq $User_List[$i].name } |
Remove-CimInstance
$Percent_Complete = ($l /$index.count) * 100
Write-Progress `
-Activity "Removing $($index.count) Profiles" `
-Status "Deleting user: $($userinfo1[$i])" `
-PercentComplete $Percent_Complete }
$Profile_Path = $User_List[$i].FullName
$TestPath = Invoke-Command -ScriptBlock { Test-Path $Using:Profile_Path } -Session $PSSession
if($TestPath -eq $false) { " DELETED" }
elseif($TestPath -eq $true) {
Invoke-Command -ScriptBlock { Remove-Item -Path $Using:Profile_Path -Recurse -Force } -Session $PSSession
$TestPath2 = Invoke-Command -ScriptBlock { Test-Path -Path $Using:Profile_Path } -Session $PSSession
if($TestPath2 -eq $false) { " DELETED" }
elseif($TestPath2 -eq $true) { CMD.exe /C RMDir /S /Q "\\$Computer\C$\Users\$($User_List[$i].name)"
$TestPath3 = Invoke-Command -ScriptBlock { Test-Path -Path $Using:Profile_Path } -Session $PSSession
if($TestPath3 -eq $false) { " DELETED" }
Else { "Unable To Delete" }
}
}
}
} Catch { "Error occurred! :(" }
}
}
} Catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
"Unable to connect to PC: $Computer `nError: $($Error[0].Exception.Message.Split('.')[2].Trim())!"
$IP = Test-Connection -ComputerName $Computer -Count 1 -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty IPV4Address |
Select-Object -ExpandProperty IPAddressToString
if($IP -EQ $True) { "IPAddress: $IP" }
Else { "IPAddress: Unable to get IP." }
} Catch [Microsoft.Management.Infrastructure.CimException] {
"Unable to establish CIM Session on PC: $Computer `n$($Error[0].Exception.Message.Split('.')[2].Trim())!"
} Finally {
if($PSSession) { Get-PSSession | Remove-PSSession }
if($CIMSession) { Get-CimSession | Remove-CimSession }
}
}
}
}
笔记:
除了我发布的当前问题之外,脚本本身确实有效。随时提供反馈!- 放轻松,哈哈,还在学习 -