powershell脚本在域上搜索文件

发布于 2025-01-24 21:17:47 字数 939 浏览 1 评论 0原文

我创建了一个简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2025-01-31 21:17:47

您所有的$ backups_file解决方案对我来说似乎是错误的。
如果要在远程系统上多余的目录,则必须至少是文件或管理共享,例如\\\\ computer \ e $ \ folder \ folder \ folder \

为什么为什么使用文件共享或当您简单地可以连接到远程主机上的PowerShell会话时,类似的事情?这是一个例子:

$mySQLServer = "Server1.domain.name", "server2.domain.name"
$backupFolder = "E:\blahBlahBla\SQL\Backups"
foreach ($server in $mySQLServer)
{
    $session = New-PSSession -ComputerName $server #maybe -cred if needed
    Invoke-Command -Session $session -ArgumentList $backupFolder -ScriptBlock {
        param(
            $directoy
        )
        if ($backup_file_exist)
        {
            # Verifies the folder exists
            Write-Output -InputObject "This folder exists"
            # returns all the files in the folder.
            Get-ChildItem -Path $directoy
            # Deletes all files in the folder that are older that 7 days.
            Get-ChildItem -Path $directoy -Recurse | Where-Object { ($_.LastWriteTime -lt (Get-Date).AddDays(-7))
            } | Remove-Item
        }
    }
    Remove-PSSession
}

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:

$mySQLServer = "Server1.domain.name", "server2.domain.name"
$backupFolder = "E:\blahBlahBla\SQL\Backups"
foreach ($server in $mySQLServer)
{
    $session = New-PSSession -ComputerName $server #maybe -cred if needed
    Invoke-Command -Session $session -ArgumentList $backupFolder -ScriptBlock {
        param(
            $directoy
        )
        if ($backup_file_exist)
        {
            # Verifies the folder exists
            Write-Output -InputObject "This folder exists"
            # returns all the files in the folder.
            Get-ChildItem -Path $directoy
            # Deletes all files in the folder that are older that 7 days.
            Get-ChildItem -Path $directoy -Recurse | Where-Object { ($_.LastWriteTime -lt (Get-Date).AddDays(-7))
            } | Remove-Item
        }
    }
    Remove-PSSession
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文