Powershell 循环服务器和驱动器名称的数组
我试图在我的服务器上查找旧文件,但Get IgnoredFiles函数的驱动器路径有点问题。我的服务器路径一直显示为\server\Powershell 循环服务器和驱动器名称的数组,powershell,audit,Powershell,Audit,我试图在我的服务器上查找旧文件,但Get IgnoredFiles函数的驱动器路径有点问题。我的服务器路径一直显示为\server\ Function Get-NeglectedFiles { Param([string[]]$path, [int]$numberDays) $cutOffDate = (Get-Date).AddDays(-$numberDays) Get-ChildItem -Path $path -Recurse | Where-Objec
Function Get-NeglectedFiles
{
Param([string[]]$path,
[int]$numberDays)
$cutOffDate = (Get-Date).AddDays(-$numberDays)
Get-ChildItem -Path $path -Recurse |
Where-Object {$_.LastAccessTime -le $cutOffDate}
}
$Endresults = @()
$serverlist = get-content "C:\temp\serverlist.txt"
foreach($server in $serverlist) {
$results = Get-WmiObject -ComputerName $Server -Class Win32_Share | select name
$Endresults += New-Object psobject -Property @{
Servername = $server
Result = $results
}
}
foreach($drive in $server){
$drives = $results | Where-Object { $_ -ne $null}
$serverpath = "\\" + $server + "\" + $drives + "\"
}
{Get-NeglectedFiles -path $serverpath -numberDays 90 | select name, lastaccesstime
}
您可能希望执行类似的操作(我对其进行了一些简化,但您可以对其进行扩展):
说明:
- 从文件
serverlist.txt
- 对于该列表中的每台服务器:
- 检索该服务器上的共享名列表
- 对于该服务器上的每个共享,生成一个
并运行serverpath
Get ignored files
- 对于该服务器上的每个共享,生成一个
- 检索该服务器上的共享名列表
旁注: 您可能还应该检查以下人员返回的内容:
Get-WmiObject -ComputerName $Server -Class Win32_Share
并确保所有返回的股票都是您想要使用的股票。例如,当我运行它时,我会获得诸如IPC$
、print$
、ADMIN$
、默认驱动器共享以及服务器上创建的所有其他自定义共享。您可能不会从这些文件中清除文件
另一个旁注:
你可能想考虑使用<代码> >文件>参数>你的代码>获取忽略文件命令,这样你只针对文件而不是目录。
我不确定它是否与它有任何关系,但是那是完美的!最后一个问题,您知道如何使用导出Excel在不同的工作表上列出每个服务器吗?而且它似乎不希望我能够在这些驱动器号上使用-Exclude:$serverlist=Get Content'C:\temp\serverlist.txt';foreach($serverlist中的服务器){$drives=Get wmioobject-ComputerName$server-Class Win32_Share{124;其中对象{$\u.Name-notlike'C$,'ADMIN$,'IPC$,'print$'}foreach($drives.Name中的驱动器){$serverpath=“\$server\$drive\”;$serverpath;Get IgnoredFiles-path$serverpath-NumberdDays 365 |选择名称、LastAccessTime、LastWriteTime、目录|导出Excel-path$PWD-AutoFilter};}@ctw-notlike
不接受项目数组,尝试将其更改为-notin
,这样做了,我还解决了导出Excel的问题:获取忽略的文件-path$serverpath-numberDays 365 |选择名称、LastAccessTime、LastWriteTime、目录|导出Excel-工作表名称“$serverpath”-path“C:\temp\OldFiles($server).xlsx“-自动过滤器
Get-WmiObject -ComputerName $Server -Class Win32_Share