Powershell脚本用于清理临时文件

huangapple go评论64阅读模式
英文:

Powershell script for cleaning up temp files

问题

Currently working on a script that prompts the user to type a machine name in and then Remote-PSSession onto the machine and delete all the temp files.

$computer = Read-Host -Prompt "Input the machine name"
Enter-PSSession -ComputerName $computer

$tempfolders = @("D:\Users\*\AppData\Local\Temp\*", "D:\Users\*\Downloads\*", "D:\Users\*\AppData\Local\Webex\wbxcache\*", "D:\Users\*\AppData\Local\CrashDumps\*")
Remove-Item $tempfolders -force -recurse

All that happens when running the above is it tries to find "D Drive" on the host machine rather than on the expected machine, suggesting it doesn't run the Enter-PSSession command.

英文:

Currently working on a script that prompts the user to type a machine name in and then Remote-PSSession onto the machine and delete all the temp files.

$computer = Read-Host -Prompt "Input the machine name"
Enter-PSSession -ComputerName $computer

$tempfolders = @("D:\Users\*\AppData\Local\Temp\*", "D:\Users\*\Downloads\*", "D:\Users\*\AppData\Local\Webex\wbxcache\*", "D:\Users\*\AppData\Local\CrashDumps\*")
Remove-Item $tempfolders -force -recurse

All that happens when running the above is it tries to find "D Drive" on the host machine rather than on the expected machine suggesting it doesn't run the Enter-PSSession command

答案1

得分: 3

Enter-PSSession 适用于交互式使用!

对于无人值守脚本,请使用 Invoke-Command

$computer = Read-Host -Prompt "输入计算机名称"

Invoke-Command -ComputerName $computer -ScriptBlock {
    $tempfolders = @("D:\Users\*\AppData\Local\Temp\*", "D:\Users\*\Downloads\*", "D:\Users\*\AppData\Local\Webex\wbxcache\*", "D:\Users\*\AppData\Local\CrashDumps\*")
    Remove-Item $tempfolders -Force -Recurse
}
英文:

Enter-PSSession is for interactive use!

Use Invoke-Command for unattended scripts:

$computer = Read-Host -Prompt "Input the machine name"

Invoke-Command -ComputerName $computer -ScriptBlock {
    $tempfolders = @("D:\Users\*\AppData\Local\Temp\*", "D:\Users\*\Downloads\*", "D:\Users\*\AppData\Local\Webex\wbxcache\*", "D:\Users\*\AppData\Local\CrashDumps\*")
    Remove-Item $tempfolders -Force -Recurse
}

答案2

得分: 0

这不是你可以使用 PowerShell 会话在远程机器上运行命令的方法。

请按照以下步骤:

步骤 1:创建一个远程会话并将其存储在一个变量中:

$Session = New-PSSession -ComputerName server1 -Credential (Get-Credential -UserName domain\username -Message "输入凭据")

步骤 2:使用以下命令行来调用命令:

Invoke-Command -Session $Session -ScriptBlock { <# 你想要运行的命令 #> }

如果该命令行应该产生任何输出,你可以将其分配给任何变量并使用它。

英文:

This is not how you can run commands on a remote machine with PowerShell Session.

Please follow this:

Step 1: Create a remote session and store it in a variable:

$Session = New-PSSession -ComputerName server1 -Credential (Get-Credential -UserName domain\username -Message &quot;Enter credentials&quot;)

Step 2: Invoke the commands using below command line:

Invoke-Command -Session $Session -ScriptBlock { &lt;# Commands that you want to run #&gt; }

If that command line is supposed to give any output, you can assign it to any variable and use it.

huangapple
  • 本文由 发表于 2023年6月12日 18:38:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定