【发布时间】:2018-02-16 05:13:03
【问题描述】:
我在这里遇到了一个奇怪的问题。基本上我需要删除远程服务器上符合某些条件的用户的配置文件文件夹。我不想删除服务器上的实际用户配置文件(注册表项会自动删除,配置文件文件夹应该是但偶尔会卡住)所以 wmi 类不起作用,我只需要删除文件夹。
我已经彻底尝试过 get-childitem | remove-item 但总是存在权限问题。我唯一知道每次都会起作用的是 rmdir /s /q ,它涉及到一个命令提示符。我已经尝试了几种方法,但到目前为止,我认为我最接近的方法是在下面,我挂断的是试图将 $Directory 变量传递给命令提示符。我什至曾经深入研究过系统变量,但这确实行不通,如果可能的话,我宁愿避免使用它。
Function Profile-Cleanup {
$Session = New-PSSession -ComputerName $Computer
Enter-PSSession $Session
Invoke-Command -Session $Session -ScriptBlock {
cd c:\Users\
$Directories = Get-ChildItem ("c:\Users") | Where-Object {(($_.Name -like "1*") -OR ($_.Name -like "2*") -OR ($_.Name -like "3*") -OR ($_.Name -like "*Temp*")) -and ($_.Name -notlike "a*") -and ($_.Name -notlike "b*") -and ($_.Name -notlike "C*")}
ForEach($Directory in $Directories){
Start-Process cmd -ArgumentList "rmdir /q /s" $Directory.Name ,"Exit" -Wait -Verb RunAs -PassThru
}
}
Exit-PSSession
Get-PSSession | Remove-PSSession
}
好的,谢谢@gms0ulman 将完整命令绑定到变量是我需要的技巧。也谢谢@postanote,虽然我没有在这个脚本块中使用它,但是一旦我可以解决它,我将传递参数,但现在这是我的工作代码,供其他遇到此问题的人使用。
Function Profile-Cleanup {
$Session = New-PSSession -ComputerName $Computer
$actiongood = $True
Try {
Invoke-Command -Session $Session -ScriptBlock {
cd c:\Users\
$Computer = $env:COMPUTERNAME
$date = (get-date).ToString('MM-dd-yyyy')
$Directories = Get-ChildItem ("c:\Users") | Where-Object {(($_.Name -like "1*") -OR ($_.Name -like "2*") -OR ($_.Name -like "3*") -OR ($_.Name -like "*Temp*")) -and ($_.LastWriteTime -lt $date) -and ($_.Name -notlike "a*") -and ($_.Name -notlike "b") -and ($_.Name -notlike "c")}
ForEach($Directory in $Directories){
$process = "/c rmdir /q /s " + $Directory.Name
Start-Process cmd -ArgumentList $process, "Exit" -Wait -Verb RunAs
}
} -ErrorAction Stop
}Catch {
Write-Host "Unable to process Profile Cleanup on computer " $Computer " ...sorry" -ForegroundColor Red
$actiongood = $False
}
Get-PSSession | Remove-PSSession
If ($actiongood){
$FailedDirectories = Get-ChildItem ("\\"+ $Computer + "\C$\Users") | Where-Object {(($_.Name -like "1*") -OR ($_.Name -like "2*") -OR ($_.Name -like "3*") -OR ($_.Name -like "*Temp*")) -and ($_.LastWriteTime -lt $date) -and ($_.Name -notlike "a*") -and ($_.Name -notlike "n") -and ($_.Name -notlike "c")}
Write-Host "The following are the user profiles were unable to be fully deleted on " $Computer " likely because they were in use" -ForegroundColor Green
Write-Host "------------------------------------------------------------------" -ForegroundColor Green
Write-Host $FailedDirectories
Write-Host "------------------------------------------------------------------" -ForegroundColor Green
Write-Host "Starting Next Process" -ForegroundColor Blue -BackgroundColor Black
Write-Host "..." -ForegroundColor Blue -BackgroundColor Black
}
}
【问题讨论】:
-
这与必须使用字符串或数组来传递变量有关 - 找不到源。如果您使用变量来存储第一部分 -
$v = "rmdir /q /s" + $Directory.Name- 然后稍后使用 --ArgumentList $v ,"Exit" -
实际上我认为这可能已经完成了......一旦我得到完全确认,我将发布更新的代码。
-
做得很好。考虑发布解决方案编辑as an answer 和accepting it
标签: powershell powershell-remoting