powershell脚本在域上搜索文件
我创建了一个简单的PowerShell脚本,该脚本将在我的域上设置的管理VM上运行。我有一个单独的SQL VM运行一个备份脚本,随着时间的流逝,它会消耗大量存储。我正在尝试运行这个非常简单的脚本。
我是否需要修改此脚本以将其存储在我的管理VM上,但是否在我的SQL VM上运行?还是我可以按原样离开路径,只是在AD任务调度程序中设置?
我已经尝试针对FQDN和IP,但似乎并不是任何一种。
$backups_file = 'E:\blahBlahBla\SQL\Backups' or
$backups_file = '<IP_ADDRESS>\E:\blahBlahBla\SQL\Backups' or
$backups_file = '<FQDN>E:\blahBlahBla\SQL\Backups'
$backup_file_exist = (Test-Path -Path $backups_file)
if ($backup_file_exist){
# Verifies the folder exists
Write-Output -InputObject "This folder exists"
# returns all the files in the folder.
Get-ChildItem -Path $backups_file
# Deletes all files in the folder that are older that 7 days.
Get-ChildItem -Path $backups_file -Recurse | Where-Object {($_.LastWriteTime -lt (Get-
Date).AddDays(-7))} | Remove-Item
}
else
{
Write-Output -InputObject "Unable to access this directory."
}
I have created a simple PowerShell script that will run on an admin VM I have setup on my domain. I have a separate SQL VM running a backup script that consume a lot of storage over time. I am trying to run this very simple script.
Do I need to modify this script in order to store it on my admin VM, but have it run on my SQL VM? Or can I leave the path as is and just set up in AD task scheduler?
I have tried targeting the FQDN and the IP, but it doesn't seem to work either way.
$backups_file = 'E:\blahBlahBla\SQL\Backups' or
$backups_file = '<IP_ADDRESS>\E:\blahBlahBla\SQL\Backups' or
$backups_file = '<FQDN>E:\blahBlahBla\SQL\Backups'
$backup_file_exist = (Test-Path -Path $backups_file)
if ($backup_file_exist){
# Verifies the folder exists
Write-Output -InputObject "This folder exists"
# returns all the files in the folder.
Get-ChildItem -Path $backups_file
# Deletes all files in the folder that are older that 7 days.
Get-ChildItem -Path $backups_file -Recurse | Where-Object {($_.LastWriteTime -lt (Get-
Date).AddDays(-7))} | Remove-Item
}
else
{
Write-Output -InputObject "Unable to access this directory."
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所有的
$ backups_file
解决方案对我来说似乎是错误的。如果要在远程系统上多余的目录,则必须至少是文件或管理共享,例如
\\\\ computer \ e $ \ folder \ folder \ folder \
为什么为什么使用文件共享或当您简单地可以连接到远程主机上的PowerShell会话时,类似的事情?这是一个例子:
All your
$backups_file
solutions seems wrong to me.If you want excess a directory on a Remote system, it has to be at least a fileshare or a administrative share like
\\\\computer\e$\folder\folder\
But why using file shares or something like that when you just simple can connect to a PowerShell Session on the Remote Host? here is a example: