如何获取当前脚本以从位于c:\的txt文件中读取计算机名称

时间:2018-12-18 22:31:05

标签: powershell

我目前正在使用此脚本,但是只能使其在本地运行,我希望它读取一个文本文件,该文件将存储在c:\ List_of_PCs.txt中,该文件的计算机名也应运行相同的脚本。这样,我可以更新文本文件,而不用修改代码。

Set-ExecutionPolicy RemoteSigned

# Get all users 
$users = Get-ChildItem -Path "C:\Users"

# Loop through users and delete the Teams file
$users | ForEach-Object {
Remove-Item -Path "C:\Users\$($_.Name)\AppData\Roaming\Microsoft\Teams\Cache\f*" -Force 
Remove-Item -Path "C:\Users\$($_.Name)\AppData\Roaming\Microsoft\Teams\Application Cache\Cache\f*" -Force 
}

对此的任何帮助,我都尝试过各种方法,我敢肯定这很简单,但对于PowerShell还是很陌生。

1 个答案:

答案 0 :(得分:2)

尝试这样的事情...

要求启用PowerShell远程处理并使用远程计算机上的管理员帐户

$ComputerList = Import-Csv -Path 'c:\List_of_PCs.txt'


$ComputerList | % { 
        Invoke-Command -ComputerName $_ -ScriptBlock {
        # Set-ExecutionPolicy RemoteSigned # this is something that should be set via GPO for all systems, not your script, so that it is centrally controlled and monitored.

        # Get all users 
        $users = Get-ChildItem -Path "C:\Users"

        # Loop through users and delete the Teams file
        $users | ForEach-Object {
        Remove-Item -Path "C:\Users\$($_.Name)\AppData\Roaming\Microsoft\Teams\Cache\f*" -Force 
        Remove-Item -Path "C:\Users\$($_.Name)\AppData\Roaming\Microsoft\Teams\Application Cache\Cache\f*" -Force 
        }
    }
}