用powershell写一个清理wsus过期更新的代码
时间: 2023-04-06 11:04:45 浏览: 100
我可以回答这个问题。以下是一个清理WSUS过期更新的PowerShell代码:
$Now = Get-Date
$Days = 30
$TargetFolder = "C:\WSUS\WsusContent\"
$LastWrite = $Now.AddDays(-$Days)
$Files = Get-ChildItem $TargetFolder -Recurse | Where-Object { $_.LastWriteTime -le "$LastWrite" }
foreach ($File in $Files)
{
if ($File -ne $NULL)
{
Write-Host "Deleting File $File" -ForegroundColor "Red"
Remove-Item $File.FullName | out-null
}
else
{
Write-Host "No more files to delete!" -foregroundcolor "Green"
}
}
请注意,这只是一个示例代码,您需要根据您的环境和需求进行修改。
相关问题
用powershell写一个测试网络的代码
我可以回答这个问题。以下是一个简单的 PowerShell 脚本,用于测试网络连接:
```
$computer = "www.google.com"
$ping = New-Object System.Net.NetworkInformation.Ping
$pingResult = $ping.Send($computer)
if ($pingResult.Status -eq "Success") {
Write-Host "Ping to $computer was successful."
} else {
Write-Host "Ping to $computer failed."
}
```
你可以将 `$computer` 变量设置为你想要测试的计算机的名称或 IP 地址。这个脚本使用 `System.Net.NetworkInformation.Ping` 类来发送 ICMP 回显请求并检查响应。如果 ping 成功,它将输出 "Ping to $computer was successful.",否则它将输出 "Ping to $computer failed."。
用powershell写一个自动定时导出共享文件夹权限的代码
可以尝试使用以下代码:
$FolderPath = "C:\SharedFolder"
$ReportPath = "C:\Reports\SharedFolderPermissions.csv"
$Acl = Get-Acl $FolderPath
$Ar = @()
foreach ($Access in $Acl.Access) {
$Ar += New-Object PSObject -Property @{
"FolderName" = $FolderPath
"AD Group or User" = $Access.IdentityReference
"Permissions" = $Access.FileSystemRights
"Inherited" = $Access.IsInherited
"Inheritance Flags" = $Access.InheritanceFlags
"Propagation Flags" = $Access.PropagationFlags
}
}
$Ar | Export-Csv -Path $ReportPath -NoTypeInformation
这段代码会将指定共享文件夹的权限信息导出到指定的 CSV 文件中。